--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 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.graphfile.adapters;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\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.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graphfile.Activator;\r
+import org.simantics.graphfile.ontology.GraphFileResource;\r
+import org.simantics.graphfile.util.GraphFileUtil;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.ui.dialogs.ShowError;\r
+\r
+/**\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class ExportSystemResourcesActionFactory implements ActionFactory{\r
+ \r
+ enum ResourceType{FILE,FOLDER,UNKNOW};\r
+ \r
+ @Override\r
+ public Runnable create(Object _target) {\r
+ final Resource target = (Resource)_target;\r
+ return new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ try {\r
+ Shell shell= Display.getCurrent().getActiveShell();\r
+ \r
+ ResourceType type = getResourceType(target);\r
+ \r
+ if (type == ResourceType.UNKNOW)\r
+ return;\r
+ \r
+ String name = getName(target);\r
+ \r
+ String exportName = null;\r
+ \r
+ if (type == ResourceType.FILE) {\r
+ FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
+ dialog.setFileName(name);\r
+ exportName = dialog.open();\r
+ } else {\r
+ DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);\r
+ exportName = dialog.open();\r
+ }\r
+ if (exportName == null)\r
+ return;\r
+ \r
+ final File targetFile = new File(exportName);\r
+ \r
+ if (type == ResourceType.FILE) {\r
+ exportFile(shell, targetFile, target);\r
+ \r
+ } else {\r
+ exportFolder(shell, targetFile, target);\r
+ }\r
+ \r
+ } catch (Exception e) {\r
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to export file/folder to disk: " + target , e));\r
+ ShowError.showError("Error", e.getMessage(), e);\r
+ } \r
+ \r
+ }\r
+ };\r
+ }\r
+ \r
+ private String getName(final Resource resource) throws DatabaseException {\r
+ return Simantics.getSession().syncRequest(new Read<String>() {\r
+ \r
+ public String perform(ReadGraph graph) throws DatabaseException { \r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ return graph.getPossibleRelatedValue(resource, l0.HasName);\r
+ }\r
+ });\r
+ }\r
+ \r
+ private ResourceType getResourceType(final Resource resource) throws DatabaseException {\r
+ return Simantics.getSession().syncRequest(new Read<ResourceType>() {\r
+ \r
+ public ResourceType perform(ReadGraph graph) throws DatabaseException { \r
+ GraphFileResource gf = GraphFileResource.getInstance(graph);\r
+ if (graph.isInstanceOf(resource, gf.File))\r
+ return ResourceType.FILE;\r
+ if (graph.isInstanceOf(resource, gf.Folder))\r
+ return ResourceType.FOLDER;\r
+ return ResourceType.UNKNOW;\r
+ }\r
+ });\r
+ }\r
+ \r
+ private void exportFile(Shell shell, final File targetFile, final Resource target) throws Exception{\r
+ if (targetFile.exists()) {\r
+ if (!targetFile.isFile()) {\r
+ MessageDialog.openError(shell, "File Problem", "Output target is not a file " + targetFile.getAbsolutePath());\r
+ return;\r
+ }\r
+ boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A file by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");\r
+ if (!ok) {\r
+ return;\r
+ }\r
+ if (!targetFile.delete()) {\r
+ MessageDialog.openError(shell, "Delete Problem", "Could not overwrite previously existing file " + targetFile.getAbsolutePath());\r
+ return;\r
+ }\r
+ }\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ GraphFileUtil.writeDataToFile(graph, target , targetFile);\r
+ } catch (IOException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ \r
+ }\r
+ });\r
+ }\r
+ \r
+ private void exportFolder(Shell shell, final File targetFile, final Resource target) throws Exception{\r
+ if (targetFile.exists()) {\r
+ if (!targetFile.isDirectory()) {\r
+ MessageDialog.openError(shell, "Folder Problem", "Output target is not a folder " + targetFile.getAbsolutePath());\r
+ return;\r
+ }\r
+ String files[] = targetFile.list();\r
+ if (files.length > 0) {\r
+ boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A folder by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");\r
+ if (!ok) {\r
+ return;\r
+ }\r
+ GraphFileUtil.clearDirectoryStructure(targetFile);\r
+ }\r
+\r
+ } else {\r
+ if (!targetFile.mkdir()) {\r
+ MessageDialog.openError(shell, "Folder Problem", "Could not create new folder " + targetFile);\r
+ return;\r
+ }\r
+ }\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ GraphFileUtil.writeFolderToDisk(graph, target, targetFile);\r
+ } catch (IOException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ \r
+ }\r
+ });\r
+ }\r
+\r
+}\r