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