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