--- /dev/null
+package org.simantics.modeling.ui.diagramEditor;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.ui.IWorkbenchPartSite;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ResourceRead;\r
+import org.simantics.db.common.request.ResourceRead2;\r
+import org.simantics.db.common.utils.RequestUtil;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.SelectionHints;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.diagram.Logger;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.ui.DiagramModelHints;\r
+import org.simantics.diagram.ui.WorkbenchSelectionProvider;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.selection.AnyResource;\r
+import org.simantics.ui.selection.AnyVariable;\r
+import org.simantics.ui.selection.WorkbenchSelectionContentType;\r
+import org.simantics.utils.DataContainer;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {\r
+\r
+ protected static class SelectionElement extends AdaptableHintContext {\r
+\r
+ final public Resource runtime;\r
+ final public Resource element;\r
+\r
+ public SelectionElement(Key[] keys, Resource runtime, Resource element) {\r
+ super(keys);\r
+ this.runtime = runtime;\r
+ this.element = element;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {\r
+ if(contentType instanceof AnyResource) return (T)element;\r
+ else if(contentType instanceof AnyVariable) {\r
+ AnyVariable type = (AnyVariable)contentType;\r
+ try {\r
+ return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {\r
+ @Override\r
+ public Variable perform(ReadGraph graph) throws DatabaseException {\r
+\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+\r
+ String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);\r
+ if (uri == null)\r
+ return null;\r
+\r
+ Variable var = Variables.getPossibleVariable(graph, uri);\r
+ if (var == null)\r
+ return null;\r
+\r
+ Resource config = graph.getPossibleObject(resource2, ModelingResources.getInstance(graph).ElementToComponent);\r
+ if (config == null)\r
+ return null;\r
+\r
+ return var.browsePossible(graph, config);\r
+\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ Logger.defaultLogError(e);\r
+ }\r
+ } \r
+ return null;\r
+ }\r
+\r
+ }\r
+\r
+ protected DataContainer<IDiagram> sourceDiagram;\r
+\r
+ public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {\r
+ super(queue, site);\r
+ assert(sourceDiagram != null);\r
+ this.sourceDiagram = sourceDiagram;\r
+ }\r
+\r
+ @Override\r
+ protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
+ ArrayList<Object> objects = new ArrayList<Object>();\r
+ IDiagram diagram = sourceDiagram.get();\r
+ if (diagram != null) {\r
+ Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
+ Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
+ for (Object o : selection) {\r
+ Resource resource = getSelectionResource(o);\r
+ if (resource != null) {\r
+ objects.add( constructSelectionElement(runtime, resource) );\r
+ } else {\r
+ System.out.println(" unrecognized selection: " + o.getClass() + ": " + o);\r
+ }\r
+ }\r
+ if (objects.isEmpty() && runtime != null && dr != null) {\r
+ objects.add( constructSelectionElement(runtime, dr) );\r
+ }\r
+ }\r
+ return new StructuredSelection(objects);\r
+ }\r
+\r
+ protected Resource getSelectionResource(Object o) {\r
+ if (o instanceof IElement) {\r
+ IElement e = (IElement) o;\r
+ Object object = e.getHint(ElementHints.KEY_OBJECT);\r
+ if (object instanceof Resource)\r
+ return (Resource) object;\r
+ object = ElementUtils.adaptElement(e, Resource.class);\r
+ if (object != null)\r
+ return (Resource) object;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * @param runtime\r
+ * @param object\r
+ * @return\r
+ */\r
+ protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {\r
+ SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);\r
+ context.setHint(SelectionHints.KEY_MAIN, object);\r
+ if (runtime != null) {\r
+ context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);\r
+ try {\r
+ Resource model = RequestUtil.trySyncRequest(\r
+ Simantics.getSession(),\r
+ SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,\r
+ SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,\r
+ null,\r
+ new ModelOfRuntime(runtime));\r
+ if (model != null)\r
+ context.setHint(SelectionHints.KEY_MODEL, model);\r
+ } catch (DatabaseException | InterruptedException ex) {\r
+ ErrorLogger.defaultLogError(ex);\r
+ }\r
+ }\r
+ return context;\r
+ }\r
+\r
+ private static class ModelOfRuntime extends ResourceRead<Resource> {\r
+ public ModelOfRuntime(Resource runtime) {\r
+ super(runtime);\r
+ }\r
+\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(graph);\r
+ String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);\r
+ if (uri != null) {\r
+ Resource resource = graph.getPossibleResource(uri);\r
+ if(resource != null) return resource;\r
+ }\r
+ Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);\r
+ if (diagram == null)\r
+ return null;\r
+ return graph.syncRequest(new PossibleModel(diagram));\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file