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