]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / ResourceEditorInputFactory.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/ResourceEditorInputFactory.java
new file mode 100644 (file)
index 0000000..2c1d383
--- /dev/null
@@ -0,0 +1,100 @@
+/*******************************************************************************\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.ui.workbench;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+import org.eclipse.ui.IElementFactory;\r
+import org.eclipse.ui.IMemento;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.workbench.StringMemento;\r
+\r
+\r
+/**\r
+ * Factory for restoring a <code>ResourceEditorInput</code>.\r
+ * The stored representation of a <code>ResourceEditorInput</code> remembers\r
+ * a reference to the input resource, which is a string ID.\r
+ * \r
+ * <p>\r
+ * The workbench will automatically create instances of this class as required.\r
+ * It is not intended to be instantiated or subclassed by the client.\r
+ * </p>\r
+ */\r
+public class ResourceEditorInputFactory 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.ui.workbench.resourceEditorInputFactory"; //$NON-NLS-1$\r
+\r
+    /**\r
+     */\r
+    public static final String TAG_RESOURCE_ID = "resourceId"; //$NON-NLS-1$\r
+\r
+    /**\r
+     */\r
+    public static final String TAG_EDITOR_ID = "editorId"; //$NON-NLS-1$\r
+\r
+    /** Tag ID for node that contains external data */\r
+    public static final String TAG_EXTERNAL_MEMENTO_ID = "external"; //$NON-NLS-1$\r
+\r
+    /**\r
+     * Creates a new factory.\r
+     */\r
+    public ResourceEditorInputFactory() {\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * Method declared on IElementFactory.\r
+     */\r
+    public IAdaptable createElement(IMemento memento) {\r
+        // Get the resource id array.\r
+        IMemento[] children = memento.getChildren(TAG_RESOURCE_ID);\r
+        if (children.length == 0)\r
+            return null;\r
+\r
+        List<String> ids = new ArrayList<String>();\r
+        for (IMemento child : children)\r
+            ids.add(child.getTextData());\r
+\r
+        String editorId = memento.getString(TAG_EDITOR_ID);\r
+\r
+        try {\r
+            ResourceEditorInput result = new ResourceEditorInput(editorId, ids);\r
+\r
+            // Read external persistent memento\r
+            String externalMementoStr = memento.getString(TAG_EXTERNAL_MEMENTO_ID);\r
+            if (externalMementoStr != null) {\r
+                StringMemento sm = new StringMemento(externalMementoStr);\r
+                sm.writeToMemento(result.getPersistentStore());\r
+            }\r
+            return result;\r
+        } catch (NumberFormatException e) {\r
+            return null;\r
+        } catch (IllegalArgumentException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+            return null;\r
+        }\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