X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Factions%2FImportDocument.java;h=52eb042ac7e6e27e01176996bb7175bdc37c0dfc;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=7a9595263494f2cc192fbc7f293b2dea8f962fbc;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java index 7a9595263..52eb042ac 100644 --- a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java @@ -1,114 +1,114 @@ -/******************************************************************************* - * 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 - * - */ -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(); - } - } - } - -} +/******************************************************************************* + * 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 + * + */ +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(); + } + } + } + +}