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.ArrayDeque;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Deque;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
24 import java.util.Stack;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.Session;
28 import org.simantics.db.UndoContext;
29 import org.simantics.db.WriteGraph;
30 import org.simantics.db.common.request.ReadRequest;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.util.Layer0Utils;
34 import org.simantics.db.service.UndoRedoSupport;
35 import org.simantics.g3d.ontology.G3D;
36 import org.simantics.g3d.scenegraph.RenderListener;
37 import org.simantics.g3d.scenegraph.base.INode;
38 import org.simantics.g3d.scenegraph.base.NodeListener;
39 import org.simantics.g3d.scenegraph.base.ParentNode;
40 import org.simantics.objmap.exceptions.MappingException;
41 import org.simantics.objmap.graph.IMapping;
42 import org.simantics.objmap.graph.IMappingListener;
43 import org.simantics.utils.datastructures.Callback;
44 import org.simantics.utils.datastructures.MapList;
45 import org.simantics.utils.datastructures.MapSet;
46 import org.simantics.utils.datastructures.Pair;
47 import org.simantics.utils.ui.ExceptionUtils;
51 public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{
53 private static final boolean DEBUG = false;
55 protected Session session;
56 protected IMapping<Object,E> mapping;
57 protected VtkView view;
59 protected MapList<E, vtkProp> nodeToActor = new MapList<E, vtkProp>();
60 protected Map<vtkProp,E> actorToNode = new HashMap<vtkProp, E>();
62 protected ParentNode<E> rootNode;
64 protected UndoRedoSupport undoRedoSupport;
65 protected int undoOpCount = 0;
66 protected int redoOpCount = 0;
67 protected boolean runUndo = false;
68 protected boolean runRedo = false;
69 public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, VtkView view, ParentNode<E> rootNode) {
70 this.session = session;
71 this.mapping = mapping;
73 this.rootNode = rootNode;
74 view.addListener(this);
75 mapping.addMappingListener(this);
76 rootNode.addListener(this);
78 undoRedoSupport = session.getService(UndoRedoSupport.class);
79 undoRedoSupport.subscribe(this);
81 UndoContext undoContext = undoRedoSupport.getUndoContext(session);
82 undoOpCount = undoContext.getAll().size();
83 redoOpCount = undoContext.getRedoList().size();
84 } catch(DatabaseException e) {
91 protected abstract void addActor(E node);
92 protected abstract void removeActor(E node);
93 protected abstract void updateActor(E node,Set<String> ids);
95 public void repaint() {
99 public void populate() {
100 for (E node : rootNode.getNodes()) {
101 receiveAdd(node, node.getParentRel(),true);
107 public E getNode(vtkProp prop) {
108 return actorToNode.get(prop);
111 @SuppressWarnings("unchecked")
113 public Collection<vtkProp> getRenderObjects(INode node) {
114 return nodeToActor.getValues((E)node);
117 @SuppressWarnings("unchecked")
119 public ParentNode<E> getRootNode() {
120 return (ParentNode<E>)rootNode;
126 public boolean isChangeTracking() {
127 return changeTracking;
131 public void setChangeTracking(boolean enabled) {
132 changeTracking = enabled;
135 private boolean changeTracking = true;
137 protected Object syncMutex = new Object();
140 private List<Pair<E,String>> added = new ArrayList<Pair<E,String>>();
141 private List<Pair<E,String>> removed = new ArrayList<Pair<E,String>>();
142 private MapSet<E, String> updated = new MapSet.Hash<E, String>();
144 private boolean rangeModified = false;
147 public void onChanged() {
149 UndoContext undoContext = undoRedoSupport.getUndoContext(session);
150 int ucount = undoContext.getAll().size();
151 int rcount = undoContext.getRedoList().size();
152 if (DEBUG) System.out.println("Previous U:" + undoOpCount +" R:" + redoOpCount +" Current U:"+ucount+" R:"+rcount);
153 if (ucount < undoOpCount) {
158 if (!runUndo && rcount > 0)
162 undoOpCount = ucount;
163 redoOpCount = rcount;
165 if (DEBUG) System.out.println("Undo " + runUndo + " Redo " + runRedo);
166 } catch (DatabaseException e) {
167 // TODO Auto-generated catch block
174 @SuppressWarnings("unchecked")
176 public void updateRenderObjectsFor(E node) {
177 List<vtkProp> toDelete = new ArrayList<vtkProp>();
179 for (vtkProp prop : nodeToActor.getValues((E)node)) {
180 if (prop.GetVTKId() != 0) {
181 view.getRenderer().RemoveActor(prop);
185 actorToNode.remove(prop);
188 nodeToActor.remove((E)node);
189 Collection<vtkProp> coll = getActors((E)node);
191 for (vtkProp prop : coll) {
192 nodeToActor.add((E)node,prop);
193 actorToNode.put(prop, (E)node);
194 toDelete.remove(prop);
197 for (vtkProp p : toDelete)
201 protected abstract Collection<vtkProp> getActors(E node);
203 @SuppressWarnings("unchecked")
204 private void receiveAdd(E node, String id, boolean db) {
205 if (DEBUG) System.out.println("receiveAdd " + debugString(node) + " " + id + " " + db);
206 synchronized (syncMutex) {
207 for (Pair<E, String> n : added) {
208 if (n.first.equals(node))
211 if (changeTracking) {
212 mapping.rangeModified((E)node.getParent());
214 added.add(new Pair<E, String>(node, id));
215 rangeModified = true;
220 @SuppressWarnings("unchecked")
221 private void receiveRemove(E node, String id, boolean db) {
222 if (DEBUG) System.out.println("receiveRemove " + debugString(node) + " " + id + " " + db);
223 synchronized (syncMutex) {
224 for (Pair<E, String> n : removed) {
225 if (n.first.equals(node))
228 if (changeTracking && !db)
229 mapping.rangeModified((E)node.getParent());
230 removed.add(new Pair<E, String>(node, id));
231 rangeModified = true;
236 @SuppressWarnings("unchecked")
237 private void receiveUpdate(E node, String id, boolean db) {
238 if (DEBUG) System.out.println("receiveUpdate " + debugString(node) + " " + id + " " + db);
239 synchronized (syncMutex) {
240 // for (Pair<E, String> n : updated) {
241 // if (n.first.equals(node))
244 if (changeTracking && !db)
245 mapping.rangeModified(node);
246 //updated.add(new Pair<E, String>(node, id));
247 updated.add(node, id);
248 rangeModified = true;
253 private boolean graphUpdates = false;
254 private Set<E> graphModified = new HashSet<E>();
256 private boolean requestCommit = false;
257 private String commitMessage = null;
260 public void commit(String message) {
261 requestCommit = true;
262 commitMessage = message;
265 protected void doCommit() {
266 session.asyncRequest(new WriteRequest() {
269 public void perform(WriteGraph graph) throws DatabaseException {
270 if (DEBUG) System.out.println("Commit " + commitMessage);
271 if (commitMessage != null) {
272 Layer0Utils.addCommentMetadata(graph, commitMessage);
273 graph.markUndoPoint();
274 commitMessage = null;
279 }, new Callback<DatabaseException>() {
282 public void run(DatabaseException parameter) {
283 if (parameter != null)
284 ExceptionUtils.logAndShowError("Cannot commit editor changes", parameter);
289 protected void commit(WriteGraph graph) throws DatabaseException {
290 synchronized(syncMutex) {
291 if (DEBUG) System.out.println("Commit");
293 mapping.updateDomain(graph);
294 graphUpdates = false;
296 if (DEBUG) System.out.println("Commit done");
303 public void domainModified() {
306 if (DEBUG)System.out.println("domainModified");
307 session.asyncRequest(new ReadRequest() {
309 @SuppressWarnings("unchecked")
311 public void run(ReadGraph graph) throws DatabaseException {
318 protected void reset(ReadGraph graph) throws MappingException {
319 if (DEBUG) System.out.println("Reset");
320 synchronized (syncMutex) {
322 mapping.getRangeModified().clear();
323 for (Object o : mapping.getDomain())
324 mapping.domainModified(o);
325 mapping.updateRange(graph);
326 graphModified.clear();
327 graphUpdates = false;
331 private boolean useFullSyncWithUndo = false;
333 protected void update(ReadGraph graph) throws DatabaseException {
334 if (DEBUG) System.out.println("Graph update start");
335 if (runUndo && useFullSyncWithUndo) {
338 synchronized (syncMutex) {
340 for (Object domainObject : mapping.getDomainModified()) {
341 E rangeObject = mapping.get(domainObject);
342 if (rangeObject != null)
343 graphModified.add(rangeObject);
345 mapping.updateRange(graph);
346 graphModified.clear();
349 graphUpdates = false;
353 if (mapping.isRangeModified() && !runUndo && !runRedo)
354 commit((String)null);
355 if (DEBUG) System.out.println("Graph update done");
359 public void rangeModified() {
360 //System.out.println("rangeModified");
365 public void postRender() {
367 // 1. Commit has been requested
368 // 2. There are no pending changes that should be processed in preRender()
369 if (requestCommit && !rangeModified) { // FIXME : not thread safe.
370 requestCommit = false;
375 // Reusable containers for data synchronisation
376 List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>(); // Removed objects
377 List<Pair<E, String>> add = new ArrayList<Pair<E,String>>(); // Added objects
378 MapSet<E, String> mod = new MapSet.Hash<E, String>(); // Modified objects
379 Set<E> propagation = new HashSet<E>(); // Objects with propagated changes
380 Stack<E> stack = new Stack<E>(); // Stack for handling propagation
381 Set<E> delete = Collections.synchronizedSet(new HashSet<E>()); // Objects to be completely deleted
382 Set<E> deleteUC = new HashSet<E>();
385 public synchronized void preRender() {
391 * When objects are removed (either from Java or Graph), after remove processing the Java objects remain in mapping cache.
392 * This causes problems with Undo and Redo, whcih the end up re-using the removed objects from mapping cache.
394 * This code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added).
397 protected void syncDeletes() {
399 for (Pair<E, String> n : removed) {
400 deleteUC.add(n.first);
402 for (Pair<E, String> n : added) {
403 deleteUC.remove(n.first);
405 if (DEBUG && deleteUC.size() > 0) {
406 System.out.println("Delete sync");
408 System.out.println(debugString(n));
411 delete.addAll(deleteUC);
416 * Clears deletable objects from mapping cache.
418 protected void clearDeletes() {
419 if (DEBUG && delete.size() > 0) System.out.println("Delete");
421 if (DEBUG) System.out.println(debugString(n));
422 mapping.getRange().remove(n);
427 protected String debugString(E n) {
428 return n + "@" + Integer.toHexString(n.hashCode());
431 @SuppressWarnings("unchecked")
432 protected void updateCycle() {
439 synchronized (syncMutex) {
440 // Check for overlapping additions and deletions, prevent deleting objects that are also added.
441 Deque<E> stack = new ArrayDeque<E>();
442 for (Pair<E, String> n : added) {
445 while (!stack.isEmpty()) {
447 for (int i = removed.size()-1; i >= 0; i--) {
448 if (removed.get(i).first == n) {
453 if (n instanceof ParentNode) {
454 ParentNode<INode> pn = (ParentNode<INode>)n;
455 for (INode cn : pn.getNodes()) {
463 for (E e : updated.getKeys()) {
464 for (String s : updated.getValues(e)) {
476 for (Pair<E, String> n : rem) {
477 stopListening(n.first);
478 removeActor(n.first);
481 for (Pair<E, String> n : add) {
486 for (E e : mod.getKeys()) {
487 Set<String> ids = mod.getValues(e);
488 if (ids.contains(G3D.URIs.hasPosition) || ids.contains(G3D.URIs.hasOrientation)) {
489 if (!propagation.contains(e))
494 if (propagation.size() > 0) {
496 stack.addAll(propagation);
498 while (!stack.isEmpty()) {
499 E node = stack.pop();
500 if (propagation.contains(node))
502 propagation.add(node);
503 for (NodeListener l : node.getListeners()) {
505 //changeTracking = false;
506 //l.propertyChanged(node, G3D.URIs.hasPosition);
507 //changeTracking = true;
509 l.propertyChanged(node, G3D.URIs.hasWorldPosition);
512 if (node instanceof ParentNode) {
513 stack.addAll(((ParentNode<E>)node).getNodes());
518 // synchronized (syncMutex) {
519 // rem.addAll(removed);
520 // add.addAll(added);
521 // //mod.addAll(updated);
522 // for (E e : updated.getKeys()) {
523 // for (String s : updated.getValues(e))
532 for (E e : mod.getKeys()) {
533 Set<String> ids = mod.getValues(e);
538 for (Pair<E, String> n : rem) {
539 for (NodeListener l : nodeListeners)
540 l.nodeRemoved(null, n.first, n.second);
542 for (Pair<E, String> n : add) {
543 for (NodeListener l : nodeListeners)
544 l.nodeAdded(n.first.getParent(), n.first, n.second);
546 // for (Pair<E, String> n : mod) {
547 // for (NodeListener l : nodeListeners)
548 // l.propertyChanged(n.first, n.second);
550 for (E e : mod.getKeys()) {
551 for (NodeListener l : nodeListeners)
552 for (String s : mod.getValues(e))
553 l.propertyChanged(e, s);
556 synchronized (syncMutex) {
557 if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
558 rangeModified = false;
562 @SuppressWarnings("unchecked")
563 private void listen(INode node) {
564 node.addListener(this);
565 if (node instanceof ParentNode<?>) {
566 ParentNode<INode> parentNode = (ParentNode<INode>)node;
567 for (INode n : parentNode.getNodes())
572 private void stopListening(INode node) {
573 node.removeListener(this);
574 if (node instanceof ParentNode<?>) {
575 @SuppressWarnings("unchecked")
576 ParentNode<INode> parentNode = (ParentNode<INode>)node;
577 for (INode n : parentNode.getNodes())
582 @SuppressWarnings("unchecked")
584 public void propertyChanged(INode node, String id) {
585 //receiveUpdate((E)node, id, graphUpdates);
586 receiveUpdate((E)node, id, graphModified.contains(node));
590 @SuppressWarnings("unchecked")
592 public <T extends INode> void nodeAdded(ParentNode<T> node, INode child,
594 if (DEBUG) System.out.println("Node added " + child + " parent " + node);
595 //receiveAdd((E)child, rel ,graphUpdates);
596 receiveAdd((E)child, rel ,graphModified.contains(node));
600 @SuppressWarnings("unchecked")
602 public <T extends INode> void nodeRemoved(ParentNode<T> node, INode child,
604 if (DEBUG) System.out.println("Node removed " + child + " parent " + node);
605 //receiveRemove((E)child, rel, graphUpdates);
606 receiveRemove((E)child, rel, graphModified.contains(node));
608 //FIXME : sometimes removed structural models cause ObjMap to add their children again.
609 // removing the listener here prevents corruption of visual model, but better fix is needed.
610 stopListening(child);
614 public void delete() {
615 if (undoRedoSupport != null)
616 undoRedoSupport.cancel(this);
618 changeTracking = false;
619 view.removeListener(this);
620 mapping.removeMappingListener(this);
622 List<E> nodes = new ArrayList<E>(nodeToActor.getKeySize());
623 nodes.addAll(nodeToActor.getKeys());
624 for (E node : nodes) {
625 node.removeListener(this);
629 for (vtkProp prop : actorToNode.keySet()) {
630 if (prop.GetVTKId() != 0)
639 private List<NodeListener> nodeListeners = new ArrayList<NodeListener>();
641 public void addListener(NodeListener listener) {
642 nodeListeners.add(listener);
647 public void removeListener(NodeListener listener) {
648 nodeListeners.remove(listener);
652 public IMapping<Object,E> getMapping() {