1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.common;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
21 import java.util.Stack;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.g3d.ontology.G3D;
31 import org.simantics.g3d.scenegraph.G3DNode;
32 import org.simantics.g3d.scenegraph.IG3DNode;
33 import org.simantics.g3d.scenegraph.RenderListener;
34 import org.simantics.g3d.scenegraph.base.INode;
35 import org.simantics.g3d.scenegraph.base.NodeListener;
36 import org.simantics.g3d.scenegraph.base.ParentNode;
37 import org.simantics.objmap.graph.IMapping;
38 import org.simantics.objmap.graph.IMappingListener;
39 import org.simantics.utils.datastructures.Callback;
40 import org.simantics.utils.datastructures.MapList;
41 import org.simantics.utils.datastructures.MapSet;
42 import org.simantics.utils.datastructures.Pair;
43 import org.simantics.utils.ui.ExceptionUtils;
47 public abstract class AbstractVTKNodeMap<E extends IG3DNode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener{
49 private static final boolean DEBUG = false;
51 protected Session session;
52 protected IMapping<Object,E> mapping;
53 protected InteractiveVtkPanel panel;
55 protected MapList<E, vtkProp> nodeToActor = new MapList<E, vtkProp>();
56 protected Map<vtkProp,E> actorToNode = new HashMap<vtkProp, E>();
58 protected ParentNode<E> rootNode;
60 public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, InteractiveVtkPanel panel, ParentNode<E> rootNode) {
61 this.session = session;
62 this.mapping = mapping;
64 this.rootNode = rootNode;
65 panel.addListener(this);
66 mapping.addMappingListener(this);
67 rootNode.addListener(this);
71 protected abstract void addActor(E node);
72 protected abstract void removeActor(E node);
73 protected abstract void updateActor(E node,Set<String> ids);
75 public void repaint() {
79 public void populate() {
80 for (E node : rootNode.getNodes()) {
81 receiveAdd(node, node.getParentRel(),true);
87 public E getNode(vtkProp prop) {
88 return actorToNode.get(prop);
91 @SuppressWarnings("unchecked")
93 public Collection<vtkProp> getRenderObjects(IG3DNode node) {
94 return nodeToActor.getValues((E)node);
97 @SuppressWarnings("unchecked")
99 public ParentNode<E> getRootNode() {
100 return (ParentNode<E>)rootNode;
106 public boolean isChangeTracking() {
107 return changeTracking;
111 public void setChangeTracking(boolean enabled) {
112 changeTracking = enabled;
115 private boolean changeTracking = true;
117 protected Object syncMutex = new Object();
120 private List<Pair<E,String>> added = new ArrayList<Pair<E,String>>();
121 private List<Pair<E,String>> removed = new ArrayList<Pair<E,String>>();
122 //private List<Pair<E,String>> updated = new ArrayList<Pair<E,String>>();
123 private MapSet<E, String> updated = new MapSet.Hash<E, String>();
125 private boolean rangeModified = false;
127 @SuppressWarnings("unchecked")
129 public void updateRenderObjectsFor(IG3DNode node) {
130 List<vtkProp> toDelete = new ArrayList<vtkProp>();
131 for (vtkProp prop : nodeToActor.getValues((E)node)) {
132 if (prop.GetVTKId() != 0) {
133 panel.GetRenderer().RemoveActor(prop);
137 actorToNode.remove(prop);
139 nodeToActor.remove((E)node);
140 Collection<vtkProp> coll = getActors((E)node);
143 for (vtkProp prop : coll) {
144 nodeToActor.add((E)node,prop);
145 actorToNode.put(prop, (E)node);
146 toDelete.remove(prop);
148 for (vtkProp p : toDelete)
152 protected abstract Collection<vtkProp> getActors(E node);
154 @SuppressWarnings("unchecked")
155 private void receiveAdd(E node, String id, boolean db) {
156 if (DEBUG) System.out.println("receiveAdd " + node + " " + id + " " + db);
157 synchronized (syncMutex) {
158 for (Pair<E, String> n : added) {
159 if (n.first.equals(node))
162 if (changeTracking) {
163 mapping.rangeModified((E)node.getParent());
165 added.add(new Pair<E, String>(node, id));
166 rangeModified = true;
171 @SuppressWarnings("unchecked")
172 private void receiveRemove(E node, String id, boolean db) {
173 if (DEBUG) System.out.println("receiveRemove " + node + " " + id + " " + db);
174 synchronized (syncMutex) {
175 for (Pair<E, String> n : removed) {
176 if (n.first.equals(node))
179 if (changeTracking && !db)
180 mapping.rangeModified((E)node.getParent());
181 removed.add(new Pair<E, String>(node, id));
182 rangeModified = true;
187 @SuppressWarnings("unchecked")
188 private void receiveUpdate(E node, String id, boolean db) {
189 if (DEBUG) System.out.println("receiveUpdate " + node + " " + id + " " + db);
190 synchronized (syncMutex) {
191 // for (Pair<E, String> n : updated) {
192 // if (n.first.equals(node))
195 if (changeTracking && !db)
196 mapping.rangeModified(node);
197 //updated.add(new Pair<E, String>(node, id));
198 updated.add(node, id);
199 rangeModified = true;
204 private boolean graphUpdates = false;
205 private Set<E> graphModified = new HashSet<E>();
207 private boolean requestCommit = false;
210 public void commit() {
211 requestCommit = true;
214 protected void doCommit() {
215 session.asyncRequest(new WriteRequest() {
218 public void perform(WriteGraph graph) throws DatabaseException {
222 }, new Callback<DatabaseException>() {
225 public void run(DatabaseException parameter) {
226 if (parameter != null)
227 ExceptionUtils.logAndShowError("Cannot commit editor changes", parameter);
232 protected void commit(WriteGraph graph) throws DatabaseException {
233 synchronized(syncMutex) {
234 if (DEBUG) System.out.println("Commit");
236 mapping.updateDomain(graph);
237 graphUpdates = false;
242 public void domainModified() {
245 if (DEBUG)System.out.println("domainModified");
246 session.asyncRequest(new ReadRequest() {
248 @SuppressWarnings("unchecked")
250 public void run(ReadGraph graph) throws DatabaseException {
257 protected void update(ReadGraph graph) throws DatabaseException {
258 synchronized (syncMutex) {
260 for (Object domainObject : mapping.getDomainModified()) {
261 E rangeObject = mapping.get(domainObject);
262 if (rangeObject != null)
263 graphModified.add(rangeObject);
265 mapping.updateRange(graph);
266 graphModified.clear();
267 graphUpdates = false;
270 if (mapping.isRangeModified())
275 public void rangeModified() {
276 //System.out.println("rangeModified");
281 public void postRender() {
283 // 1. Commit has been requested
284 // 2. There are no pending changes that should be processed in preRender()
285 if (requestCommit && !rangeModified) { // FIXME : not thread safe.
286 requestCommit = false;
291 List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>();
292 List<Pair<E, String>> add = new ArrayList<Pair<E,String>>();
293 MapSet<E, String> mod = new MapSet.Hash<E, String>();
294 Set<E> propagation = new HashSet<E>();
295 Stack<E> stack = new Stack<E>();
299 public synchronized void preRender() {
303 @SuppressWarnings("unchecked")
304 protected void updateCycle() {
310 synchronized (syncMutex) {
313 for (E e : updated.getKeys()) {
314 for (String s : updated.getValues(e)) {
324 for (Pair<E, String> n : rem) {
325 stopListening(n.first);
326 removeActor(n.first);
330 for (Pair<E, String> n : add) {
335 for (E e : mod.getKeys()) {
336 Set<String> ids = mod.getValues(e);
337 if (ids.contains(G3D.URIs.hasPosition) || ids.contains(G3D.URIs.hasOrientation)) {
338 if (!propagation.contains(e))
343 if (propagation.size() > 0) {
345 stack.addAll(propagation);
347 while (!stack.isEmpty()) {
348 E node = stack.pop();
349 if (propagation.contains(node))
351 propagation.add(node);
352 for (NodeListener l : node.getListeners()) {
354 //changeTracking = false;
355 //l.propertyChanged(node, G3D.URIs.hasPosition);
356 //changeTracking = true;
358 l.propertyChanged(node, G3D.URIs.hasWorldPosition);
361 if (node instanceof ParentNode) {
362 stack.addAll(((ParentNode<E>)node).getNodes());
367 // synchronized (syncMutex) {
368 // rem.addAll(removed);
369 // add.addAll(added);
370 // //mod.addAll(updated);
371 // for (E e : updated.getKeys()) {
372 // for (String s : updated.getValues(e))
381 for (E e : mod.getKeys()) {
382 Set<String> ids = mod.getValues(e);
387 for (Pair<E, String> n : rem) {
388 for (NodeListener l : nodeListeners)
389 l.nodeRemoved(null, n.first, n.second);
391 for (Pair<E, String> n : add) {
392 for (NodeListener l : nodeListeners)
393 l.nodeAdded(n.first.getParent(), n.first, n.second);
395 // for (Pair<E, String> n : mod) {
396 // for (NodeListener l : nodeListeners)
397 // l.propertyChanged(n.first, n.second);
399 for (E e : mod.getKeys()) {
400 for (NodeListener l : nodeListeners)
401 for (String s : mod.getValues(e))
402 l.propertyChanged(e, s);
404 synchronized (syncMutex) {
405 if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
406 rangeModified = false;
410 @SuppressWarnings("unchecked")
411 private void listen(INode node) {
412 node.addListener(this);
413 if (node instanceof ParentNode<?>) {
414 ParentNode<INode> parentNode = (ParentNode<INode>)node;
415 for (INode n : parentNode.getNodes())
420 private void stopListening(INode node) {
421 node.removeListener(this);
422 if (node instanceof ParentNode<?>) {
423 @SuppressWarnings("unchecked")
424 ParentNode<INode> parentNode = (ParentNode<INode>)node;
425 for (INode n : parentNode.getNodes())
430 @SuppressWarnings("unchecked")
432 public void propertyChanged(INode node, String id) {
433 //receiveUpdate((E)node, id, graphUpdates);
434 receiveUpdate((E)node, id, graphModified.contains(node));
438 @SuppressWarnings("unchecked")
440 public <T extends INode> void nodeAdded(ParentNode<T> node, INode child,
442 if (DEBUG) System.out.println("Node added " + child + " parent " + node);
443 //receiveAdd((E)child, rel ,graphUpdates);
444 receiveAdd((E)child, rel ,graphModified.contains(node));
448 @SuppressWarnings("unchecked")
450 public <T extends INode> void nodeRemoved(ParentNode<T> node, INode child,
452 if (DEBUG) System.out.println("Node removed " + child + " parent " + node);
453 //receiveRemove((E)child, rel, graphUpdates);
454 receiveRemove((E)child, rel, graphModified.contains(node));
456 //FIXME : sometimes removed structural models cause ObjMap to add their children again.
457 // removing the listener here prevents corruption of visual model, but better fix is needed.
458 stopListening(child);
462 public void delete() {
463 changeTracking = false;
464 panel.removeListener(this);
465 mapping.removeMappingListener(this);
467 List<E> nodes = new ArrayList<E>(nodeToActor.getKeySize());
468 nodes.addAll(nodeToActor.getKeys());
469 for (E node : nodes) {
470 node.removeListener(this);
474 for (vtkProp prop : actorToNode.keySet()) {
475 if (prop.GetVTKId() != 0)
484 private List<NodeListener> nodeListeners = new ArrayList<NodeListener>();
486 public void addListener(NodeListener listener) {
487 nodeListeners.add(listener);
492 public void removeListener(NodeListener listener) {
493 nodeListeners.remove(listener);
497 public IMapping<Object,E> getMapping() {