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