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.swt.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.WorkbenchSelectionContentType;
35 import org.simantics.utils.DataContainer;
36 import org.simantics.utils.threads.IThreadWorkQueue;
37 import org.simantics.utils.ui.ErrorLogger;
40 * @author Antti Villberg
42 public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
44 protected static class SelectionElement extends AdaptableHintContext {
46 final public Resource runtime;
47 final public Resource element;
49 public SelectionElement(Key[] keys, Resource runtime, Resource element) {
51 this.runtime = runtime;
52 this.element = element;
55 @SuppressWarnings("unchecked")
57 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
58 if(contentType instanceof AnyResource) return (T)element;
59 else if(contentType instanceof AnyVariable) {
60 AnyVariable type = (AnyVariable)contentType;
62 return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
64 public Variable perform(ReadGraph graph) throws DatabaseException {
66 DiagramResource DIA = DiagramResource.getInstance(graph);
67 ModelingResources MOD = ModelingResources.getInstance(graph);
68 Layer0 L0 = Layer0.getInstance(graph);
70 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
74 Variable var = Variables.getPossibleVariable(graph, uri);
78 Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);
80 if (graph.isInstanceOf(resource2, DIA.Connection)) {
81 Variable v = FlagUtil.getPossibleConnectionSignal(graph, var, resource2, L0.Entity);
85 // Apros #9646: if resource2 is the diagram
86 // itself, return the diagram composite variable
87 // since it is generally more useful than the
88 // variable to the diagram.
89 Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);
90 if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {
91 //return Variables.getPossibleVariable(graph, resource2);
95 if(graph.isInstanceOf(resource2, DIA.Flag)) {
96 Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity);
105 return var.browsePossible(graph, config);
109 } catch (DatabaseException e) {
110 Logger.defaultLogError(e);
118 protected DataContainer<IDiagram> sourceDiagram;
120 public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {
122 assert(sourceDiagram != null);
123 this.sourceDiagram = sourceDiagram;
127 protected ISelection constructAdaptableSelection(Iterable<?> selection) {
128 ArrayList<Object> objects = new ArrayList<Object>();
129 IDiagram diagram = sourceDiagram.get();
130 if (diagram != null) {
131 Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
132 Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
133 for (Object o : selection) {
134 Resource resource = getSelectionResource(o);
135 if (resource != null) {
136 objects.add( constructSelectionElement(runtime, resource) );
138 System.out.println(" unrecognized selection: " + o.getClass() + ": " + o);
141 if (objects.isEmpty() && runtime != null && dr != null) {
142 objects.add( constructSelectionElement(runtime, dr) );
145 return new StructuredSelection(objects);
148 protected Resource getSelectionResource(Object o) {
149 if (o instanceof IElement) {
150 IElement e = (IElement) o;
151 Object object = e.getHint(ElementHints.KEY_OBJECT);
152 if (object instanceof Resource)
153 return (Resource) object;
154 object = ElementUtils.adaptElement(e, Resource.class);
156 return (Resource) object;
166 protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {
167 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);
168 context.setHint(SelectionHints.KEY_MAIN, object);
169 if (runtime != null) {
170 context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);
172 Resource model = RequestUtil.trySyncRequest(
173 Simantics.getSession(),
174 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
175 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
177 new ModelOfRuntime(runtime));
179 context.setHint(SelectionHints.KEY_MODEL, model);
180 } catch (DatabaseException | InterruptedException ex) {
181 ErrorLogger.defaultLogError(ex);
187 private static class ModelOfRuntime extends ResourceRead<Resource> {
188 public ModelOfRuntime(Resource runtime) {
193 public Resource perform(ReadGraph graph) throws DatabaseException {
194 DiagramResource DIA = DiagramResource.getInstance(graph);
195 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);
197 Resource resource = graph.getPossibleResource(uri);
198 if(resource != null) return resource;
200 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
203 return graph.syncRequest(new PossibleModel(diagram));