]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/ExportSystemResourcesActionFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / adapters / ExportSystemResourcesActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphfile.adapters;
13
14 import java.io.File;
15 import java.io.IOException;
16
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;
37
38 /**
39  * 
40  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
41  *
42  */
43 public class ExportSystemResourcesActionFactory implements ActionFactory{
44         
45         enum ResourceType{FILE,FOLDER,UNKNOW};
46         
47         @Override
48         public Runnable create(Object _target) {
49                 final Resource target = (Resource)_target;
50                 return new Runnable() {
51                         
52                         @Override
53                         public void run() {
54                                 try {
55                                         Shell shell= Display.getCurrent().getActiveShell();
56                                         
57                                         ResourceType type = getResourceType(target);
58                                 
59                                         if (type == ResourceType.UNKNOW)
60                                                 return;
61                                         
62                                         String name = getName(target);
63                                         
64                                         String exportName = null;
65                                         
66                                         if (type == ResourceType.FILE) {
67                                                 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
68                                                 dialog.setFileName(name);
69                                                 exportName = dialog.open();
70                                         } else {
71                                                 DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);
72                                                 exportName = dialog.open();
73                                         }
74                                         if (exportName == null)
75                                                 return;
76                                         
77                                         final File targetFile = new File(exportName);
78                                         
79                                         if (type == ResourceType.FILE) {
80                                                 exportFile(shell, targetFile, target);
81                                                 
82                                         } else {
83                                                 exportFolder(shell, targetFile, target);
84                                         }
85                                 
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);
89                                 } 
90                                 
91                         }
92                 };
93         }
94         
95         private String getName(final Resource resource) throws DatabaseException {
96                 return Simantics.getSession().syncRequest(new Read<String>() {
97                         
98                         public String perform(ReadGraph graph) throws DatabaseException {       
99                                 Layer0 l0 = Layer0.getInstance(graph);
100                                 return graph.getPossibleRelatedValue(resource, l0.HasName);
101                          }
102                 });
103         }
104         
105         private ResourceType getResourceType(final Resource resource) throws DatabaseException {
106                 return Simantics.getSession().syncRequest(new Read<ResourceType>() {
107                         
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;
115                         }
116                 });
117         }
118         
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());
123                 return;
124                 }
125                         boolean ok = MessageDialog.openConfirm(shell, "Overwrite", "A file by the name " + targetFile.getAbsolutePath() + " contains files.\n\nDo you want to overwrite the files?");
126                 if (!ok) {
127                         return;
128                 }
129                 if (!targetFile.delete()) {
130                         MessageDialog.openError(shell, "Delete Problem", "Could not overwrite previously existing file " + targetFile.getAbsolutePath());
131                 return;
132                 }
133                 }
134                 Simantics.getSession().syncRequest(new ReadRequest() {
135                         
136                         @Override
137                         public void run(ReadGraph graph) throws DatabaseException {
138                                 try {
139                                         GraphFileUtil.writeDataToFile(graph, target , targetFile);
140                                 } catch (IOException e) {
141                                         throw new DatabaseException(e);
142                                 }
143                                 
144                         }
145                 });
146         }
147         
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());
152                                 return;
153                         }
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?");
157                                 if (!ok) {
158                                         return;
159                                 }
160                                 GraphFileUtil.clearDirectoryStructure(targetFile);
161                         }
162
163                 } else {
164                         if (!targetFile.mkdir()) {
165                                 MessageDialog.openError(shell, "Folder Problem", "Could not create new folder " + targetFile);
166                                 return;
167                         }
168                 }
169                 Simantics.getSession().syncRequest(new ReadRequest() {
170                         
171                         @Override
172                         public void run(ReadGraph graph) throws DatabaseException {
173                                 try {
174                                         GraphFileUtil.writeFolderToDisk(graph, target, targetFile);
175                                 } catch (IOException e) {
176                                         throw new DatabaseException(e);
177                                 }
178                                 
179                         }
180                 });
181         }
182
183 }