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;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.DirectoryDialog;
19 import org.eclipse.swt.widgets.Display;
20 import org.simantics.DatabaseJob;
21 import org.simantics.Simantics;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.ActionFactory;
28 import org.simantics.document.FileDocumentUtil;
29 import org.simantics.document.ui.Activator;
32 * Action for importing files as documents.
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class ImportDocumentFolder implements ActionFactory {
41 public ImportDocumentFolder(ReadGraph graph, String folderTypeUri, String relationUri) throws DatabaseException {
42 folderType = graph.getResource(folderTypeUri);
43 relation = graph.getResource(relationUri);
47 public Runnable create(Object target) {
48 if(!(target instanceof Resource))
50 final Resource resource = (Resource)target;
51 return new Runnable() {
55 DirectoryDialog dialog = new DirectoryDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
56 final String filename = dialog.open();
57 if (filename == null) {
61 ImportJob job = new ImportJob(Messages.ImportDocumentFolder_ImportFolder, resource, filename);
69 private class ImportJob extends DatabaseJob {
71 public ImportJob(String name, Resource resource, String filename) {
73 this.resource = resource;
74 this.filename = filename;
81 protected IStatus run(final IProgressMonitor monitor) {
83 Simantics.getSession().syncRequest(new WriteRequest() {
85 public void perform(WriteGraph graph) throws DatabaseException {
87 graph.markUndoPoint();
88 FileDocumentUtil.importFolderWithName(graph,filename,resource,folderType,relation,monitor);
89 } catch (Exception e) {
90 throw new DatabaseException(e);
94 return new Status(IStatus.OK, Activator.PLUGIN_ID, Messages.ImportDocumentFolder_ActivatorFolderImported);
95 } catch (DatabaseException e) {
96 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.ImportDocumentFolder_ActivatorCannotImportDocumentFolder, e);