1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.vtk.common;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.List;
\r
18 import org.eclipse.jface.viewers.ISelection;
\r
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
\r
20 import org.eclipse.jface.viewers.ISelectionProvider;
\r
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
\r
22 import org.eclipse.jface.viewers.StructuredSelection;
\r
23 import org.eclipse.ui.IEditorPart;
\r
24 import org.eclipse.ui.ISelectionListener;
\r
25 import org.eclipse.ui.IWorkbenchPart;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.g3d.scenegraph.IG3DNode;
\r
28 import org.simantics.g3d.scenegraph.base.INode;
\r
29 import org.simantics.g3d.tools.AdaptationUtils;
\r
30 import org.simantics.objmap.graph.IMapping;
\r
31 import org.simantics.objmap.structural.StructuralResource;
\r
35 public class NodeSelectionProvider2<DBObject,JavaObject> implements ISelectionProvider, ISelectionChangedListener, ISelectionListener{
\r
36 private ISelection selection = new StructuredSelection();
\r
37 private List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();
\r
39 List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>(); // selection is ordered
\r
40 //List<Resource> selectedResources = new ArrayList<Resource>();
\r
41 List<VTKSelectionItem<DBObject>> selectedItems = new ArrayList<VTKSelectionItem<DBObject>>();
\r
44 IMapping<DBObject,JavaObject> mapping;
\r
47 public NodeSelectionProvider2(IEditorPart part, IMapping<DBObject,JavaObject> mapping, VTKNodeMap nodeMap) {
\r
49 this.mapping = mapping;
\r
50 this.nodeMap = nodeMap;
\r
54 public void addSelectionChangedListener(
\r
55 ISelectionChangedListener listener) {
\r
56 listeners.add(listener);
\r
60 public void removeSelectionChangedListener(
\r
61 ISelectionChangedListener listener) {
\r
62 listeners.remove(listener);
\r
66 public ISelection getSelection() {
\r
71 public void setSelection(ISelection selection) {
\r
75 // events coming from vtk
\r
77 public void selectionChanged(SelectionChangedEvent event) {
\r
78 ISelection s = event.getSelection();
\r
80 processSelection(s);
\r
81 fireSelectionChanged(event.getSource());
\r
84 @SuppressWarnings("unchecked")
\r
85 private void processSelection(ISelection s) {
\r
86 selectedNodes.clear();
\r
87 selectedItems.clear();
\r
89 Collection<vtkProp> selectedActors = AdaptationUtils.adaptToCollection(s, vtkProp.class);
\r
90 if (selectedActors.size() > 0) {
\r
91 for (vtkProp a : selectedActors) {
\r
92 IG3DNode node = (IG3DNode)nodeMap.getNode((vtkProp)a);
\r
95 if (!selectedNodes.contains(node))
\r
96 selectedNodes.add(node);
\r
97 DBObject r = mapping.inverseGet((JavaObject)node);
\r
98 selectedItems.add(new VTKSelectionItem<DBObject>(a, node,r));
\r
101 Collection<IG3DNode> selectedNds = AdaptationUtils.adaptToCollection(s, IG3DNode.class);
\r
102 for (INode node : selectedNds) {
\r
103 if (!selectedNodes.contains(node))
\r
104 selectedNodes.add((IG3DNode)node);
\r
105 DBObject r = mapping.inverseGet((JavaObject)node);
\r
106 selectedItems.add(new VTKSelectionItem<DBObject>(null, (IG3DNode)node,r));
\r
111 selection = new StructuredSelection(selectedItems);
\r
114 private void fireSelectionChanged(Object source) {
\r
115 SelectionChangedEvent evt = new SelectionChangedEvent((ISelectionProvider)source, selection);
\r
116 for (ISelectionChangedListener l : listeners) {
\r
117 l.selectionChanged(evt);
\r
123 // events coming from workbench
\r
125 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
\r
126 if (part == this.part)
\r
128 processSelection(selection);
\r
132 public List<IG3DNode> getSelectedNodes() {
\r
133 return selectedNodes;
\r
136 @SuppressWarnings("unchecked")
\r
137 public List<DBObject> getSelectedResources() {
\r
139 List<DBObject> list = new ArrayList<DBObject>();
\r
140 for (VTKSelectionItem<DBObject> i : selectedItems) {
\r
141 DBObject r = (DBObject)i.getAdapter(Resource.class);
\r
143 r = (DBObject)i.getAdapter(StructuralResource.class);
\r
146 if (!list.contains(r))
\r
152 protected INode getNode(DBObject r) {
\r
153 return (INode)mapping.get(r);
\r