From: Marko Luukkainen Date: Wed, 23 Oct 2019 11:34:31 +0000 (+0300) Subject: Overriding selection and hover highlights X-Git-Tag: v1.43.0~177 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=58b68ec134db6382f4c401ee7374a74d0aa6cd78;p=simantics%2F3d.git Overriding selection and hover highlights Additionally changed VTKSelectionItem equals to handle different Objects / Resources with the same vtkProp. gitlab #29 Change-Id: I305ff007bd48660d736b1abd01ca183075ba8b88 --- diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/NodeSelectionProvider2.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/NodeSelectionProvider2.java index 6d0a1842..5de69f35 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/NodeSelectionProvider2.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/NodeSelectionProvider2.java @@ -1,155 +1,155 @@ -/******************************************************************************* - * 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 implements ISelectionProvider, ISelectionChangedListener, ISelectionListener{ - private ISelection selection = new StructuredSelection(); - private List listeners = new ArrayList(); - - List selectedNodes = new ArrayList(); // selection is ordered - //List selectedResources = new ArrayList(); - List> selectedItems = new ArrayList>(); - - IEditorPart part; - IMapping mapping; - VTKNodeMap nodeMap; - - public NodeSelectionProvider2(IEditorPart part, IMapping 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") - private void processSelection(ISelection s) { - selectedNodes.clear(); - selectedItems.clear(); - - Collection 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(a, node,r)); - } - } else { - Collection 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(null, (IG3DNode)node,r)); - } - } - - - selection = new StructuredSelection(selectedItems); - } - - private 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 getSelectedNodes() { - return selectedNodes; - } - - @SuppressWarnings("unchecked") - public List getSelectedResources() { - - List list = new ArrayList(); - for (VTKSelectionItem 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); - } -} +/******************************************************************************* + * 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 implements ISelectionProvider, ISelectionChangedListener, ISelectionListener{ + protected ISelection selection = new StructuredSelection(); + protected List listeners = new ArrayList(); + + protected List selectedNodes = new ArrayList(); // selection is ordered + //List selectedResources = new ArrayList(); + protected List> selectedItems = new ArrayList>(); + + protected IEditorPart part; + protected IMapping mapping; + protected VTKNodeMap nodeMap; + + public NodeSelectionProvider2(IEditorPart part, IMapping 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 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(a, node,r)); + } + } else { + Collection 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(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 getSelectedNodes() { + return selectedNodes; + } + + @SuppressWarnings("unchecked") + public List getSelectedResources() { + + List list = new ArrayList(); + for (VTKSelectionItem 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); + } +} diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java index dec74211..789cfb18 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/SelectionHighlighter.java @@ -35,17 +35,17 @@ import vtk.vtkMapper; import vtk.vtkProp; import vtk.vtkProperty; -public class SelectionHighlighter implements ISelectionChangedListener{ +public class SelectionHighlighter implements ISelectionChangedListener{ - VtkView panel; - VTKNodeMap nodeMap; + protected VtkView panel; + protected VTKNodeMap nodeMap; - List selectedNodes = new ArrayList(); - List selectedActors = new ArrayList(); + protected List selectedNodes = new ArrayList(); + protected List selectedActors = new ArrayList(); - HighlightObjectType type = HighlightObjectType.Node; + protected HighlightObjectType type = HighlightObjectType.Node; public SelectionHighlighter(VtkView panel, VTKNodeMap nodeMap) { this.panel = panel; diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/VTKSelectionItem.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/VTKSelectionItem.java index d998801b..222da8b9 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/VTKSelectionItem.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/VTKSelectionItem.java @@ -1,106 +1,75 @@ -/******************************************************************************* - * 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 implements IAdaptable{ - - private vtkProp prop; - private IG3DNode node; - private DBObject resource; - - //private VTKpropSet actors; - - - public VTKSelectionItem(vtkProp prop, IG3DNode node, DBObject res) { - this.prop = prop; - this.node = node; - this.resource = res; - } - -// public VTKSelectionItem(Resource res, Collection actors) { -// this.resource = res; -// this.actors = new VTKpropSet(); -// this.actors.addAll(actors); -// } -// -// public VTKSelectionItem(Resource res, vtkProp... actors) { -// this.resource = res; -// this.actors = new VTKpropSet(); -// for (vtkProp a : actors) -// this.actors.add(a); -// } - - @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 Object getAdapter(Class adapter) { -// if (adapter == Resource.class) -// return resource; -// if (adapter == VTKpropSet.class) -// return actors; -// return null; -// } - -// public Resource getResource() { -// return resource; -// } -// -// public VTKpropSet getActors() { -// return actors; -// } - - @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) - return prop.equals(other.prop); - if (node != null) - return node.equals(other.node); - return resource.equals(other.resource); - //return resource.equals(other.resource); - - } -} +/******************************************************************************* + * 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 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; + + } +} diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java index 82d592c4..273e1062 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java @@ -171,12 +171,12 @@ public class Plant3DEditor extends ResourceEditorPart { throw new RuntimeException("Scenegraph loading failed."); populate(); - selectionProvider = new NodeSelectionProvider2(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 createSelectionProvider() { + return new NodeSelectionProvider2(this,mapping,nodeMap); + } + + protected HoverHighlighter createHoverHighlhighter() { + return new HoverHighlighter(panel,nodeMap); + } + + protected SelectionHighlighter createSelectionHighlighter() { + return new SelectionHighlighter(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 getMapping() { return mapping; } + + public P3DNodeMap getNodeMap() { + return nodeMap; + } }