1 package org.simantics.modeling.ui.diagramEditor;
\r
3 import java.util.ArrayList;
\r
5 import org.eclipse.jface.viewers.ISelection;
\r
6 import org.eclipse.jface.viewers.StructuredSelection;
\r
7 import org.eclipse.ui.IWorkbenchPartSite;
\r
8 import org.simantics.Simantics;
\r
9 import org.simantics.browsing.ui.swt.AdaptableHintContext;
\r
10 import org.simantics.db.ReadGraph;
\r
11 import org.simantics.db.Resource;
\r
12 import org.simantics.db.common.request.ResourceRead;
\r
13 import org.simantics.db.common.request.ResourceRead2;
\r
14 import org.simantics.db.common.utils.RequestUtil;
\r
15 import org.simantics.db.exception.DatabaseException;
\r
16 import org.simantics.db.layer0.SelectionHints;
\r
17 import org.simantics.db.layer0.request.PossibleModel;
\r
18 import org.simantics.db.layer0.variable.Variable;
\r
19 import org.simantics.db.layer0.variable.Variables;
\r
20 import org.simantics.diagram.Logger;
\r
21 import org.simantics.diagram.stubs.DiagramResource;
\r
22 import org.simantics.diagram.ui.DiagramModelHints;
\r
23 import org.simantics.diagram.ui.WorkbenchSelectionProvider;
\r
24 import org.simantics.g2d.diagram.IDiagram;
\r
25 import org.simantics.g2d.element.ElementHints;
\r
26 import org.simantics.g2d.element.ElementUtils;
\r
27 import org.simantics.g2d.element.IElement;
\r
28 import org.simantics.modeling.ModelingResources;
\r
29 import org.simantics.ui.SimanticsUI;
\r
30 import org.simantics.ui.selection.AnyResource;
\r
31 import org.simantics.ui.selection.AnyVariable;
\r
32 import org.simantics.ui.selection.WorkbenchSelectionContentType;
\r
33 import org.simantics.utils.DataContainer;
\r
34 import org.simantics.utils.threads.IThreadWorkQueue;
\r
35 import org.simantics.utils.ui.ErrorLogger;
\r
38 * @author Antti Villberg
\r
40 public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
\r
42 protected static class SelectionElement extends AdaptableHintContext {
\r
44 final public Resource runtime;
\r
45 final public Resource element;
\r
47 public SelectionElement(Key[] keys, Resource runtime, Resource element) {
\r
49 this.runtime = runtime;
\r
50 this.element = element;
\r
53 @SuppressWarnings("unchecked")
\r
55 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
\r
56 if(contentType instanceof AnyResource) return (T)element;
\r
57 else if(contentType instanceof AnyVariable) {
\r
58 AnyVariable type = (AnyVariable)contentType;
\r
60 return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
\r
62 public Variable perform(ReadGraph graph) throws DatabaseException {
\r
64 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
65 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
67 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
\r
71 Variable var = Variables.getPossibleVariable(graph, uri);
\r
75 Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);
\r
76 if (config == null) {
\r
77 // Apros #9646: if resource2 is the diagram
\r
78 // itself, return the diagram composite variable
\r
79 // since it is generally more useful than the
\r
80 // variable to the diagram.
\r
81 Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);
\r
82 if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {
\r
83 //return Variables.getPossibleVariable(graph, resource2);
\r
90 return var.browsePossible(graph, config);
\r
94 } catch (DatabaseException e) {
\r
95 Logger.defaultLogError(e);
\r
103 protected DataContainer<IDiagram> sourceDiagram;
\r
105 public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {
\r
106 super(queue, site);
\r
107 assert(sourceDiagram != null);
\r
108 this.sourceDiagram = sourceDiagram;
\r
112 protected ISelection constructAdaptableSelection(Iterable<?> selection) {
\r
113 ArrayList<Object> objects = new ArrayList<Object>();
\r
114 IDiagram diagram = sourceDiagram.get();
\r
115 if (diagram != null) {
\r
116 Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
\r
117 Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
\r
118 for (Object o : selection) {
\r
119 Resource resource = getSelectionResource(o);
\r
120 if (resource != null) {
\r
121 objects.add( constructSelectionElement(runtime, resource) );
\r
123 System.out.println(" unrecognized selection: " + o.getClass() + ": " + o);
\r
126 if (objects.isEmpty() && runtime != null && dr != null) {
\r
127 objects.add( constructSelectionElement(runtime, dr) );
\r
130 return new StructuredSelection(objects);
\r
133 protected Resource getSelectionResource(Object o) {
\r
134 if (o instanceof IElement) {
\r
135 IElement e = (IElement) o;
\r
136 Object object = e.getHint(ElementHints.KEY_OBJECT);
\r
137 if (object instanceof Resource)
\r
138 return (Resource) object;
\r
139 object = ElementUtils.adaptElement(e, Resource.class);
\r
140 if (object != null)
\r
141 return (Resource) object;
\r
151 protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {
\r
152 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);
\r
153 context.setHint(SelectionHints.KEY_MAIN, object);
\r
154 if (runtime != null) {
\r
155 context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);
\r
157 Resource model = RequestUtil.trySyncRequest(
\r
158 Simantics.getSession(),
\r
159 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
160 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
162 new ModelOfRuntime(runtime));
\r
164 context.setHint(SelectionHints.KEY_MODEL, model);
\r
165 } catch (DatabaseException | InterruptedException ex) {
\r
166 ErrorLogger.defaultLogError(ex);
\r
172 private static class ModelOfRuntime extends ResourceRead<Resource> {
\r
173 public ModelOfRuntime(Resource runtime) {
\r
178 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
179 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
180 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);
\r
182 Resource resource = graph.getPossibleResource(uri);
\r
183 if(resource != null) return resource;
\r
185 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
\r
186 if (diagram == null)
\r
188 return graph.syncRequest(new PossibleModel(diagram));
\r