]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFolder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ExportDocumentFolder.java
diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFolder.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFolder.java
new file mode 100644 (file)
index 0000000..e597ad5
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************\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 java.io.File;\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.jface.dialogs.MessageDialog;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.DirectoryDialog;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.DatabaseJob;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\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
+import org.simantics.graphfile.util.GraphFileUtil;\r
+\r
+/**\r
+ * Action for exporting file based documents.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class ExportDocumentFolder implements ActionFactory {\r
+       Resource relation;\r
+\r
+       public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {\r
+               relation = graph.getResource(relationUri);\r
+       }\r
+       \r
+       @Override\r
+       public Runnable create(Object target) {\r
+\r
+               if(!(target instanceof Resource))\r
+                       return null;\r
+\r
+               final Resource resource = (Resource)target;\r
+\r
+               return new Runnable() {\r
+                       @Override\r
+                       public void run() {\r
+                               Shell shell = Display.getCurrent().getActiveShell();\r
+                               DirectoryDialog dialog = new DirectoryDialog(shell,SWT.SAVE);\r
+                               String folderName = dialog.open();\r
+                               if (folderName == null) {\r
+                                       return;\r
+                               }\r
+                               File folder = new File(folderName);\r
+                               int choice = -1;\r
+                               if (folder.list().length > 0) {\r
+                                       MessageDialog messageDialog = new MessageDialog(shell, "Folder export", null, "Selected folder \"" + folderName + "\" is not empty.", MessageDialog.QUESTION, new String[]{"Delete and export","Overwrite","Cancel"}, 2);\r
+                                       choice = messageDialog.open();\r
+                                       if (choice == 2)\r
+                                               return;\r
+                                       \r
+                               }\r
+                               ExportJob job = new ExportJob(resource, folder, choice == 0);\r
+                               job.setUser(true);\r
+                               job.schedule();\r
+\r
+                       }\r
+               };\r
+       }\r
+       \r
+       private class ExportJob extends DatabaseJob {\r
+               Resource resource;\r
+               File folder;\r
+               boolean clear = false;\r
+               public ExportJob(Resource resource,File folder, boolean clear) {\r
+                       super("Export folder");\r
+                       this.resource = resource;\r
+                       this.folder = folder;\r
+                       this.clear = clear;\r
+               }\r
+               @Override\r
+               protected IStatus run(IProgressMonitor monitor) {\r
+                       try {\r
+                               monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);\r
+                               if (clear) {\r
+                                       GraphFileUtil.clearDirectoryStructure(folder);\r
+                                       monitor.worked(1);\r
+                               }\r
+                               FileDocumentUtil.exportDocumentFolder(resource, folder, relation,monitor);\r
+                               monitor.done();\r
+                               return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");\r
+                       } catch (Exception e) {\r
+                               monitor.done();\r
+                               return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);\r
+                       }\r
+               }\r
+       }\r
+}\r