1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.workbench.editor;
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;
22 * A very simple and limited implementation of ResourceEditorAdapter.
25 * Easy to use because all necessary information can be given as constructor
33 * class MyEditorAdapter extends SimpleEditorAdapter {
35 * super("My Editor", "My.Editor.Id",
36 * new String[] { Builtins.URIs.Entity },
37 * new String[] { Builtins.URIs.Entity });
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.
46 * @author Tuukka Lehtonen
48 public class SimpleEditorAdapter extends AbstractResourceEditorAdapter {
50 private final String editorViewId;
51 private final String perspectiveId;
52 private final String[] inherits;
53 private final String[] instanceOf;
61 public SimpleEditorAdapter(String name, String editorViewId,
62 String[] inherits, String[] instanceOf)
64 this(name, (ImageDescriptor) null, editorViewId, inherits, instanceOf);
74 public SimpleEditorAdapter(String name, ImageDescriptor icon,
76 String[] inherits, String[] instanceOf)
78 this(name, icon, editorViewId, null, inherits, instanceOf);
85 * @param perspectiveId
89 public SimpleEditorAdapter(String name, ImageDescriptor icon,
90 String editorViewId, String perspectiveId,
91 String[] inherits, String[] instanceOf)
94 this.editorViewId = editorViewId;
95 this.perspectiveId = perspectiveId;
96 this.inherits = inherits;
97 this.instanceOf = instanceOf;
101 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
102 //System.out.println("canHandle: " + this.getClass().getCanonicalName());
104 // If not filters are defined, allow anything.
105 if ((inherits == null || inherits.length == 0)
106 && (instanceOf == null || instanceOf.length == 0))
109 if (inherits != null) {
110 for (String type : inherits) {
111 //System.out.println("type: " + type);
113 if (g.isInheritedFrom(r, g.getResource(type)))
115 } catch (DatabaseException e) {
116 ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()
117 + " checked for inheritance of '" + type + "' but resource was not found", e);
121 if (instanceOf != null) {
122 for (String instance : instanceOf) {
123 //System.out.println("instance: " + instance);
125 //System.out.println(instance);
126 if (g.isInstanceOf(r, g.getResource(instance)))
128 } catch (DatabaseException e) {
129 ErrorLogger.defaultLogError("BUG: SimpleEditorAdapter " + getClass().getName()
130 + " checked for instanceOf '" + instance + "' but the type was not found", e);
139 protected void openEditor(Resource r) throws Exception {
140 assert(editorViewId != null);
142 if (perspectiveId != null) {
143 openEditorWithIdInPerspective(editorViewId, new ResourceEditorInput(editorViewId, r), perspectiveId);
145 openEditorWithId(editorViewId, new ResourceEditorInput(editorViewId, r));
149 protected void openView(Resource r) throws Exception {
150 assert(editorViewId != null);
152 if (perspectiveId != null) {
153 openViewWithIdInPerspective(editorViewId, r, perspectiveId);
155 openViewWithId(editorViewId, r);