]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java
Removed unnecessary dependencies on org.apache.log4j
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramViewerSelectionProvider.java
index 42c489e24d4b866d310507cf7d1d00d4cc1508cf..d9d14c37eada50ab64cddc9a9e55c0e94cf64249 100644 (file)
-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.flag.FlagUtil;\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.layer0.Layer0;\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
-                            ModelingResources MOD = ModelingResources.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, MOD.ElementToComponent);\r
-                            if (config == null) {\r
-                                // Apros #9646: if resource2 is the diagram\r
-                                // itself, return the diagram composite variable\r
-                                // since it is generally more useful than the\r
-                                // variable to the diagram.\r
-                                Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);\r
-                                if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {\r
-                                    //return Variables.getPossibleVariable(graph, resource2);\r
-                                    return var;\r
-                                }\r
-                                \r
-                                if(graph.isInstanceOf(resource2, DIA.Flag)) {\r
-                                       Layer0 L0 = Layer0.getInstance(graph);\r
-                                       Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity);\r
-                                       if(signal != null)\r
-                                               return signal;\r
-                                }\r
-\r
-                                return null;\r
-                                \r
-                            }\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
+package org.simantics.modeling.ui.diagramEditor;
+
+import java.util.ArrayList;
+
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IWorkbenchPartSite;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.common.AdaptableHintContext;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.ResourceRead2;
+import org.simantics.db.common.utils.RequestUtil;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.diagram.flag.FlagUtil;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.diagram.ui.WorkbenchSelectionProvider;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.ui.selection.AnyResource;
+import org.simantics.ui.selection.AnyVariable;
+import org.simantics.ui.selection.ParentVariable;
+import org.simantics.ui.selection.WorkbenchSelectionContentType;
+import org.simantics.utils.DataContainer;
+import org.simantics.utils.threads.IThreadWorkQueue;
+import org.simantics.utils.ui.ErrorLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Antti Villberg
+ */
+public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider {
+
+    protected static class SelectionElement extends AdaptableHintContext {
+
+        private static final Logger LOGGER = LoggerFactory.getLogger(SelectionElement.class);
+
+        final public Resource runtime;
+        final public Resource element;
+
+        public SelectionElement(Key[] keys, Resource runtime, Resource element) {
+            super(keys);
+            this.runtime = runtime;
+            this.element = element;
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
+               if(contentType instanceof AnyResource) return (T)element;
+               else if(contentType instanceof AnyVariable) {
+                AnyVariable type = (AnyVariable)contentType;
+                try {
+                    return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
+                        @Override
+                        public Variable perform(ReadGraph graph) throws DatabaseException {
+
+                            DiagramResource DIA = DiagramResource.getInstance(graph);
+                            ModelingResources MOD = ModelingResources.getInstance(graph);
+                            Layer0 L0 = Layer0.getInstance(graph);
+
+                            String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
+                            if (uri == null)
+                                return null;
+
+                            Variable var = Variables.getPossibleVariable(graph, uri);
+                            if (var == null)
+                                return null;
+
+                            Resource config = graph.getPossibleObject(resource2, MOD.ElementToComponent);
+                            if (config == null) {
+                                if (graph.isInstanceOf(resource2, DIA.Connection)) {
+                                    Variable v = FlagUtil.getPossibleConnectionSignal(graph, var, resource2, L0.Entity);
+                                    if (v != null)
+                                        return v;
+                                }
+                                // Apros #9646: if resource2 is the diagram
+                                // itself, return the diagram composite variable
+                                // since it is generally more useful than the
+                                // variable to the diagram.
+                                Resource composite = graph.getPossibleObject(resource2, MOD.DiagramToComposite);
+                                if (composite != null && composite.equals(var.getPossibleRepresents(graph))) {
+                                    //return Variables.getPossibleVariable(graph, resource2);
+                                    return var;
+                                }
+                                
+                                if(graph.isInstanceOf(resource2, DIA.Flag)) {
+                                       Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity);
+                                       if(signal != null)
+                                               return signal;
+                                }
+
+                                return null;
+                                
+                            }
+
+                            return var.browsePossible(graph, config);
+
+                        }
+                    });
+                } catch (DatabaseException e) {
+                    LOGGER.error("WorkbenchSelectionElement.getContent failed for type AnyVariable", e);
+                }
+            }
+            else if(contentType instanceof ParentVariable) {
+                ParentVariable type = (ParentVariable)contentType;
+                try {
+                    return (T) type.processor.sync(new ResourceRead2<Variable>(runtime, element) {
+                        @Override
+                        public Variable perform(ReadGraph graph) throws DatabaseException {
+                            DiagramResource DIA = DiagramResource.getInstance(graph);
+
+                            String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasVariable);
+                            if (uri == null)
+                                return null;
+
+                            Variable var = Variables.getPossibleVariable(graph, uri);
+                            if (var == null)
+                                return null;
+
+                            return var;
+                        }
+                    });
+                } catch (DatabaseException e) {
+                    LOGGER.error("WorkbenchSelectionElement.getContent failed for type ParentVariable", e);
+                }
+            } 
+
+            return null;
+        }
+
+    }
+
+    protected DataContainer<IDiagram> sourceDiagram;
+
+    public DiagramViewerSelectionProvider(IThreadWorkQueue queue, IWorkbenchPartSite site, DataContainer<IDiagram> sourceDiagram) {
+        super(queue, site);
+       assert(sourceDiagram != null);
+        this.sourceDiagram = sourceDiagram;
+    }
+
+       @Override
+       protected ISelection constructAdaptableSelection(Iterable<?> selection) {
+               ArrayList<Object> objects = new ArrayList<Object>();
+               IDiagram diagram = sourceDiagram.get();
+               if (diagram != null) {
+                       Resource runtime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
+                       Resource dr = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
+                       for (Object o : selection) {
+                               Resource resource = getSelectionResource(o);
+                               if (resource != null) {
+                                       objects.add( constructSelectionElement(runtime, resource) );
+                               } else {
+                                       System.out.println("  unrecognized selection: " + o.getClass() + ": " + o); //$NON-NLS-1$ //$NON-NLS-2$
+                               }
+                       }
+                       if (objects.isEmpty() && runtime != null && dr != null) {
+                               objects.add( constructSelectionElement(runtime, dr) );
+                       }
+               }
+               return new StructuredSelection(objects);
+       }
+
+       protected Resource getSelectionResource(Object o) {
+               if (o instanceof IElement) {
+                       IElement e = (IElement) o;
+                       Object object = e.getHint(ElementHints.KEY_OBJECT);
+                       if (object instanceof Resource)
+                               return (Resource) object;
+                       object = ElementUtils.adaptElement(e, Resource.class);
+                       if (object != null)
+                               return (Resource) object;
+               }
+               return null;
+       }
+
+       /**
+        * @param runtime
+        * @param object
+        * @return
+        */
+       protected static SelectionElement constructSelectionElement(Resource runtime, Resource object) {
+               SelectionElement context = new SelectionElement(SelectionHints.STD_KEYS, runtime, object);
+               context.setHint(SelectionHints.KEY_MAIN, object);
+               if (runtime != null) {
+                       context.setHint(SelectionHints.KEY_VARIABLE_RESOURCE, runtime);
+                       try {
+                               Resource model = RequestUtil.trySyncRequest(
+                                               Simantics.getSession(),
+                                               SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
+                                               SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
+                                               null,
+                                               new ModelOfRuntime(runtime));
+                               if (model != null)
+                                       context.setHint(SelectionHints.KEY_MODEL, model);
+                       } catch (DatabaseException | InterruptedException ex) {
+                               ErrorLogger.defaultLogError(ex);
+                       }
+               }
+               return context;
+       }
+
+       private static class ModelOfRuntime extends ResourceRead<Resource> {
+               public ModelOfRuntime(Resource runtime) {
+                       super(runtime);
+               }
+
+               @Override
+               public Resource perform(ReadGraph graph) throws DatabaseException {
+                       DiagramResource DIA = DiagramResource.getInstance(graph);
+                       String uri = graph.getPossibleRelatedValue(resource, DIA.RuntimeDiagram_HasModelURI);
+                       if (uri != null) {
+                               Resource resource = graph.getPossibleResource(uri);
+                               if(resource != null) return resource;
+                       }
+                       Resource diagram = graph.getPossibleObject(resource, DIA.RuntimeDiagram_HasConfiguration);
+                       if (diagram == null)
+                               return null;
+                       return graph.syncRequest(new PossibleModel(diagram));
+               }
+       }
+
 }
\ No newline at end of file