Merge commit '0b471805f017da83d715a0d8409f53bdd009d31e'
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ExportDocumentFolder.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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.document.ui.actions;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.core.runtime.IProgressMonitor;\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.Shell;\r
24 import org.simantics.DatabaseJob;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.layer0.adapter.ActionFactory;\r
29 import org.simantics.document.ui.Activator;\r
30 import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
31 import org.simantics.graphfile.util.GraphFileUtil;\r
32 \r
33 /**\r
34  * Action for exporting file based documents.\r
35  * \r
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
37  *\r
38  */\r
39 public class ExportDocumentFolder implements ActionFactory {\r
40         Resource relation;\r
41         boolean useResourceNames;\r
42 \r
43         public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {\r
44                 relation = graph.getResource(relationUri);\r
45                 useResourceNames = true;\r
46         }\r
47 \r
48         public ExportDocumentFolder(ReadGraph graph, String relationUri, String useResourceNames) throws DatabaseException {\r
49                 relation = graph.getResource(relationUri);\r
50                 this.useResourceNames = useResourceNames.equals("true");\r
51         }\r
52         \r
53         @Override\r
54         public Runnable create(Object target) {\r
55 \r
56                 if(!(target instanceof Resource))\r
57                         return null;\r
58 \r
59                 final Resource resource = (Resource)target;\r
60 \r
61                 return new Runnable() {\r
62                         @Override\r
63                         public void run() {\r
64                                 Shell shell = Display.getCurrent().getActiveShell();\r
65                                 DirectoryDialog dialog = new DirectoryDialog(shell,SWT.SAVE);\r
66                                 String folderName = dialog.open();\r
67                                 if (folderName == null) {\r
68                                         return;\r
69                                 }\r
70                                 File folder = new File(folderName);\r
71                                 int choice = -1;\r
72                                 if (folder.list().length > 0) {\r
73                                         MessageDialog messageDialog = new MessageDialog(shell, "Folder export", null, "Selected folder \"" + folderName + "\" is not empty.", MessageDialog.QUESTION, new String[]{"Delete and export","Overwrite","Cancel"}, 2);\r
74                                         choice = messageDialog.open();\r
75                                         if (choice == 2)\r
76                                                 return;\r
77                                         \r
78                                 }\r
79                                 ExportJob job = new ExportJob(resource, folder, choice == 0);\r
80                                 job.setUser(true);\r
81                                 job.schedule();\r
82 \r
83                         }\r
84                 };\r
85         }\r
86         \r
87         private class ExportJob extends DatabaseJob {\r
88                 Resource resource;\r
89                 File folder;\r
90                 boolean clear = false;\r
91                 public ExportJob(Resource resource,File folder, boolean clear) {\r
92                         super("Export folder");\r
93                         this.resource = resource;\r
94                         this.folder = folder;\r
95                         this.clear = clear;\r
96                 }\r
97                 @Override\r
98                 protected IStatus run(IProgressMonitor monitor) {\r
99                         try {\r
100                                 monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);\r
101                                 if (clear) {\r
102                                         GraphFileUtil.clearDirectoryStructure(folder);\r
103                                         monitor.worked(1);\r
104                                 }\r
105                                 FileDocumentUtil.exportDocumentFolder(resource, folder, relation, useResourceNames, monitor);\r
106                                 monitor.done();\r
107                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");\r
108                         } catch (Exception e) {\r
109                                 monitor.done();\r
110                                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);\r
111                         }\r
112                 }\r
113         }\r
114 }\r