]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.workbench.editor;\r
13 \r
14 import org.eclipse.jface.resource.ImageDescriptor;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.ui.workbench.ResourceEditorInput;\r
19 import org.simantics.utils.ui.ErrorLogger;\r
20 \r
21 /**\r
22  * A very simple and limited implementation of ResourceEditorAdapter.\r
23  * \r
24  * <p>\r
25  * Easy to use because all necessary information can be given as constructor\r
26  * parameters.\r
27  * </p>\r
28  * \r
29  * <p>\r
30  * Example:\r
31  * \r
32  * <pre>\r
33  * class MyEditorAdapter extends SimpleEditorAdapter {\r
34  *     MyEditorAdapter() {\r
35  *         super(&quot;My Editor&quot;, &quot;My.Editor.Id&quot;,\r
36  *                 new String[] { Builtins.URIs.Entity },\r
37  *                 new String[] { Builtins.URIs.Entity });\r
38  *     }\r
39  * }\r
40  * </pre>\r
41  * \r
42  * allows opening the editor "My.Editor.Id" for a resource if it is\r
43  * inherited from the type Object or is an instance of Object.\r
44  * </p>\r
45  * \r
46  * @author Tuukka Lehtonen\r
47  */\r
48 public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {\r
49 \r
50     private final String   editorViewId;\r
51     private final String   perspectiveId;\r
52     private final String[] inherits;\r
53     private final String[] instanceOf;\r
54 \r
55     /**\r
56      * @param name\r
57      * @param editorViewId\r
58      * @param inherits\r
59      * @param instanceOf\r
60      */\r
61     public SimpleEditorAdapter(String name, String editorViewId,\r
62             String[] inherits, String[] instanceOf)\r
63     {\r
64         this(name, (ImageDescriptor) null, editorViewId, inherits, instanceOf);\r
65     }\r
66 \r
67     /**\r
68      * @param name\r
69      * @param icon\r
70      * @param editorViewId\r
71      * @param inherits\r
72      * @param instanceOf\r
73      */\r
74     public SimpleEditorAdapter(String name, ImageDescriptor icon,\r
75             String editorViewId,\r
76             String[] inherits, String[] instanceOf)\r
77     {\r
78         this(name, icon, editorViewId, null, inherits, instanceOf);\r
79     }\r
80 \r
81     /**\r
82      * @param name\r
83      * @param icon\r
84      * @param editorViewId\r
85      * @param perspectiveId\r
86      * @param inherits\r
87      * @param instanceOf\r
88      */\r
89     public SimpleEditorAdapter(String name, ImageDescriptor icon,\r
90             String editorViewId, String perspectiveId,\r
91             String[] inherits, String[] instanceOf)\r
92     {\r
93         super(name, icon);\r
94         this.editorViewId = editorViewId;\r
95         this.perspectiveId = perspectiveId;\r
96         this.inherits = inherits;\r
97         this.instanceOf = instanceOf;\r
98     }\r
99 \r
100     @Override\r
101     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {\r
102         //System.out.println("canHandle: " + this.getClass().getCanonicalName());\r
103 \r
104         // If not filters are defined, allow anything.\r
105         if ((inherits == null || inherits.length == 0)\r
106                 && (instanceOf == null || instanceOf.length == 0))\r
107             return true;\r
108 \r
109         if (inherits != null) {\r
110             for (String type : inherits) {\r
111                 //System.out.println("type: " + type);\r
112                 try {\r
113                     if (g.isInheritedFrom(r, g.getResource(type)))\r
114                         return true;\r
115                 } catch (DatabaseException e) {\r
116                     ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()\r
117                             + " checked for inheritance of '" + type + "' but resource was not found", e);\r
118                 }\r
119             }\r
120         }\r
121         if (instanceOf != null) {\r
122             for (String instance : instanceOf) {\r
123                 //System.out.println("instance: " + instance);\r
124                 try {\r
125                     //System.out.println(instance);\r
126                     if (g.isInstanceOf(r, g.getResource(instance)))\r
127                         return true;\r
128                 } catch (DatabaseException e) {\r
129                     ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()\r
130                             + " checked for instanceOf '" + instance + "' but the type was not found", e);\r
131                 }\r
132             }\r
133         }\r
134         return false;\r
135     }\r
136 \r
137 \r
138     @Override\r
139     protected void openEditor(Resource r) throws Exception {\r
140         assert(editorViewId != null);\r
141 \r
142         if (perspectiveId != null) {\r
143             openEditorWithIdInPerspective(editorViewId, new ResourceEditorInput(editorViewId, r), perspectiveId);\r
144         } else {\r
145             openEditorWithId(editorViewId, new ResourceEditorInput(editorViewId, r));\r
146         }\r
147     }\r
148 \r
149     protected void openView(Resource r) throws Exception {\r
150         assert(editorViewId != null);\r
151 \r
152         if (perspectiveId != null) {\r
153             openViewWithIdInPerspective(editorViewId, r, perspectiveId);\r
154         } else {\r
155             openViewWithId(editorViewId, r);\r
156         }\r
157     }\r
158 \r
159 }\r