-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.document.ui.actions;\r
-\r
-import java.io.File;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.FileDialog;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.document.ui.Activator;\r
-import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
-\r
-/**\r
- * Action for importing files as documents.\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- *\r
- */\r
-public class ImportDocument extends AddDocumentAction {\r
- \r
-\r
- public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {\r
- super(graph, relationUri);\r
- }\r
-\r
- @Override\r
- public Runnable create(Object target) {\r
- if(!(target instanceof Resource))\r
- return null;\r
- final Resource resource = (Resource)target;\r
- return new Runnable() {\r
- \r
- @Override\r
- public void run() {\r
- FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN | SWT.MULTI);\r
- // TODO : is there any way to read file/executable bindings from OS?\r
- // if is, use those extensions to filter this list.\r
- // note: in windows using "reg query ..." to read bindings form registry would work.\r
- // Note : If the above mentioned filtering is implemented it should be made optional / configurable.\r
- dialog.setFilterExtensions(new String[]{"*.*"});\r
- if (dialog.open() == null) return;\r
-\r
- String filterPath = dialog.getFilterPath();\r
- String[] filenames = dialog.getFileNames();\r
- \r
- ImportJob job = new ImportJob(filenames.length > 1 ? "Import files" : "Import file", resource, filterPath, filenames);\r
- job.setUser(true);\r
- job.schedule();\r
- }\r
- };\r
- }\r
- \r
- private class ImportJob extends DatabaseJob {\r
-\r
- public ImportJob(String name, Resource resource, String path, String[] filenames) {\r
- super(name);\r
- this.resource = resource;\r
- this.path = path;\r
- this.filenames = filenames;\r
- }\r
-\r
- Resource resource;\r
- String path;\r
- String[] filenames;\r
-\r
- @Override\r
- protected IStatus run(final IProgressMonitor monitor) {\r
- monitor.beginTask("Importing...", filenames.length);\r
- try {\r
- Simantics.getSession().syncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- try {\r
- graph.markUndoPoint();\r
- for (String filename : filenames) {\r
- File f = new File(path, filename);\r
- Resource newDoc = FileDocumentUtil.importFileWithName(graph, f.getAbsolutePath());\r
- linkDocument(graph, resource, newDoc);\r
- monitor.worked(1);\r
- }\r
- } catch (Exception e) {\r
- throw new DatabaseException(e);\r
- }\r
- }\r
- });\r
- return new Status(IStatus.OK, Activator.PLUGIN_ID, "Import succesful.");\r
- } catch (DatabaseException e) {\r
- return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed.", e);\r
- } finally {\r
- monitor.done();\r
- }\r
- }\r
- }\r
- \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.document.ui.actions;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.FileDialog;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.document.ui.Activator;
+import org.simantics.document.ui.graphfile.FileDocumentUtil;
+
+/**
+ * Action for importing files as documents.
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ *
+ */
+public class ImportDocument extends AddDocumentAction {
+
+
+ public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {
+ super(graph, relationUri);
+ }
+
+ @Override
+ public Runnable create(Object target) {
+ if(!(target instanceof Resource))
+ return null;
+ final Resource resource = (Resource)target;
+ return new Runnable() {
+
+ @Override
+ public void run() {
+ FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN | SWT.MULTI);
+ // TODO : is there any way to read file/executable bindings from OS?
+ // if is, use those extensions to filter this list.
+ // note: in windows using "reg query ..." to read bindings form registry would work.
+ // Note : If the above mentioned filtering is implemented it should be made optional / configurable.
+ dialog.setFilterExtensions(new String[]{"*.*"});
+ if (dialog.open() == null) return;
+
+ String filterPath = dialog.getFilterPath();
+ String[] filenames = dialog.getFileNames();
+
+ ImportJob job = new ImportJob(filenames.length > 1 ? "Import files" : "Import file", resource, filterPath, filenames);
+ job.setUser(true);
+ job.schedule();
+ }
+ };
+ }
+
+ private class ImportJob extends DatabaseJob {
+
+ public ImportJob(String name, Resource resource, String path, String[] filenames) {
+ super(name);
+ this.resource = resource;
+ this.path = path;
+ this.filenames = filenames;
+ }
+
+ Resource resource;
+ String path;
+ String[] filenames;
+
+ @Override
+ protected IStatus run(final IProgressMonitor monitor) {
+ monitor.beginTask("Importing...", filenames.length);
+ try {
+ Simantics.getSession().syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ try {
+ graph.markUndoPoint();
+ for (String filename : filenames) {
+ File f = new File(path, filename);
+ Resource newDoc = FileDocumentUtil.importFileWithName(graph, f.getAbsolutePath());
+ linkDocument(graph, resource, newDoc);
+ monitor.worked(1);
+ }
+ } catch (Exception e) {
+ throw new DatabaseException(e);
+ }
+ }
+ });
+ return new Status(IStatus.OK, Activator.PLUGIN_ID, "Import succesful.");
+ } catch (DatabaseException e) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed.", e);
+ } finally {
+ monitor.done();
+ }
+ }
+ }
+
+}