1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.actions;
\r
14 import org.eclipse.core.runtime.IProgressMonitor;
\r
15 import org.eclipse.core.runtime.IStatus;
\r
16 import org.eclipse.core.runtime.Status;
\r
17 import org.eclipse.swt.SWT;
\r
18 import org.eclipse.swt.widgets.DirectoryDialog;
\r
19 import org.eclipse.swt.widgets.Display;
\r
20 import org.simantics.DatabaseJob;
\r
21 import org.simantics.Simantics;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.WriteGraph;
\r
25 import org.simantics.db.common.request.WriteRequest;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.layer0.adapter.ActionFactory;
\r
28 import org.simantics.document.ui.Activator;
\r
29 import org.simantics.document.ui.graphfile.FileDocumentUtil;
\r
32 * Action for importing files as documents.
\r
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
37 public class ImportDocumentFolder implements ActionFactory {
\r
38 Resource folderType;
\r
41 public ImportDocumentFolder(ReadGraph graph, String folderTypeUri, String relationUri) throws DatabaseException {
\r
42 folderType = graph.getResource(folderTypeUri);
\r
43 relation = graph.getResource(relationUri);
\r
47 public Runnable create(Object target) {
\r
48 if(!(target instanceof Resource))
\r
50 final Resource resource = (Resource)target;
\r
51 return new Runnable() {
\r
55 DirectoryDialog dialog = new DirectoryDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
\r
56 final String filename = dialog.open();
\r
57 if (filename == null) {
\r
61 ImportJob job = new ImportJob("Import folder", resource, filename);
\r
69 private class ImportJob extends DatabaseJob {
\r
71 public ImportJob(String name, Resource resource, String filename) {
\r
73 this.resource = resource;
\r
74 this.filename = filename;
\r
81 protected IStatus run(final IProgressMonitor monitor) {
\r
83 Simantics.getSession().syncRequest(new WriteRequest() {
\r
85 public void perform(WriteGraph graph) throws DatabaseException {
\r
87 graph.markUndoPoint();
\r
88 FileDocumentUtil.importFolderWithName(graph,filename,resource,folderType,relation,monitor);
\r
89 } catch (Exception e) {
\r
90 throw new DatabaseException(e);
\r
94 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder imported.");
\r
95 } catch (DatabaseException e) {
\r
96 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot import document folder.", e);
\r