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
66 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
\r
70 Variable var = Variables.getPossibleVariable(graph, uri);
\r
74 Resource config = graph.getPossibleObject(resource2, ModelingResources.getInstance(graph).ElementToComponent);
\r
78 return var.browsePossible(graph, config);
\r
82 } catch (DatabaseException e) {
\r
83 Logger.defaultLogError(e);
\r
91 protected DataContainer<IDiagram> sourceDiagram;
\r
93 public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {
\r
95 assert(sourceDiagram != null);
\r
96 this.sourceDiagram = sourceDiagram;
\r
100 protected ISelection constructAdaptableSelection(Iterable<?> selection) {
\r
101 ArrayList<Object> objects = new ArrayList<Object>();
\r
102 IDiagram diagram = sourceDiagram.get();
\r
103 if (diagram != null) {
\r
104 Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
\r
105 Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
\r
106 for (Object o : selection) {
\r
107 Resource resource = getSelectionResource(o);
\r
108 if (resource != null) {
\r
109 objects.add( constructSelectionElement(runtime, resource) );
\r
111 System.out.println(" unrecognized selection: " + o.getClass() + ": " + o);
\r
114 if (objects.isEmpty() && runtime != null && dr != null) {
\r
115 objects.add( constructSelectionElement(runtime, dr) );
\r
118 return new StructuredSelection(objects);
\r
121 protected Resource getSelectionResource(Object o) {
\r
122 if (o instanceof IElement) {
\r
123 IElement e = (IElement) o;
\r
124 Object object = e.getHint(ElementHints.KEY_OBJECT);
\r
125 if (object instanceof Resource)
\r
126 return (Resource) object;
\r
127 object = ElementUtils.adaptElement(e, Resource.class);
\r
128 if (object != null)
\r
129 return (Resource) object;
\r
139 protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {
\r
140 SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);
\r
141 context.setHint(SelectionHints.KEY_MAIN, object);
\r
142 if (runtime != null) {
\r
143 context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);
\r
145 Resource model = RequestUtil.trySyncRequest(
\r
146 Simantics.getSession(),
\r
147 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
148 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
150 new ModelOfRuntime(runtime));
\r
152 context.setHint(SelectionHints.KEY_MODEL, model);
\r
153 } catch (DatabaseException | InterruptedException ex) {
\r
154 ErrorLogger.defaultLogError(ex);
\r
160 private static class ModelOfRuntime extends ResourceRead<Resource> {
\r
161 public ModelOfRuntime(Resource runtime) {
\r
166 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
167 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
168 String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);
\r
170 Resource resource = graph.getPossibleResource(uri);
\r
171 if(resource != null) return resource;
\r
173 Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
\r
174 if (diagram == null)
\r
176 return graph.syncRequest(new PossibleModel(diagram));
\r