]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/GraphFileEditorInputFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / hack / GraphFileEditorInputFactory.java
diff --git a/bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/GraphFileEditorInputFactory.java b/bundles/org.simantics.graphfile/src/org/simantics/graphfile/hack/GraphFileEditorInputFactory.java
new file mode 100644 (file)
index 0000000..9f0a050
--- /dev/null
@@ -0,0 +1,111 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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.graphfile.hack;\r
+\r
+import org.eclipse.core.resources.IWorkspace;\r
+import org.eclipse.core.resources.ResourcesPlugin;\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.ui.IElementFactory;\r
+import org.eclipse.ui.IMemento;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.service.SerialisationSupport;\r
+\r
+/**\r
+ * FileEditorInputFactory for GraphFiles. Handles loading and saving GraphFile references.\r
+ * \r
+ * TODO : uses resource id to reference resources. That is unreliable.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public class GraphFileEditorInputFactory implements IElementFactory{\r
+        /**\r
+     * Factory id. The workbench plug-in registers a factory by this name\r
+     * with the "org.eclipse.ui.elementFactories" extension point.\r
+     */\r
+    private static final String ID_FACTORY = "org.simantics.graphfile.hack.GraphFileEditorInputFactory"; //$NON-NLS-1$\r
+\r
+    /**\r
+     * Tag for the IFile.fullPath of the file resource.\r
+     */\r
+    private static final String TAG_RESOURCE_ID = "id"; //$NON-NLS-1$\r
+\r
+    /**\r
+     * Creates a new factory.\r
+     */\r
+    public GraphFileEditorInputFactory() {\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on IElementFactory.\r
+     */\r
+    public IAdaptable createElement(IMemento memento) {\r
+        // Get the file name.\r
+        String sid = memento.getString(TAG_RESOURCE_ID);\r
+        if (sid == null) {\r
+                       return null;\r
+               }\r
+        final long id = Long.parseLong(sid);\r
+        \r
+        Resource fileResource = null;\r
+        try {\r
+               fileResource = Simantics.getSession().syncRequest(new Read<Resource>() {\r
+                       @Override\r
+                       public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                                 SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
+                             try {\r
+                                 Resource r = support.getResource(id);\r
+                                 return r;\r
+                             } catch (Exception e) {\r
+                                 return null;\r
+                             }\r
+                                 \r
+                       }\r
+               });\r
+        } catch (DatabaseException e) {\r
+               return null;\r
+        }\r
+        \r
+        if (fileResource == null)\r
+               return null;\r
+\r
+        // Get a handle to the IFile...which can be a handle\r
+        // to a resource that does not exist in workspace\r
+        IWorkspace ws = (IWorkspace) ResourcesPlugin.getWorkspace();\r
+        GraphFile file = new GraphFile(fileResource, ws);\r
+        return new GraphFileEditorInput(file);\r
+    }\r
+\r
+    /**\r
+     * Returns the element factory id for this class.\r
+     * \r
+     * @return the element factory id\r
+     */\r
+    public static String getFactoryId() {\r
+        return ID_FACTORY;\r
+    }\r
+\r
+    /**\r
+     * Saves the state of the given file editor input into the given memento.\r
+     *\r
+     * @param memento the storage area for element state\r
+     * @param input the file editor input\r
+     */\r
+    public static void saveState(IMemento memento, GraphFileEditorInput input) {\r
+        GraphFile file = (GraphFile)input.getFile();\r
+        memento.putString(TAG_RESOURCE_ID, Long.toString(file.getFileResource().getResourceId()));\r
+    }\r
+}\r