]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/GraphEditorAdapterDescriptor.java
Allow OpenWithMenuContribution to filter out currently active editor.
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / GraphEditorAdapterDescriptor.java
1 package org.simantics.ui.workbench.editor;
2
3 import java.util.Collection;
4
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;
23
24 public class GraphEditorAdapterDescriptor implements EditorAdapterDescriptor {
25     private static final Logger LOGGER = LoggerFactory.getLogger(GraphEditorAdapterDescriptor.class);
26
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;
32
33         public GraphEditorAdapterDescriptor(String editorId, String label, ImageDescriptor imageDescriptor, Resource contribution, int priority) {
34                 this.editorId = editorId;
35                 this.label = label;
36                 this.imageDescriptor = imageDescriptor;
37                 this.contribution = contribution;
38                 this.priority = priority;
39         }
40         
41         @Override
42         public String getId() {
43                 return editorId;
44         }
45
46         @Override
47         public String getGroupId() {
48                 return null;
49         }
50
51         @Override
52         public EditorAdapter getAdapter() {
53                 
54                 return new AbstractResourceEditorAdapter(editorId, null, priority) {
55
56                         @Override
57                         public String getName() {
58                                 return label;
59                         }
60
61                         @Override
62                         public ImageDescriptor getIcon() {
63                                 return imageDescriptor;
64                         }
65
66                         @Override
67                         public int getPriority() {
68                                 return priority;
69                         }
70                         
71                         @Override
72                         public String getEditorId() {
73                                 return editorId;
74                         }
75
76                         @Override
77                         public boolean canHandle(ReadGraph graph, Resource input) throws DatabaseException {
78                                 
79                                 ModelingResources MOD = ModelingResources.getInstance(graph);
80                         Boolean result = Simantics.tryInvokeSCL(graph, contribution, MOD.EditorContribution_canHandle, input);
81                         if(result == null) return false;
82                         return result;
83                         
84                         }
85
86                         @Override
87                         public void openEditor(Object input) throws Exception {
88                                 
89                         Resource r = ResourceAdaptionUtils.toSingleResource(input);
90                                 
91                         Simantics.getSession().asyncRequest(new ReadRequest() {
92                             @Override
93                             public void run(ReadGraph g) throws DatabaseException {
94
95                                 Variable variable = Variables.getVariable(g, r);
96                                 final Resource model = g.syncRequest(new PossibleIndexRoot(r));
97                                 final RVI rvi = variable.getPossibleRVI(g);
98                                 
99                                 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
100                                     @Override
101                                     public void run() {
102                                         try {
103                                             WorkbenchUtils.openEditor(editorId, new ResourceEditorInput2(editorId, r, model, rvi));
104                                         } catch (PartInitException e) {
105                                             LOGGER.error("Failed to open the graph editor", e);
106                                         }
107                                     }
108                                 });
109                             }
110                         });
111                         }
112                         
113                 };
114         }
115
116         @Override
117         public Collection<String> getInContexts() {
118                 return null;
119         }
120
121         @Override
122         public boolean isActive(Collection<?> activeContextIds) {
123                 return true;
124         }
125
126 }