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