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;fp=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Factions%2FImportDocument.java;h=ca50c8bb333f35b2c44a9017682334710a766868;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;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 new file mode 100644 index 000000000..ca50c8bb3 --- /dev/null +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * 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 org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +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.graphfile.FileDocumentUtil; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * 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); + // 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. + dialog.setFilterExtensions(new String[]{"*.*"}); + final String filename = dialog.open(); + if (filename == null) { + return; + } + Simantics.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + + Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename); + linkDocument(graph, resource, newDoc); + } + },new Callback() { + @Override + public void run(DatabaseException parameter) { + if (parameter != null) { + ExceptionUtils.logAndShowError("Cannot import document.", parameter); + } + + } + }); + } + }; + } + +}