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.SelectionChangedEvent;
\r
21 import org.eclipse.jface.viewers.StructuredSelection;
\r
22 import org.eclipse.swt.widgets.Composite;
\r
23 import org.simantics.g3d.scenegraph.IG3DNode;
\r
24 import org.simantics.g3d.scenegraph.base.INode;
\r
25 import org.simantics.g3d.scenegraph.base.ParentNode;
\r
26 import org.simantics.g3d.tools.AdaptationUtils;
\r
27 import org.simantics.g3d.ui.ScenegraphOutlinePage;
\r
29 public class VTKContentOutlinePage<DBObject, JavaObject extends INode> extends ScenegraphOutlinePage{
\r
30 private NodeSelectionProvider2<DBObject, JavaObject> provider;
\r
32 public VTKContentOutlinePage(ParentNode<? extends JavaObject> rootNode, NodeSelectionProvider2<DBObject, JavaObject> selectionProvider) {
\r
34 this.provider = selectionProvider;
\r
37 private boolean outsideSelection = false;
\r
40 public void createControl(Composite parent) {
\r
42 super.createControl(parent);
\r
44 provider.addSelectionChangedListener(new ISelectionChangedListener() {
\r
47 public void selectionChanged(SelectionChangedEvent event) {
\r
48 Object source = event.getSource();
\r
49 if (source == VTKContentOutlinePage.this)
\r
51 ISelection s = event.getSelection();
\r
52 Collection<INode> nodes = AdaptationUtils.adaptToCollection(s, INode.class);
\r
53 outsideSelection = true;
\r
54 getTreeViewer().setSelection(new StructuredSelection(nodes.toArray()),true);
\r
55 outsideSelection = false;
\r
61 List<INode> selectedNodes = new ArrayList<INode>();
\r
63 @SuppressWarnings("unchecked")
\r
65 protected void fireSelectionChanged(ISelection selection) {
\r
66 if (outsideSelection)
\r
68 // TreeViewer provides selected object in tree order, not in selected order.
\r
69 Collection<INode> selectedUnsortedNodes = AdaptationUtils.adaptToCollection(selection, INode.class);
\r
70 List<INode> toRemove = new ArrayList<INode>();
\r
71 for (INode node : selectedNodes) {
\r
72 if (!selectedUnsortedNodes.contains(node))
\r
75 for (INode node : toRemove)
\r
76 selectedNodes.remove(node);
\r
77 for (INode node : selectedUnsortedNodes) {
\r
78 if (!selectedNodes.contains(node))
\r
79 selectedNodes.add(node);
\r
82 List<VTKSelectionItem<DBObject>> selectedItems = new ArrayList<VTKSelectionItem<DBObject>>();
\r
83 for (INode node : selectedNodes) {
\r
84 DBObject r = provider.mapping.inverseGet((JavaObject)node);
\r
85 selectedItems.add(new VTKSelectionItem<DBObject>(null, (IG3DNode)node, r));
\r
88 super.fireSelectionChanged(new StructuredSelection(selectedItems));
\r