1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphfile.adapters;
15 import java.io.IOException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.DirectoryDialog;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.FileDialog;
24 import org.eclipse.swt.widgets.Shell;
25 import org.simantics.Simantics;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.Resource;
28 import org.simantics.db.common.request.ReadRequest;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.layer0.adapter.ActionFactory;
31 import org.simantics.db.request.Read;
32 import org.simantics.graphfile.Activator;
33 import org.simantics.graphfile.ontology.GraphFileResource;
34 import org.simantics.graphfile.util.GraphFileUtil;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.utils.ui.dialogs.ShowError;
40 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
43 public class ExportSystemResourcesActionFactory implements ActionFactory{
45 enum ResourceType{FILE,FOLDER,UNKNOW};
48 public Runnable create(Object _target) {
49 final Resource target = (Resource)_target;
50 return new Runnable() {
55 Shell shell= Display.getCurrent().getActiveShell();
57 ResourceType type = getResourceType(target);
59 if (type == ResourceType.UNKNOW)
62 String name = getName(target);
64 String exportName = null;
66 if (type == ResourceType.FILE) {
67 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
68 dialog.setFileName(name);
69 exportName = dialog.open();
71 DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);
72 exportName = dialog.open();
74 if (exportName == null)
77 final File targetFile = new File(exportName);
79 if (type == ResourceType.FILE) {
80 exportFile(shell, targetFile, target);
83 exportFolder(shell, targetFile, target);
86 } catch (Exception e) {
87 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to export file/folder to disk: " + target , e));
88 ShowError.showError("Error", e.getMessage(), e);
95 private String getName(final Resource resource) throws DatabaseException {
96 return Simantics.getSession().syncRequest(new Read<String>() {
98 public String perform(ReadGraph graph) throws DatabaseException {
99 Layer0 l0 = Layer0.getInstance(graph);
100 return graph.getPossibleRelatedValue(resource, l0.HasName);
105 private ResourceType getResourceType(final Resource resource) throws DatabaseException {
106 return Simantics.getSession().syncRequest(new Read<ResourceType>() {
108 public ResourceType perform(ReadGraph graph) throws DatabaseException {
109 GraphFileResource gf = GraphFileResource.getInstance(graph);
110 if (graph.isInstanceOf(resource, gf.File))
111 return ResourceType.FILE;
112 if (graph.isInstanceOf(resource, gf.Folder))
113 return ResourceType.FOLDER;
114 return ResourceType.UNKNOW;
119 private void exportFile(Shell shell, final File targetFile, final Resource target) throws Exception{
120 if (targetFile.exists()) {
121 if (!targetFile.isFile()) {
122 MessageDialog.openError(shell, "File Problem", "Output target is not a file " + targetFile.getAbsolutePath());
125 boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A file by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
129 if (!targetFile.delete()) {
130 MessageDialog.openError(shell, "Delete Problem", "Could not overwrite previously existing file " + targetFile.getAbsolutePath());
134 Simantics.getSession().syncRequest(new ReadRequest() {
137 public void run(ReadGraph graph) throws DatabaseException {
139 GraphFileUtil.writeDataToFile(graph, target , targetFile);
140 } catch (IOException e) {
141 throw new DatabaseException(e);
148 private void exportFolder(Shell shell, final File targetFile, final Resource target) throws Exception{
149 if (targetFile.exists()) {
150 if (!targetFile.isDirectory()) {
151 MessageDialog.openError(shell, "Folder Problem", "Output target is not a folder " + targetFile.getAbsolutePath());
154 String files[] = targetFile.list();
155 if (files.length > 0) {
156 boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A folder by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
160 GraphFileUtil.clearDirectoryStructure(targetFile);
164 if (!targetFile.mkdir()) {
165 MessageDialog.openError(shell, "Folder Problem", "Could not create new folder " + targetFile);
169 Simantics.getSession().syncRequest(new ReadRequest() {
172 public void run(ReadGraph graph) throws DatabaseException {
174 GraphFileUtil.writeFolderToDisk(graph, target, targetFile);
175 } catch (IOException e) {
176 throw new DatabaseException(e);