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