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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.workbench.editor;
\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
22 * A very simple and limited implementation of ResourceEditorAdapter.
\r
25 * Easy to use because all necessary information can be given as constructor
\r
33 * class MyEditorAdapter extends SimpleEditorAdapter {
\r
34 * MyEditorAdapter() {
\r
35 * super("My Editor", "My.Editor.Id",
\r
36 * new String[] { Builtins.URIs.Entity },
\r
37 * new String[] { Builtins.URIs.Entity });
\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
46 * @author Tuukka Lehtonen
\r
48 public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {
\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
57 * @param editorViewId
\r
61 public SimpleEditorAdapter(String name, String editorViewId,
\r
62 String[] inherits, String[] instanceOf)
\r
64 this(name, (ImageDescriptor) null, editorViewId, inherits, instanceOf);
\r
70 * @param editorViewId
\r
74 public SimpleEditorAdapter(String name, ImageDescriptor icon,
\r
75 String editorViewId,
\r
76 String[] inherits, String[] instanceOf)
\r
78 this(name, icon, editorViewId, null, inherits, instanceOf);
\r
84 * @param editorViewId
\r
85 * @param perspectiveId
\r
89 public SimpleEditorAdapter(String name, ImageDescriptor icon,
\r
90 String editorViewId, String perspectiveId,
\r
91 String[] inherits, String[] instanceOf)
\r
94 this.editorViewId = editorViewId;
\r
95 this.perspectiveId = perspectiveId;
\r
96 this.inherits = inherits;
\r
97 this.instanceOf = instanceOf;
\r
101 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
\r
102 //System.out.println("canHandle: " + this.getClass().getCanonicalName());
\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
109 if (inherits != null) {
\r
110 for (String type : inherits) {
\r
111 //System.out.println("type: " + type);
\r
113 if (g.isInheritedFrom(r, g.getResource(type)))
\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
121 if (instanceOf != null) {
\r
122 for (String instance : instanceOf) {
\r
123 //System.out.println("instance: " + instance);
\r
125 //System.out.println(instance);
\r
126 if (g.isInstanceOf(r, g.getResource(instance)))
\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
139 protected void openEditor(Resource r) throws Exception {
\r
140 assert(editorViewId != null);
\r
142 if (perspectiveId != null) {
\r
143 openEditorWithIdInPerspective(editorViewId, new ResourceEditorInput(editorViewId, r), perspectiveId);
\r
145 openEditorWithId(editorViewId, new ResourceEditorInput(editorViewId, r));
\r
149 protected void openView(Resource r) throws Exception {
\r
150 assert(editorViewId != null);
\r
152 if (perspectiveId != null) {
\r
153 openViewWithIdInPerspective(editorViewId, r, perspectiveId);
\r
155 openViewWithId(editorViewId, r);
\r