--- /dev/null
+/*******************************************************************************\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
+import java.io.FileNotFoundException;\r
+\r
+import org.eclipse.swt.widgets.Display;\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.dialogs.FileDetailDialog;\r
+import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+/**\r
+ * Action for importing files as document.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class ImportDocumentWithDetail extends AddDocumentAction {\r
+ \r
+\r
+ public ImportDocumentWithDetail(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
+ final FileDetailDialog dialog = new FileDetailDialog(Display.getCurrent().getActiveShell(), resource);\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
+ if (dialog.open() == FileDetailDialog.CANCEL) {\r
+ dialog.getAnnotationConfigurator().dispose();\r
+ return;\r
+ }\r
+ final String filename = dialog.getFileName();\r
+ final String name = dialog.getName();\r
+ if (filename == null) {\r
+ dialog.getAnnotationConfigurator().dispose();\r
+ return;\r
+ }\r
+ Simantics.getSession().asyncRequest(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ graph.markUndoPoint();\r
+ \r
+ Resource newDoc = doDocumentImport(graph, resource, filename, name);\r
+ dialog.getAnnotationConfigurator().apply(graph,newDoc);\r
+ }\r
+ },new Callback<DatabaseException>() { \r
+ @Override\r
+ public void run(DatabaseException parameter) {\r
+ dialog.getAnnotationConfigurator().dispose();\r
+ if (parameter != null) {\r
+ ExceptionUtils.logAndShowError("Cannot import document.", parameter);\r
+ }\r
+ \r
+ }\r
+ });\r
+ }\r
+ };\r
+ }\r
+ \r
+ public Resource doDocumentImport(WriteGraph graph, Resource target, String filename, String name) throws DatabaseException {\r
+ Layer0 l0 = Layer0.getInstance(graph);\r
+ Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);\r
+ graph.claimLiteral(newDoc, l0.HasName, name);\r
+ linkDocument(graph, target, newDoc);\r
+ return newDoc;\r
+ }\r
+ \r
+ public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {\r
+ File file = new File(filename);\r
+ if (!file.exists())\r
+ throw new FileNotFoundException("File not found - " + file.getAbsolutePath());\r
+ ImportDocumentWithDetail document = new ImportDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");\r
+ return document.doDocumentImport(graph, target, filename, file.getName());\r
+ }\r
+\r
+}\r