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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.actions;
\r
14 import java.io.File;
\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
34 * Action for exporting file based documents.
\r
36 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
39 public class ExportDocumentFolder implements ActionFactory {
\r
41 boolean useResourceNames;
\r
43 public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {
\r
44 relation = graph.getResource(relationUri);
\r
45 useResourceNames = true;
\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
54 public Runnable create(Object target) {
\r
56 if(!(target instanceof Resource))
\r
59 final Resource resource = (Resource)target;
\r
61 return new Runnable() {
\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
70 File folder = new File(folderName);
\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
79 ExportJob job = new ExportJob(resource, folder, choice == 0);
\r
87 private class ExportJob extends DatabaseJob {
\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
98 protected IStatus run(IProgressMonitor monitor) {
\r
100 monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);
\r
102 GraphFileUtil.clearDirectoryStructure(folder);
\r
105 FileDocumentUtil.exportDocumentFolder(resource, folder, relation, useResourceNames, monitor);
\r
107 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");
\r
108 } catch (Exception e) {
\r
110 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);
\r