]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/AbstractResourceEditorAdapter.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / AbstractResourceEditorAdapter.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.action.IAction;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.WorkbenchException;
17 import org.simantics.Simantics;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
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;
29
30 /**
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)}.
35  * 
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}.
39  * 
40  * @author Tuukka Lehtonen
41  */
42 public abstract class AbstractResourceEditorAdapter extends AbstractEditorAdapter {
43
44     public AbstractResourceEditorAdapter(String name) {
45         super(name, null, 0);
46     }
47
48     public AbstractResourceEditorAdapter(String name, ImageDescriptor icon) {
49         super(name, icon, 0);
50     }
51
52     public AbstractResourceEditorAdapter(String name, ImageDescriptor icon, int priority) {
53         super(name, icon, priority);
54     }
55
56     /**
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}.
60      * 
61      * Returns <code>false</code> by default.
62      * 
63      * @param g
64      * @param input
65      * @return
66      * @throws DatabaseException
67      */
68     protected boolean canHandle(ReadGraph g, Resource input) throws DatabaseException {
69         return false;
70     }
71
72     /**
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}.
76      * 
77      * @param input
78      * @throws Exception
79      */
80     protected void openEditor(Resource input) throws Exception {
81     }
82
83     @Override
84     public boolean canHandle(ReadGraph g, Object input) throws DatabaseException {
85         Resource r = ResourceAdaptionUtils.toSingleResource(input);
86         if (r == null)
87             return false;
88         return canHandle(g, r);
89     }
90
91     @Override
92     public void openEditor(Object input) throws Exception {
93         Resource r = ResourceAdaptionUtils.toSingleResource(input);
94         if (r != null)
95             openEditor(r);
96     }
97
98     protected void openViewWithId(String viewId, Resource id, String suffix) throws Exception {
99         Session ls = Simantics.getSession();
100         ResourceViewPartUtils.activateViewForResource(viewId, ls, id, null);
101     }
102
103     protected void openViewWithId(String viewId, Resource id) throws Exception {
104         openViewWithId(viewId, id, null);
105     }
106
107     protected void openViewWithIdInPerspective(String viewId, Resource id, String perspectiveId) throws Exception {
108         try {
109             WorkbenchUtils.showPerspective(perspectiveId);
110         } catch (WorkbenchException e) {
111             ErrorLogger.getDefault().logError("Could not open perspective with ID'" + perspectiveId + "'.", e);
112         }
113
114         openViewWithId(viewId, id);
115     }
116
117     public IAction toAction(Resource r) {
118         return new ResourceEditorAdapterAction(this, r);
119     }
120
121     public IPriorityAction toPriorityAction(int priority, Resource r) {
122         return new PriorityActionAdapter(priority, toAction(r));
123     }
124
125 }