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