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.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;
36 * Action for exporting file based documents.
38 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
41 public class ExportDocumentFolder implements ActionFactory {
43 boolean useResourceNames;
45 public ExportDocumentFolder(ReadGraph graph, String relationUri) throws DatabaseException {
46 relation = graph.getResource(relationUri);
47 useResourceNames = true;
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$
56 public Runnable create(Object target) {
58 if(!(target instanceof Resource))
61 final Resource resource = (Resource)target;
63 return new Runnable() {
66 Shell shell = Display.getCurrent().getActiveShell();
67 DirectoryDialog dialog = new DirectoryDialog(shell,SWT.SAVE);
68 String folderName = dialog.open();
69 if (folderName == null) {
72 File folder = new File(folderName);
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();
81 ExportJob job = new ExportJob(resource, folder, choice == 0);
89 private class ExportJob extends DatabaseJob {
92 boolean clear = false;
93 public ExportJob(Resource resource,File folder, boolean clear) {
94 super(Messages.ExportDocumentFolder_ExportFolder);
95 this.resource = resource;
100 protected IStatus run(IProgressMonitor monitor) {
102 monitor.beginTask(Messages.ExportDocumentFolder_ExportFolder, IProgressMonitor.UNKNOWN);
104 GraphFileUtil.clearDirectoryStructure(folder);
107 FileDocumentUtil.exportDocumentFolder(resource, folder, relation, useResourceNames, monitor);
109 return new Status(IStatus.OK, Activator.PLUGIN_ID, Messages.ExportDocumentFolder_ActivatorFolderExported);
110 } catch (Exception e) {
112 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ExportDocumentFolder_AcivatorCannotExport, e);