1 package org.simantics.ui.workbench.editor;
3 import java.util.Collection;
5 import org.eclipse.jface.resource.ImageDescriptor;
6 import org.eclipse.ui.PartInitException;
7 import org.eclipse.ui.PlatformUI;
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.PossibleIndexRoot;
12 import org.simantics.db.common.request.ReadRequest;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.RVI;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.db.layer0.variable.Variables;
17 import org.simantics.modeling.ModelingResources;
18 import org.simantics.ui.utils.ResourceAdaptionUtils;
19 import org.simantics.ui.workbench.ResourceEditorInput2;
20 import org.simantics.utils.ui.workbench.WorkbenchUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class GraphEditorAdapterDescriptor implements EditorAdapterDescriptor {
25 private static final Logger LOGGER = LoggerFactory.getLogger(GraphEditorAdapterDescriptor.class);
27 private final String editorId;
28 private final String label;
29 private final ImageDescriptor imageDescriptor;
30 private final Resource contribution;
31 private final int priority;
33 public GraphEditorAdapterDescriptor(String editorId, String label, ImageDescriptor imageDescriptor, Resource contribution, int priority) {
34 this.editorId = editorId;
36 this.imageDescriptor = imageDescriptor;
37 this.contribution = contribution;
38 this.priority = priority;
42 public String getId() {
47 public String getGroupId() {
52 public EditorAdapter getAdapter() {
54 return new AbstractResourceEditorAdapter(editorId, null, priority) {
57 public String getName() {
62 public ImageDescriptor getIcon() {
63 return imageDescriptor;
67 public int getPriority() {
72 public boolean canHandle(ReadGraph graph, Resource input) throws DatabaseException {
74 ModelingResources MOD = ModelingResources.getInstance(graph);
75 Boolean result = Simantics.tryInvokeSCL(graph, contribution, MOD.EditorContribution_canHandle, input);
76 if(result == null) return false;
82 public void openEditor(Object input) throws Exception {
84 Resource r = ResourceAdaptionUtils.toSingleResource(input);
86 Simantics.getSession().asyncRequest(new ReadRequest() {
88 public void run(ReadGraph g) throws DatabaseException {
90 Variable variable = Variables.getVariable(g, r);
91 final Resource model = g.syncRequest(new PossibleIndexRoot(r));
92 final RVI rvi = variable.getPossibleRVI(g);
94 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
98 WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, r, model, rvi));
99 } catch (PartInitException e) {
100 LOGGER.error("Failed to open the graph editor", e);
112 public Collection<String> getInContexts() {
117 public boolean isActive(Collection<?> activeContextIds) {