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
42 public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {
\r
43 relation = graph.getResource(relationUri);
\r
47 public Runnable create(Object target) {
\r
49 if(!(target instanceof Resource))
\r
52 final Resource resource = (Resource)target;
\r
54 return new Runnable() {
\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
63 File folder = new File(folderName);
\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
72 ExportJob job = new ExportJob(resource, folder, choice == 0);
\r
80 private class ExportJob extends DatabaseJob {
\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
91 protected IStatus run(IProgressMonitor monitor) {
\r
93 monitor.beginTask("Export folder", IProgressMonitor.UNKNOWN);
\r
95 GraphFileUtil.clearDirectoryStructure(folder);
\r
98 FileDocumentUtil.exportDocumentFolder(resource, folder, relation,monitor);
\r
100 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder exported.");
\r
101 } catch (Exception e) {
\r
103 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot export document folder.", e);
\r