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.action.IAction;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.WorkbenchException;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.ui.SimanticsUI;
22 import org.simantics.ui.utils.ResourceAdaptionUtils;
23 import org.simantics.ui.workbench.ResourceViewPartUtils;
24 import org.simantics.ui.workbench.action.ResourceEditorAdapterAction;
25 import org.simantics.utils.ui.ErrorLogger;
26 import org.simantics.utils.ui.action.IPriorityAction;
27 import org.simantics.utils.ui.action.PriorityActionAdapter;
28 import org.simantics.utils.ui.workbench.WorkbenchUtils;
31 * A base implementation for {@link EditorAdapter} that adapts
32 * {@link #canHandle(ReadGraph, Object)} into
33 * {@link #canHandle(ReadGraph, Resource)} and {@link #openEditor(Object)} into
34 * {@link #openEditor(Resource)}.
36 * If you only plan to support {@link Resource} or {@link Resource}-adaptable
37 * editor inputs, this class is a bit easier to use than
38 * {@link AbstractEditorAdapter}.
40 * @author Tuukka Lehtonen
42 public abstract class AbstractResourceEditorAdapter extends AbstractEditorAdapter {
44 public AbstractResourceEditorAdapter(String name) {
48 public AbstractResourceEditorAdapter(String name, ImageDescriptor icon) {
52 public AbstractResourceEditorAdapter(String name, ImageDescriptor icon, int priority) {
53 super(name, icon, priority);
57 * Override to perform the can handle check based on a single resource
58 * adapted from the Object input using default mechanisms provided by
59 * {@link ResourceAdaptionUtils}.
61 * Returns <code>false</code> by default.
66 * @throws DatabaseException
68 protected boolean canHandle(ReadGraph g, Resource input) throws DatabaseException {
73 * Override to open an editor based on a single resource adapted from the
74 * Object input using default mechanisms provided by
75 * {@link ResourceAdaptionUtils}.
80 protected void openEditor(Resource input) throws Exception {
84 public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
85 Resource r = ResourceAdaptionUtils.toSingleResource(input);
88 return canHandle(g, r);
92 public void openEditor(Object input) throws Exception {
93 Resource r = ResourceAdaptionUtils.toSingleResource(input);
98 protected void openViewWithId(String viewId, Resource id, String suffix) throws Exception {
99 Session ls = SimanticsUI.getSession();
100 ResourceViewPartUtils.activateViewForResource(viewId, ls, id, null);
103 protected void openViewWithId(String viewId, Resource id) throws Exception {
104 openViewWithId(viewId, id, null);
107 protected void openViewWithIdInPerspective(String viewId, Resource id, String perspectiveId) throws Exception {
109 WorkbenchUtils.showPerspective(perspectiveId);
110 } catch (WorkbenchException e) {
111 ErrorLogger.getDefault().logError("Could not open perspective with ID'" + perspectiveId + "'.", e);
114 openViewWithId(viewId, id);
117 public IAction toAction(Resource r) {
118 return new ResourceEditorAdapterAction(this, r);
121 public IPriorityAction toPriorityAction(int priority, Resource r) {
122 return new PriorityActionAdapter(priority, toAction(r));