]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFolder.java
Migrated source code from Simantics SVN
[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 \r
42         public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {\r
43                 relation = graph.getResource(relationUri);\r
44         }\r
45         \r
46         @Override\r
47         public Runnable create(Object target) {\r
48 \r
49                 if(!(target instanceof Resource))\r
50                         return null;\r
51 \r
52                 final Resource resource = (Resource)target;\r
53 \r
54                 return new Runnable() {\r
55                         @Override\r
56                         public void run() {\r
57                                 Shell shell = Display.getCurrent().getActiveShell();\r
58                                 DirectoryDialog dialog = new DirectoryDialog(shell,SWT.SAVE);\r
59                                 String folderName = dialog.open();\r
60                                 if (folderName == null) {\r
61                                         return;\r
62                                 }\r
63                                 File folder = new File(folderName);\r
64                                 int choice = -1;\r
65                                 if (folder.list().length > 0) {\r
66                                         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
67                                         choice = messageDialog.open();\r
68                                         if (choice == 2)\r
69                                                 return;\r
70                                         \r
71                                 }\r
72                                 ExportJob job = new ExportJob(resource, folder, choice == 0);\r
73                                 job.setUser(true);\r
74                                 job.schedule();\r
75 \r
76                         }\r
77                 };\r
78         }\r
79         \r
80         private class ExportJob extends DatabaseJob {\r
81                 Resource resource;\r
82                 File folder;\r
83                 boolean clear = false;\r
84                 public ExportJob(Resource resource,File folder, boolean clear) {\r
85                         super("Export folder");\r
86                         this.resource = resource;\r
87                         this.folder = folder;\r
88                         this.clear = clear;\r
89                 }\r
90                 @Override\r
91                 protected IStatus run(IProgressMonitor monitor) {\r
92                         try {\r
93                                 monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);\r
94                                 if (clear) {\r
95                                         GraphFileUtil.clearDirectoryStructure(folder);\r
96                                         monitor.worked(1);\r
97                                 }\r
98                                 FileDocumentUtil.exportDocumentFolder(resource, folder, relation,monitor);\r
99                                 monitor.done();\r
100                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");\r
101                         } catch (Exception e) {\r
102                                 monitor.done();\r
103                                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);\r
104                         }\r
105                 }\r
106         }\r
107 }\r