1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.ui.actions;
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;
34 * Action for exporting file based documents.
36 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
39 public class ExportDocumentFolder implements ActionFactory {
41 boolean useResourceNames;
43 public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {
44 relation = graph.getResource(relationUri);
45 useResourceNames = true;
48 public ExportDocumentFolder(ReadGraph graph, String relationUri, String useResourceNames) throws DatabaseException {
49 relation = graph.getResource(relationUri);
50 this.useResourceNames = useResourceNames.equals("true");
54 public Runnable create(Object target) {
56 if(!(target instanceof Resource))
59 final Resource resource = (Resource)target;
61 return new Runnable() {
64 Shell shell = Display.getCurrent().getActiveShell();
65 DirectoryDialog dialog = new DirectoryDialog(shell,SWT.SAVE);
66 String folderName = dialog.open();
67 if (folderName == null) {
70 File folder = new File(folderName);
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();
79 ExportJob job = new ExportJob(resource, folder, choice == 0);
87 private class ExportJob extends DatabaseJob {
90 boolean clear = false;
91 public ExportJob(Resource resource,File folder, boolean clear) {
92 super("Export folder");
93 this.resource = resource;
98 protected IStatus run(IProgressMonitor monitor) {
100 monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);
102 GraphFileUtil.clearDirectoryStructure(folder);
105 FileDocumentUtil.exportDocumentFolder(resource, folder, relation, useResourceNames, monitor);
107 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");
108 } catch (Exception e) {
110 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);