]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / SimpleEditorAdapter.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/SimpleEditorAdapter.java
new file mode 100644 (file)
index 0000000..d6e9c84
--- /dev/null
@@ -0,0 +1,159 @@
+/*******************************************************************************\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.editor;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.workbench.ResourceEditorInput;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * A very simple and limited implementation of ResourceEditorAdapter.\r
+ * \r
+ * <p>\r
+ * Easy to use because all necessary information can be given as constructor\r
+ * parameters.\r
+ * </p>\r
+ * \r
+ * <p>\r
+ * Example:\r
+ * \r
+ * <pre>\r
+ * class MyEditorAdapter extends SimpleEditorAdapter {\r
+ *     MyEditorAdapter() {\r
+ *         super(&quot;My Editor&quot;, &quot;My.Editor.Id&quot;,\r
+ *                 new String[] { Builtins.URIs.Entity },\r
+ *                 new String[] { Builtins.URIs.Entity });\r
+ *     }\r
+ * }\r
+ * </pre>\r
+ * \r
+ * allows opening the editor "My.Editor.Id" for a resource if it is\r
+ * inherited from the type Object or is an instance of Object.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {\r
+\r
+    private final String   editorViewId;\r
+    private final String   perspectiveId;\r
+    private final String[] inherits;\r
+    private final String[] instanceOf;\r
+\r
+    /**\r
+     * @param name\r
+     * @param editorViewId\r
+     * @param inherits\r
+     * @param instanceOf\r
+     */\r
+    public SimpleEditorAdapter(String name, String editorViewId,\r
+            String[] inherits, String[] instanceOf)\r
+    {\r
+        this(name, (ImageDescriptor) null, editorViewId, inherits, instanceOf);\r
+    }\r
+\r
+    /**\r
+     * @param name\r
+     * @param icon\r
+     * @param editorViewId\r
+     * @param inherits\r
+     * @param instanceOf\r
+     */\r
+    public SimpleEditorAdapter(String name, ImageDescriptor icon,\r
+            String editorViewId,\r
+            String[] inherits, String[] instanceOf)\r
+    {\r
+        this(name, icon, editorViewId, null, inherits, instanceOf);\r
+    }\r
+\r
+    /**\r
+     * @param name\r
+     * @param icon\r
+     * @param editorViewId\r
+     * @param perspectiveId\r
+     * @param inherits\r
+     * @param instanceOf\r
+     */\r
+    public SimpleEditorAdapter(String name, ImageDescriptor icon,\r
+            String editorViewId, String perspectiveId,\r
+            String[] inherits, String[] instanceOf)\r
+    {\r
+        super(name, icon);\r
+        this.editorViewId = editorViewId;\r
+        this.perspectiveId = perspectiveId;\r
+        this.inherits = inherits;\r
+        this.instanceOf = instanceOf;\r
+    }\r
+\r
+    @Override\r
+    public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
+        //System.out.println("canHandle: " + this.getClass().getCanonicalName());\r
+\r
+        // If not filters are defined, allow anything.\r
+        if ((inherits == null || inherits.length == 0)\r
+                && (instanceOf == null || instanceOf.length == 0))\r
+            return true;\r
+\r
+        if (inherits != null) {\r
+            for (String type : inherits) {\r
+                //System.out.println("type: " + type);\r
+                try {\r
+                    if (g.isInheritedFrom(r, g.getResource(type)))\r
+                        return true;\r
+                } catch (DatabaseException e) {\r
+                    ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()\r
+                            + " checked for inheritance of '" + type + "' but resource was not found", e);\r
+                }\r
+            }\r
+        }\r
+        if (instanceOf != null) {\r
+            for (String instance : instanceOf) {\r
+                //System.out.println("instance: " + instance);\r
+                try {\r
+                    //System.out.println(instance);\r
+                    if (g.isInstanceOf(r, g.getResource(instance)))\r
+                        return true;\r
+                } catch (DatabaseException e) {\r
+                    ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()\r
+                            + " checked for instanceOf '" + instance + "' but the type was not found", e);\r
+                }\r
+            }\r
+        }\r
+        return false;\r
+    }\r
+\r
+\r
+    @Override\r
+    protected void openEditor(Resource r) throws Exception {\r
+        assert(editorViewId != null);\r
+\r
+        if (perspectiveId != null) {\r
+            openEditorWithIdInPerspective(editorViewId, new ResourceEditorInput(editorViewId, r), perspectiveId);\r
+        } else {\r
+            openEditorWithId(editorViewId, new ResourceEditorInput(editorViewId, r));\r
+        }\r
+    }\r
+\r
+    protected void openView(Resource r) throws Exception {\r
+        assert(editorViewId != null);\r
+\r
+        if (perspectiveId != null) {\r
+            openViewWithIdInPerspective(editorViewId, r, perspectiveId);\r
+        } else {\r
+            openViewWithId(editorViewId, r);\r
+        }\r
+    }\r
+\r
+}\r