1 package org.simantics.modeling.ui.diagramEditor;
3 import java.util.ArrayList;
5 import org.eclipse.jface.viewers.ISelection;
6 import org.eclipse.jface.viewers.StructuredSelection;
7 import org.eclipse.ui.IWorkbenchPartSite;
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.common.AdaptableHintContext;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.ResourceRead;
13 import org.simantics.db.common.request.ResourceRead2;
14 import org.simantics.db.common.utils.RequestUtil;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.SelectionHints;
17 import org.simantics.db.layer0.request.PossibleModel;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.Variables;
20 import org.simantics.diagram.Logger;
21 import org.simantics.diagram.flag.FlagUtil;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.ui.DiagramModelHints;
24 import org.simantics.diagram.ui.WorkbenchSelectionProvider;
25 import org.simantics.g2d.diagram.IDiagram;
26 import org.simantics.g2d.element.ElementHints;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.modeling.ModelingResources;
31 import org.simantics.ui.SimanticsUI;
32 import org.simantics.ui.selection.AnyResource;
33 import org.simantics.ui.selection.AnyVariable;
34 import org.simantics.ui.selection.ParentVariable;
35 import org.simantics.ui.selection.WorkbenchSelectionContentType;
36 import org.simantics.utils.DataContainer;
37 import org.simantics.utils.threads.IThreadWorkQueue;
38 import org.simantics.utils.ui.ErrorLogger;
41 * @author Antti Villberg
43 public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
45 protected static class SelectionElement extends AdaptableHintContext {
47 final public Resource runtime;
48 final public Resource element;
50 public SelectionElement(Key[] keys, Resource runtime, Resource element) {
52 this.runtime = runtime;
53 this.element = element;
56 @SuppressWarnings("unchecked")
58 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
59 if(contentType instanceof AnyResource) return (T)element;
60 else if(contentType instanceof AnyVariable) {
61 AnyVariable type = (AnyVariable)contentType;
63 return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
65 public Variable perform(ReadGraph graph) throws DatabaseException {
67 DiagramResource DIA = DiagramResource.getInstance(graph);
68 ModelingResources MOD = ModelingResources.getInstance(graph);
69 Layer0 L0 = Layer0.getInstance(graph);
71 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
75 Variable var = Variables.getPossibleVariable(graph, uri);
79 Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);
81 if (graph.isInstanceOf(resource2, DIA.Connection)) {
82 Variable v = FlagUtil.getPossibleConnectionSignal(graph, var, resource2, L0.Entity);
86 // Apros #9646: if resource2 is the diagram
87 // itself, return the diagram composite variable
88 // since it is generally more useful than the
89 // variable to the diagram.
90 Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);
91 if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {
92 //return Variables.getPossibleVariable(graph, resource2);
96 if(graph.isInstanceOf(resource2, DIA.Flag)) {
97 Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity);
106 return var.browsePossible(graph, config);
110 } catch (DatabaseException e) {
111 Logger.defaultLogError(e);
114 else if(contentType instanceof ParentVariable) {
115 ParentVariable type = (ParentVariable)contentType;
117 return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
119 public Variable perform(ReadGraph graph) throws DatabaseException {
121 DiagramResource DIA = DiagramResource.getInstance(graph);
122 ModelingResources MOD = ModelingResources.getInstance(graph);
123 Layer0 L0 = Layer0.getInstance(graph);
125 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
129 Variable var = Variables.getPossibleVariable(graph, uri);
136 } catch (DatabaseException e) {
137 Logger.defaultLogError(e);
146 protected DataContainer<IDiagram> sourceDiagram;
148 public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {
150 assert(sourceDiagram != null);
151 this.sourceDiagram = sourceDiagram;
155 protected ISelection constructAdaptableSelection(Iterable<?> selection) {
156 ArrayList<Object> objects = new ArrayList<Object>();
157 IDiagram diagram = sourceDiagram.get();
158 if (diagram != null) {
159 Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
160 Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
161 for (Object o : selection) {
162 Resource resource = getSelectionResource(o);
163 if (resource != null) {
164 objects.add( constructSelectionElement(runtime, resource) );
166 System.out.println(" unrecognized selection: " + o.getClass() + ": " + o); //$NON-NLS-1$ //$NON-NLS-2$
169 if (objects.isEmpty() && runtime != null && dr != null) {
170 objects.add( constructSelectionElement(runtime, dr) );
173 return new StructuredSelection(objects);
176 protected Resource getSelectionResource(Object o) {
177 if (o instanceof IElement) {
178 IElement e = (IElement) o;
179 Object object = e.getHint(ElementHints.KEY_OBJECT);
180 if (object instanceof Resource)
181 return (Resource) object;
182 object = ElementUtils.adaptElement(e, Resource.class);
184 return (Resource) object;
194 protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {
195 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);
196 context.setHint(SelectionHints.KEY_MAIN, object);
197 if (runtime != null) {
198 context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);
200 Resource model = RequestUtil.trySyncRequest(
201 Simantics.getSession(),
202 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
203 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
205 new ModelOfRuntime(runtime));
207 context.setHint(SelectionHints.KEY_MODEL, model);
208 } catch (DatabaseException | InterruptedException ex) {
209 ErrorLogger.defaultLogError(ex);
215 private static class ModelOfRuntime extends ResourceRead<Resource> {
216 public ModelOfRuntime(Resource runtime) {
221 public Resource perform(ReadGraph graph) throws DatabaseException {
222 DiagramResource DIA = DiagramResource.getInstance(graph);
223 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);
225 Resource resource = graph.getPossibleResource(uri);
226 if(resource != null) return resource;
228 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
231 return graph.syncRequest(new PossibleModel(diagram));