]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocumentFolder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ImportDocumentFolder.java
diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocumentFolder.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocumentFolder.java
new file mode 100644 (file)
index 0000000..e09ff46
--- /dev/null
@@ -0,0 +1,101 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.document.ui.actions;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.DirectoryDialog;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.document.ui.Activator;\r
+import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
+\r
+/**\r
+ * Action for importing files as documents.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class ImportDocumentFolder implements ActionFactory {\r
+       Resource folderType;\r
+       Resource relation;\r
+\r
+       public ImportDocumentFolder(ReadGraph graph, String folderTypeUri, String relationUri) throws DatabaseException {\r
+               folderType = graph.getResource(folderTypeUri);\r
+               relation = graph.getResource(relationUri);\r
+       }\r
+\r
+       @Override\r
+       public Runnable create(Object target) {\r
+               if(!(target instanceof Resource))\r
+                       return null;\r
+               final Resource resource = (Resource)target;\r
+               return new Runnable() {\r
+                       \r
+                       @Override\r
+                       public void run() {\r
+                               DirectoryDialog dialog = new DirectoryDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);\r
+                               final String filename = dialog.open();\r
+                               if (filename == null) {\r
+                                       return;\r
+                               }\r
+\r
+                               ImportJob job = new ImportJob("Import folder", resource, filename);\r
+                               job.setUser(true);\r
+                               job.schedule();\r
+                       }\r
+               };\r
+       }\r
+       \r
+       \r
+       private class ImportJob extends DatabaseJob {\r
+\r
+               public ImportJob(String name, Resource resource, String filename) {\r
+                       super(name);\r
+                       this.resource = resource;\r
+                       this.filename = filename;\r
+               }\r
+\r
+               Resource resource;\r
+               String filename;\r
+\r
+               @Override\r
+               protected IStatus run(final IProgressMonitor monitor) {\r
+                       try {\r
+                               Simantics.getSession().syncRequest(new WriteRequest() {\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               try {\r
+                                                   graph.markUndoPoint();\r
+                                                       FileDocumentUtil.importFolderWithName(graph,filename,resource,folderType,relation,monitor);\r
+                                               } catch (Exception e) {\r
+                                                       throw new DatabaseException(e);\r
+                                               }\r
+                                       }\r
+                               });\r
+                               return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder imported.");\r
+                       } catch (DatabaseException e) {\r
+                               return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot import document folder.", e);\r
+                       }\r
+               }\r
+       }\r
+       \r
+}\r