]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Overriding selection and hover highlights 93/3393/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 23 Oct 2019 11:34:31 +0000 (14:34 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 23 Oct 2019 11:34:31 +0000 (14:34 +0300)
Additionally changed VTKSelectionItem equals to handle different Objects
/ Resources with the same vtkProp.

gitlab #29

Change-Id: I305ff007bd48660d736b1abd01ca183075ba8b88

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/NodeSelectionProvider2.java
org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java
org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/VTKSelectionItem.java
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java

index 6d0a18423b916df35b8dcc4d445dbc086347cef5..5de69f354b03b38142772ccb227bd339589f95c6 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.vtk.common;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.List;\r
-\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionChangedListener;\r
-import org.eclipse.jface.viewers.ISelectionProvider;\r
-import org.eclipse.jface.viewers.SelectionChangedEvent;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-import org.eclipse.ui.IEditorPart;\r
-import org.eclipse.ui.ISelectionListener;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.simantics.db.Resource;\r
-import org.simantics.g3d.scenegraph.IG3DNode;\r
-import org.simantics.g3d.scenegraph.base.INode;\r
-import org.simantics.g3d.tools.AdaptationUtils;\r
-import org.simantics.objmap.graph.IMapping;\r
-import org.simantics.objmap.structural.StructuralResource;\r
-\r
-import vtk.vtkProp;\r
-\r
-public class NodeSelectionProvider2<DBObject,JavaObject> implements ISelectionProvider, ISelectionChangedListener, ISelectionListener{\r
-       private ISelection selection = new StructuredSelection();\r
-       private List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();\r
-       \r
-       List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>(); // selection is ordered\r
-       //List<Resource> selectedResources = new ArrayList<Resource>();\r
-       List<VTKSelectionItem<DBObject>> selectedItems = new ArrayList<VTKSelectionItem<DBObject>>();\r
-       \r
-       IEditorPart part;\r
-       IMapping<DBObject,JavaObject> mapping;\r
-       VTKNodeMap nodeMap;\r
-       \r
-       public NodeSelectionProvider2(IEditorPart part, IMapping<DBObject,JavaObject> mapping, VTKNodeMap nodeMap) {\r
-               this.part = part;\r
-               this.mapping = mapping;\r
-               this.nodeMap = nodeMap;\r
-       }\r
-       \r
-       @Override\r
-       public void addSelectionChangedListener(\r
-                       ISelectionChangedListener listener) {\r
-               listeners.add(listener);\r
-       }\r
-       \r
-       @Override\r
-       public void removeSelectionChangedListener(\r
-                       ISelectionChangedListener listener) {\r
-               listeners.remove(listener);\r
-       }\r
-       \r
-       @Override\r
-       public ISelection getSelection() {\r
-               return selection;\r
-       }\r
-       \r
-       @Override\r
-       public void setSelection(ISelection selection) {\r
-               \r
-       }\r
-       \r
-       // events coming from vtk       \r
-       @Override\r
-       public void selectionChanged(SelectionChangedEvent event) {\r
-               ISelection s = event.getSelection();\r
-               \r
-               processSelection(s);\r
-               fireSelectionChanged(event.getSource());\r
-       }\r
-       \r
-       @SuppressWarnings("unchecked")\r
-       private void processSelection(ISelection s) {\r
-               selectedNodes.clear();\r
-               selectedItems.clear();\r
-               \r
-               Collection<vtkProp> selectedActors = AdaptationUtils.adaptToCollection(s, vtkProp.class);\r
-               if (selectedActors.size() > 0) {\r
-                       for (vtkProp a : selectedActors) {\r
-                               IG3DNode node = (IG3DNode)nodeMap.getNode((vtkProp)a);\r
-                               if (node == null)\r
-                                       continue;\r
-                               if (!selectedNodes.contains(node))\r
-                                       selectedNodes.add(node);\r
-                               DBObject r = mapping.inverseGet((JavaObject)node);\r
-                               selectedItems.add(new VTKSelectionItem<DBObject>(a, node,r));\r
-                       }\r
-               } else {\r
-                       Collection<IG3DNode> selectedNds = AdaptationUtils.adaptToCollection(s, IG3DNode.class);\r
-                       for (INode node : selectedNds) {\r
-                               if (!selectedNodes.contains(node))\r
-                                       selectedNodes.add((IG3DNode)node);\r
-                               DBObject r = mapping.inverseGet((JavaObject)node);\r
-                               selectedItems.add(new VTKSelectionItem<DBObject>(null, (IG3DNode)node,r));\r
-                       }\r
-               }\r
-               \r
-               \r
-               selection = new StructuredSelection(selectedItems);\r
-       }\r
-       \r
-       private void fireSelectionChanged(Object source) {\r
-               SelectionChangedEvent evt = new SelectionChangedEvent((ISelectionProvider)source, selection);\r
-               for (ISelectionChangedListener l : listeners) {\r
-                       l.selectionChanged(evt);\r
-               }\r
-       }\r
-       \r
-       \r
-       \r
-       // events coming from workbench\r
-       @Override\r
-       public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
-               if (part == this.part)\r
-                       return;\r
-               processSelection(selection);\r
-               \r
-       }\r
-       \r
-       public List<IG3DNode> getSelectedNodes() {\r
-               return selectedNodes;\r
-       }\r
-       \r
-       @SuppressWarnings("unchecked")\r
-       public List<DBObject> getSelectedResources() {\r
-               \r
-               List<DBObject> list = new ArrayList<DBObject>();\r
-               for (VTKSelectionItem<DBObject> i : selectedItems) {\r
-                       DBObject r = (DBObject)i.getAdapter(Resource.class);\r
-                       if (r == null)\r
-                               r = (DBObject)i.getAdapter(StructuralResource.class);\r
-                       if (r == null)\r
-                               continue;\r
-                       if (!list.contains(r))\r
-                               list.add(r);\r
-               }\r
-               return list;\r
-       }\r
-       \r
-       protected INode getNode(DBObject r) {\r
-               return (INode)mapping.get(r);\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.common;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.ISelectionListener;
+import org.eclipse.ui.IWorkbenchPart;
+import org.simantics.db.Resource;
+import org.simantics.g3d.scenegraph.IG3DNode;
+import org.simantics.g3d.scenegraph.base.INode;
+import org.simantics.g3d.tools.AdaptationUtils;
+import org.simantics.objmap.graph.IMapping;
+import org.simantics.objmap.structural.StructuralResource;
+
+import vtk.vtkProp;
+
+public class NodeSelectionProvider2<DBObject,JavaObject> implements ISelectionProvider, ISelectionChangedListener, ISelectionListener{
+    protected ISelection selection = new StructuredSelection();
+       protected List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();
+       
+       protected List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>(); // selection is ordered
+       //List<Resource> selectedResources = new ArrayList<Resource>();
+       protected List<VTKSelectionItem<DBObject>> selectedItems = new ArrayList<VTKSelectionItem<DBObject>>();
+       
+       protected IEditorPart part;
+       protected IMapping<DBObject,JavaObject> mapping;
+       protected VTKNodeMap nodeMap;
+       
+       public NodeSelectionProvider2(IEditorPart part, IMapping<DBObject,JavaObject> mapping, VTKNodeMap nodeMap) {
+               this.part = part;
+               this.mapping = mapping;
+               this.nodeMap = nodeMap;
+       }
+       
+       @Override
+       public void addSelectionChangedListener(
+                       ISelectionChangedListener listener) {
+               listeners.add(listener);
+       }
+       
+       @Override
+       public void removeSelectionChangedListener(
+                       ISelectionChangedListener listener) {
+               listeners.remove(listener);
+       }
+       
+       @Override
+       public ISelection getSelection() {
+               return selection;
+       }
+       
+       @Override
+       public void setSelection(ISelection selection) {
+               
+       }
+       
+       // events coming from vtk       
+       @Override
+       public void selectionChanged(SelectionChangedEvent event) {
+               ISelection s = event.getSelection();
+               
+               processSelection(s);
+               fireSelectionChanged(event.getSource());
+       }
+       
+       @SuppressWarnings("unchecked")
+       protected void processSelection(ISelection s) {
+               selectedNodes.clear();
+               selectedItems.clear();
+               
+               Collection<vtkProp> selectedActors = AdaptationUtils.adaptToCollection(s, vtkProp.class);
+               if (selectedActors.size() > 0) {
+                       for (vtkProp a : selectedActors) {
+                               IG3DNode node = (IG3DNode)nodeMap.getNode((vtkProp)a);
+                               if (node == null)
+                                       continue;
+                               if (!selectedNodes.contains(node))
+                                       selectedNodes.add(node);
+                               DBObject r = mapping.inverseGet((JavaObject)node);
+                               selectedItems.add(new VTKSelectionItem<DBObject>(a, node,r));
+                       }
+               } else {
+                       Collection<IG3DNode> selectedNds = AdaptationUtils.adaptToCollection(s, IG3DNode.class);
+                       for (INode node : selectedNds) {
+                               if (!selectedNodes.contains(node))
+                                       selectedNodes.add((IG3DNode)node);
+                               DBObject r = mapping.inverseGet((JavaObject)node);
+                               selectedItems.add(new VTKSelectionItem<DBObject>(null, (IG3DNode)node,r));
+                       }
+               }
+               
+               
+               selection = new StructuredSelection(selectedItems);
+       }
+       
+       protected void fireSelectionChanged(Object source) {
+               SelectionChangedEvent evt = new SelectionChangedEvent((ISelectionProvider)source, selection);
+               for (ISelectionChangedListener l : listeners) {
+                       l.selectionChanged(evt);
+               }
+       }
+       
+       
+       
+       // events coming from workbench
+       @Override
+       public void selectionChanged(IWorkbenchPart part, ISelection selection) {
+               if (part == this.part)
+                       return;
+               processSelection(selection);
+               
+       }
+       
+       public List<IG3DNode> getSelectedNodes() {
+               return selectedNodes;
+       }
+       
+       @SuppressWarnings("unchecked")
+       public List<DBObject> getSelectedResources() {
+               
+               List<DBObject> list = new ArrayList<DBObject>();
+               for (VTKSelectionItem<DBObject> i : selectedItems) {
+                       DBObject r = (DBObject)i.getAdapter(Resource.class);
+                       if (r == null)
+                               r = (DBObject)i.getAdapter(StructuralResource.class);
+                       if (r == null)
+                               continue;
+                       if (!list.contains(r))
+                               list.add(r);
+               }
+               return list;
+       }
+       
+       protected INode getNode(DBObject r) {
+               return (INode)mapping.get(r);
+       }
+}
index dec7421152fd683ed4da8fccfc1f8b2b1b45d0f0..789cfb1883c7218fde90b5d4e54b1ac740f3c091 100644 (file)
@@ -35,17 +35,17 @@ import vtk.vtkMapper;
 import vtk.vtkProp;
 import vtk.vtkProperty;
 
-public class SelectionHighlighter<E extends IG3DNode> implements ISelectionChangedListener{
+public class SelectionHighlighter<E extends INode> implements ISelectionChangedListener{
 
        
        
-       VtkView panel;
-       VTKNodeMap<E> nodeMap;
+       protected VtkView panel;
+       protected VTKNodeMap<E> nodeMap;
        
-       List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>();
-       List<vtkActor> selectedActors = new ArrayList<vtkActor>();
+       protected List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>();
+       protected List<vtkActor> selectedActors = new ArrayList<vtkActor>();
        
-       HighlightObjectType type = HighlightObjectType.Node;
+       protected HighlightObjectType type = HighlightObjectType.Node;
        
        public SelectionHighlighter(VtkView panel, VTKNodeMap<E> nodeMap) {
                this.panel = panel;
index d998801b50faed05e3debfc8d96e384d5037d0e7..222da8b92012de4a404d0d3f862b84ed926768e3 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.vtk.common;\r
-\r
-import org.eclipse.core.runtime.IAdaptable;\r
-import org.simantics.db.Resource;\r
-import org.simantics.g3d.scenegraph.IG3DNode;\r
-import org.simantics.g3d.scenegraph.base.INode;\r
-import org.simantics.objmap.structural.StructuralResource;\r
-\r
-import vtk.vtkProp;\r
-\r
-public class VTKSelectionItem<DBObject> implements IAdaptable{\r
-\r
-       private vtkProp prop;\r
-       private IG3DNode node;\r
-       private DBObject resource;\r
-\r
-       //private VTKpropSet actors;\r
-       \r
-       \r
-       public VTKSelectionItem(vtkProp prop, IG3DNode node, DBObject res) {\r
-               this.prop = prop;\r
-               this.node = node;\r
-               this.resource = res;\r
-       }\r
-       \r
-//     public VTKSelectionItem(Resource res, Collection<vtkProp> actors) {\r
-//             this.resource = res;\r
-//             this.actors = new VTKpropSet();\r
-//             this.actors.addAll(actors);\r
-//     }\r
-//     \r
-//     public VTKSelectionItem(Resource res, vtkProp... actors) {\r
-//             this.resource = res;\r
-//             this.actors = new VTKpropSet();\r
-//             for (vtkProp a : actors)\r
-//                     this.actors.add(a);\r
-//     }\r
-\r
-       @SuppressWarnings("rawtypes")\r
-       @Override\r
-       public Object getAdapter(Class adapter) {\r
-               if (adapter == Resource.class)\r
-                       if (resource instanceof Resource)\r
-                               return resource;\r
-                       else\r
-                               return null;\r
-               if (adapter == StructuralResource.class)\r
-                       if (resource instanceof StructuralResource)\r
-                               return resource;\r
-                       else\r
-                               return null;\r
-               if (adapter == vtkProp.class)\r
-                       return prop;\r
-               if (adapter == IG3DNode.class)\r
-                       return node;\r
-               if (adapter == INode.class)\r
-                       return node;\r
-               return null;\r
-       }\r
-       \r
-//     @SuppressWarnings("rawtypes")\r
-//     @Override\r
-//     public Object getAdapter(Class adapter) {\r
-//             if (adapter == Resource.class)\r
-//                     return resource;\r
-//             if (adapter == VTKpropSet.class)\r
-//                     return actors;\r
-//             return null;\r
-//     }\r
-       \r
-//     public Resource getResource() {\r
-//             return resource;\r
-//     }\r
-//     \r
-//     public VTKpropSet getActors() {\r
-//             return actors;\r
-//     }\r
-       \r
-       @SuppressWarnings("rawtypes")\r
-       @Override\r
-       public boolean equals(Object obj) {\r
-               if (obj == null)\r
-                       return false;\r
-               if (obj.getClass() != this.getClass())\r
-                       return false;\r
-               VTKSelectionItem other = (VTKSelectionItem)obj;\r
-               if (prop != null)\r
-                       return prop.equals(other.prop);\r
-               if (node != null)\r
-                       return node.equals(other.node);\r
-               return resource.equals(other.resource);\r
-               //return resource.equals(other.resource);\r
-                       \r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.common;
+
+import org.eclipse.core.runtime.IAdaptable;
+import org.simantics.db.Resource;
+import org.simantics.g3d.scenegraph.IG3DNode;
+import org.simantics.g3d.scenegraph.base.INode;
+import org.simantics.objmap.structural.StructuralResource;
+
+import vtk.vtkProp;
+
+public class VTKSelectionItem<DBObject> implements IAdaptable{
+
+       private vtkProp prop;
+       private IG3DNode node;
+       private DBObject resource;
+       
+       public VTKSelectionItem(vtkProp prop, IG3DNode node, DBObject res) {
+               this.prop = prop;
+               this.node = node;
+               this.resource = res;
+       }
+
+       @SuppressWarnings("rawtypes")
+       @Override
+       public Object getAdapter(Class adapter) {
+               if (adapter == Resource.class)
+                       if (resource instanceof Resource)
+                               return resource;
+                       else
+                               return null;
+               if (adapter == StructuralResource.class)
+                       if (resource instanceof StructuralResource)
+                               return resource;
+                       else
+                               return null;
+               if (adapter == vtkProp.class)
+                       return prop;
+               if (adapter == IG3DNode.class)
+                       return node;
+               if (adapter == INode.class)
+                       return node;
+               return null;
+       }
+       
+       @SuppressWarnings("rawtypes")
+       @Override
+       public boolean equals(Object obj) {
+               if (obj == null)
+                       return false;
+               if (obj.getClass() != this.getClass())
+                       return false;
+               VTKSelectionItem other = (VTKSelectionItem)obj;
+               if (prop != null)
+                       if (!prop.equals(other.prop))
+                           return false;
+               if (node != null)
+                       if (!node.equals(other.node))
+                           return false;
+               if (resource != null)
+                   return resource.equals(other.resource);
+               return true;
+                       
+       }
+}
index 82d592c4d4b8b8c0c95652b81b4d6819f72dd899..273e1062d15ae6e805fbf1507007000df30a6673 100644 (file)
@@ -171,12 +171,12 @@ public class Plant3DEditor extends ResourceEditorPart {
                                throw new RuntimeException("Scenegraph loading failed.");
                        populate();
                        
-                       selectionProvider = new NodeSelectionProvider2<Resource,Object>(this,mapping,nodeMap);
+                       selectionProvider = createSelectionProvider();
 
                        cameraAction.addSelectionChangedListener(selectionProvider);
 
-                       cameraAction.addHoverChangedListener(new HoverHighlighter(panel,nodeMap));
-                       selectionProvider.addSelectionChangedListener(new SelectionHighlighter(panel,nodeMap));
+                       cameraAction.addHoverChangedListener(createHoverHighlhighter());
+                       selectionProvider.addSelectionChangedListener(createSelectionHighlighter());
                        
                        getSite().setSelectionProvider(selectionProvider);
                        getSite().getPage().addPostSelectionListener(selectionProvider);
@@ -212,6 +212,18 @@ public class Plant3DEditor extends ResourceEditorPart {
            return new vtkCameraAndSelectorAction(panel); 
        }
        
+       protected NodeSelectionProvider2<Resource,Object> createSelectionProvider() {
+           return new NodeSelectionProvider2<Resource,Object>(this,mapping,nodeMap);
+       }
+       
+       protected HoverHighlighter createHoverHighlhighter() {
+           return new HoverHighlighter(panel,nodeMap);
+       }
+       
+       protected SelectionHighlighter<INode> createSelectionHighlighter() {
+           return new SelectionHighlighter<INode>(panel,nodeMap);
+       }
+       
        protected void createActions() {
                translateAction = new TranslateAction(panel,nodeMap);
                translateInlineAction = new TranslateInlineAction(panel, nodeMap);
@@ -437,6 +449,10 @@ public class Plant3DEditor extends ResourceEditorPart {
                return result;
        }
        
+       public InteractiveVtkComposite getPanel() {
+        return panel;
+    }
+       
        public P3DRootNode getRootNode() {
                return rootNode;
        }
@@ -444,4 +460,8 @@ public class Plant3DEditor extends ResourceEditorPart {
        public IMapping<Resource, Object> getMapping() {
                return mapping;
        }
+       
+       public P3DNodeMap getNodeMap() {
+        return nodeMap;
+    }
 }