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;
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.swt.SWT;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.FileDialog;
22 import org.simantics.DatabaseJob;
23 import org.simantics.Simantics;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.WriteRequest;
28 import org.simantics.db.exception.DatabaseException;
31 * Action for importing files as documents.
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
36 public class ImportDocument extends AddDocumentAction {
39 public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {
40 super(graph, relationUri);
44 public Runnable create(Object target) {
45 if(!(target instanceof Resource))
47 final Resource resource = (Resource)target;
48 return new Runnable() {
52 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN | SWT.MULTI);
53 // TODO : is there any way to read file/executable bindings from OS?
54 // if is, use those extensions to filter this list.
55 // note: in windows using "reg query ..." to read bindings form registry would work.
56 // Note : If the above mentioned filtering is implemented it should be made optional / configurable.
57 dialog.setFilterExtensions(new String[]{"*.*"});
58 if (dialog.open() == null) return;
60 String filterPath = dialog.getFilterPath();
61 String[] filenames = dialog.getFileNames();
63 ImportJob job = new ImportJob(filenames.length > 1 ? "Import files" : "Import file", resource, filterPath, filenames);
70 private class ImportJob extends DatabaseJob {
72 public ImportJob(String name, Resource resource, String path, String[] filenames) {
74 this.resource = resource;
76 this.filenames = filenames;
84 protected IStatus run(final IProgressMonitor monitor) {
85 monitor.beginTask("Importing...", filenames.length);
87 Simantics.getSession().syncRequest(new WriteRequest() {
89 public void perform(WriteGraph graph) throws DatabaseException {
91 graph.markUndoPoint();
92 for (String filename : filenames) {
93 File f = new File(path, filename);
94 Resource newDoc = FileDocumentUtil.importFileWithName(graph, f.getAbsolutePath());
95 linkDocument(graph, resource, newDoc);
98 } catch (Exception e) {
99 throw new DatabaseException(e);
103 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Import succesful.");
104 } catch (DatabaseException e) {
105 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed.", e);