-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.graphfile.adapters;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.DirectoryDialog;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ActionFactory;
+import org.simantics.db.request.Read;
+import org.simantics.graphfile.Activator;
+import org.simantics.graphfile.ontology.GraphFileResource;
+import org.simantics.graphfile.util.GraphFileUtil;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.ui.dialogs.ShowError;
+
+/**
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class ExportSystemResourcesActionFactory implements ActionFactory{
+
+ enum ResourceType{FILE,FOLDER,UNKNOW};
+
+ @Override
+ public Runnable create(Object _target) {
+ final Resource target = (Resource)_target;
+ return new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ Shell shell= Display.getCurrent().getActiveShell();
+
+ ResourceType type = getResourceType(target);
+
+ if (type == ResourceType.UNKNOW)
+ return;
+
+ String name = getName(target);
+
+ String exportName = null;
+
+ if (type == ResourceType.FILE) {
+ FileDialog dialog = new FileDialog(shell, SWT.SAVE);
+ dialog.setFileName(name);
+ exportName = dialog.open();
+ } else {
+ DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);
+ exportName = dialog.open();
+ }
+ if (exportName == null)
+ return;
+
+ final File targetFile = new File(exportName);
+
+ if (type == ResourceType.FILE) {
+ exportFile(shell, targetFile, target);
+
+ } else {
+ exportFolder(shell, targetFile, target);
+ }
+
+ } catch (Exception e) {
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to export file/folder to disk: " + target , e));
+ ShowError.showError("Error", e.getMessage(), e);
+ }
+
+ }
+ };
+ }
+
+ private String getName(final Resource resource) throws DatabaseException {
+ return Simantics.getSession().syncRequest(new Read<String>() {
+
+ public String perform(ReadGraph graph) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(graph);
+ return graph.getPossibleRelatedValue(resource, l0.HasName);
+ }
+ });
+ }
+
+ private ResourceType getResourceType(final Resource resource) throws DatabaseException {
+ return Simantics.getSession().syncRequest(new Read<ResourceType>() {
+
+ public ResourceType perform(ReadGraph graph) throws DatabaseException {
+ GraphFileResource gf = GraphFileResource.getInstance(graph);
+ if (graph.isInstanceOf(resource, gf.File))
+ return ResourceType.FILE;
+ if (graph.isInstanceOf(resource, gf.Folder))
+ return ResourceType.FOLDER;
+ return ResourceType.UNKNOW;
+ }
+ });
+ }
+
+ private void exportFile(Shell shell, final File targetFile, final Resource target) throws Exception{
+ if (targetFile.exists()) {
+ if (!targetFile.isFile()) {
+ MessageDialog.openError(shell, "File Problem", "Output target is not a file " + targetFile.getAbsolutePath());
+ return;
+ }
+ boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A file by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
+ if (!ok) {
+ return;
+ }
+ if (!targetFile.delete()) {
+ MessageDialog.openError(shell, "Delete Problem", "Could not overwrite previously existing file " + targetFile.getAbsolutePath());
+ return;
+ }
+ }
+ Simantics.getSession().syncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ try {
+ GraphFileUtil.writeDataToFile(graph, target , targetFile);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ }
+
+ }
+ });
+ }
+
+ private void exportFolder(Shell shell, final File targetFile, final Resource target) throws Exception{
+ if (targetFile.exists()) {
+ if (!targetFile.isDirectory()) {
+ MessageDialog.openError(shell, "Folder Problem", "Output target is not a folder " + targetFile.getAbsolutePath());
+ return;
+ }
+ String files[] = targetFile.list();
+ if (files.length > 0) {
+ boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A folder by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
+ if (!ok) {
+ return;
+ }
+ GraphFileUtil.clearDirectoryStructure(targetFile);
+ }
+
+ } else {
+ if (!targetFile.mkdir()) {
+ MessageDialog.openError(shell, "Folder Problem", "Could not create new folder " + targetFile);
+ return;
+ }
+ }
+ Simantics.getSession().syncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ try {
+ GraphFileUtil.writeFolderToDisk(graph, target, targetFile);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ }
+
+ }
+ });
+ }
+
+}