]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java
Allow multiple radii for turns.
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / common / AbstractVTKNodeMap.java
index 646fbe41cdef8300e24e71ea84eba13510995e35..2c37521b8367b751451d3a73c5b02cbca60977cd 100644 (file)
  *******************************************************************************/
 package org.simantics.g3d.vtk.common;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -22,15 +25,19 @@ import java.util.Stack;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Session;
+import org.simantics.db.UndoContext;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.service.UndoRedoSupport;
 import org.simantics.g3d.ontology.G3D;
 import org.simantics.g3d.scenegraph.RenderListener;
 import org.simantics.g3d.scenegraph.base.INode;
 import org.simantics.g3d.scenegraph.base.NodeListener;
 import org.simantics.g3d.scenegraph.base.ParentNode;
+import org.simantics.objmap.exceptions.MappingException;
 import org.simantics.objmap.graph.IMapping;
 import org.simantics.objmap.graph.IMappingListener;
 import org.simantics.utils.datastructures.Callback;
@@ -41,20 +48,25 @@ import org.simantics.utils.ui.ExceptionUtils;
 
 import vtk.vtkProp;
 
-public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener{
+public abstract class AbstractVTKNodeMap<DBObject,E extends INode> implements VTKNodeMap<DBObject,E>, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{
 
        private static final boolean DEBUG = false;
        
        protected Session session;
-       protected IMapping<Object,E> mapping;
+       protected IMapping<DBObject,E> mapping;
        protected VtkView view;
        
-       protected MapList<E, vtkProp> nodeToActor = new MapList<E, vtkProp>();
-       protected Map<vtkProp,E> actorToNode = new HashMap<vtkProp, E>();
+       private MapList<E, vtkProp> nodeToActor = new MapList<E, vtkProp>();
+       private Map<vtkProp,E> actorToNode = new HashMap<vtkProp, E>();
 
        protected ParentNode<E> rootNode;
        
-       public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, VtkView view, ParentNode<E> rootNode) {
+       protected UndoRedoSupport undoRedoSupport;
+       protected int undoOpCount = 0;
+       protected int redoOpCount = 0;
+       protected boolean runUndo = false;
+       protected boolean runRedo = false;
+       public AbstractVTKNodeMap(Session session, IMapping<DBObject,E> mapping, VtkView view, ParentNode<E> rootNode) {
                this.session = session;
                this.mapping = mapping;
                this.view = view;
@@ -62,9 +74,18 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                view.addListener(this);
                mapping.addMappingListener(this);
                rootNode.addListener(this);
+               
+               undoRedoSupport = session.getService(UndoRedoSupport.class);
+               undoRedoSupport.subscribe(this);
+               try {
+                       UndoContext undoContext = undoRedoSupport.getUndoContext(session); 
+                       undoOpCount = undoContext.getAll().size();
+                       redoOpCount = undoContext.getRedoList().size();
+               } catch(DatabaseException e) {
+                       e.printStackTrace();
+               }
        }
-       
-       
+
        protected abstract void addActor(E node);
        protected abstract void removeActor(E node);
        protected abstract void updateActor(E node,Set<String> ids);
@@ -91,6 +112,21 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                return nodeToActor.getValues((E)node);
        }
        
+       protected <T extends vtkProp> void map(E node, Collection<T> props) {
+           for (vtkProp p : props) {
+               nodeToActor.add(node, p);
+               actorToNode.put(p, node);
+           }
+       }
+       
+       protected void removeMap(E node) {
+           Collection<vtkProp> coll = nodeToActor.getValuesUnsafe(node);
+           for (vtkProp p : coll) {
+               actorToNode.remove(p);
+           }
+           nodeToActor.remove(node);
+       }
+       
        @SuppressWarnings("unchecked")
        @Override
        public ParentNode<E> getRootNode() {
@@ -116,14 +152,45 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
 
        private List<Pair<E,String>> added = new ArrayList<Pair<E,String>>();
        private List<Pair<E,String>> removed = new ArrayList<Pair<E,String>>();
-       //private List<Pair<E,String>> updated = new ArrayList<Pair<E,String>>();
        private MapSet<E, String> updated = new MapSet.Hash<E, String>();
 
        private boolean rangeModified = false;
        
+       public boolean isRangeModified() {
+        return rangeModified;
+    }
+       
+       @Override
+       public void onChanged() {
+               try {
+                       UndoContext undoContext = undoRedoSupport.getUndoContext(session);
+                       int ucount = undoContext.getAll().size();
+                       int rcount = undoContext.getRedoList().size();
+                       if (DEBUG) System.out.println("Previous U:" + undoOpCount +" R:" + redoOpCount +" Current U:"+ucount+" R:"+rcount);
+                       if (ucount < undoOpCount) {
+                               runUndo = true;
+                       } else {
+                               runUndo = false;
+                       }
+                       if (!runUndo && rcount > 0)
+                               runRedo = true;
+                       else
+                               runRedo = false;
+                       undoOpCount = ucount;
+                       redoOpCount = rcount;
+                       
+                       if (DEBUG) System.out.println("Undo " + runUndo + " Redo " + runRedo);
+               } catch (DatabaseException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               
+               
+       }
+       
        @SuppressWarnings("unchecked")
        @Override
-       public void updateRenderObjectsFor(INode node) {
+       public void updateRenderObjectsFor(E node) {
                List<vtkProp> toDelete = new ArrayList<vtkProp>();
                view.lock();
                for (vtkProp prop : nodeToActor.getValues((E)node)) {
@@ -137,12 +204,12 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                view.unlock();
                nodeToActor.remove((E)node);
                Collection<vtkProp> coll = getActors((E)node);
-               if (coll == null)
-                       return;
-               for (vtkProp prop : coll) {
-                       nodeToActor.add((E)node,prop);
-                       actorToNode.put(prop, (E)node);
-                       toDelete.remove(prop);
+               if (coll != null) {
+                       for (vtkProp prop : coll) {
+                               nodeToActor.add((E)node,prop);
+                               actorToNode.put(prop, (E)node);
+                               toDelete.remove(prop);
+                       }
                }
                for (vtkProp p : toDelete)
                        p.Delete();
@@ -152,7 +219,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        @SuppressWarnings("unchecked")
        private void receiveAdd(E node, String id, boolean db) {
-               if (DEBUG) System.out.println("receiveAdd " + node  + " " + id + " " + db);
+               if (DEBUG) System.out.println("receiveAdd " + debugString(node)  + " " + id + " " + db);
                synchronized (syncMutex) {
                        for (Pair<E, String> n : added) {
                                if (n.first.equals(node))
@@ -164,12 +231,12 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        added.add(new Pair<E, String>(node, id));
                        rangeModified = true;
                }
-               view.refresh();
+               repaint();
        }
        
        @SuppressWarnings("unchecked")
        private void receiveRemove(E node, String id, boolean db) {
-               if (DEBUG) System.out.println("receiveRemove " + node  + " " + id + " " + db);
+               if (DEBUG) System.out.println("receiveRemove " + debugString(node)  + " " + id + " " + db);
                synchronized (syncMutex) {
                        for (Pair<E, String> n : removed) {
                                if (n.first.equals(node))
@@ -180,34 +247,36 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        removed.add(new Pair<E, String>(node, id));
                        rangeModified = true;
                }
-               view.refresh();
+               repaint();
        }
        
        @SuppressWarnings("unchecked")
        private void receiveUpdate(E node, String id, boolean db) {
-               if (DEBUG) System.out.println("receiveUpdate " + node  + " " + id + " " + db);
+               if (DEBUG) System.out.println("receiveUpdate " + debugString(node)  + " " + id + " " + db);
                synchronized (syncMutex) {
-//                     for (Pair<E, String> n : updated) {
-//                             if (n.first.equals(node))
-//                                     return;
-//                     }
+//          for (Pair<E, String> n : updated) {
+//              if (n.first.equals(node))
+//                  return;
+//          }
                        if (changeTracking && !db)
                                mapping.rangeModified(node);
                        //updated.add(new Pair<E, String>(node, id));
                        updated.add(node, id);
                        rangeModified = true;
                }
-               view.refresh();
+               repaint();
        }
        
        private boolean graphUpdates = false;
        private Set<E> graphModified = new HashSet<E>();
        
        private boolean requestCommit = false;
+       private String commitMessage = null;
        
        @Override
-       public void commit() {
+       public void commit(String message) {
                requestCommit = true;
+               commitMessage = message;
        }
        
        protected void doCommit() {
@@ -215,6 +284,12 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        
                        @Override
                        public void perform(WriteGraph graph) throws DatabaseException {
+                               if (DEBUG) System.out.println("Commit " + commitMessage);
+                               if (commitMessage != null) {
+                                       Layer0Utils.addCommentMetadata(graph, commitMessage);
+                                       graph.markUndoPoint();
+                                       commitMessage = null;
+                               }
                                commit(graph);
                        }
                        
@@ -234,9 +309,13 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        graphUpdates = true;
                        mapping.updateDomain(graph);
                        graphUpdates = false;
+                       clearDeletes();
+                       if (DEBUG) System.out.println("Commit done");
                }
        }
        
+       
+       
        @Override
        public void domainModified() {
                if (graphUpdates)
@@ -253,21 +332,45 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                
        }
        
-       protected void update(ReadGraph graph) throws DatabaseException {
+       protected void reset(ReadGraph graph) throws MappingException {
+               if (DEBUG) System.out.println("Reset");
+               
                synchronized (syncMutex) {
                        graphUpdates = true;
-                       for (Object domainObject : mapping.getDomainModified()) {
-                               E rangeObject = mapping.get(domainObject);
-                               if (rangeObject != null)
-                                       graphModified.add(rangeObject);
-                       }
+                       mapping.getRangeModified().clear();
+                       for (DBObject o : mapping.getDomain())
+                               mapping.domainModified(o);
                        mapping.updateRange(graph);
                        graphModified.clear();
                        graphUpdates = false;
                }
+       }
+       
+       private boolean useFullSyncWithUndo = false;
+       
+       protected void update(ReadGraph graph) throws DatabaseException {
+               if (DEBUG) System.out.println("Graph update start");
+               if (runUndo && useFullSyncWithUndo) {
+                       reset(graph);
+               } else {
+                       synchronized (syncMutex) {
+                               graphUpdates = true;
+                               for (DBObject domainObject : mapping.getDomainModified()) {
+                                       E rangeObject = mapping.get(domainObject);
+                                       if (rangeObject != null)
+                                               graphModified.add(rangeObject);
+                               }
+                               mapping.updateRange(graph);
+                               graphModified.clear();
+                               syncDeletes();
+                               clearDeletes();
+                               graphUpdates = false;
+                       }
+               }
                
-               if (mapping.isRangeModified())
-                       commit();
+               if (mapping.isRangeModified() && !runUndo && !runRedo)
+                       commit((String)null);
+               if (DEBUG) System.out.println("Graph update done");
        }
        
        @Override
@@ -287,18 +390,72 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                }
        }
        
-       List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>();
-       List<Pair<E, String>> add = new ArrayList<Pair<E,String>>();
-       MapSet<E, String> mod = new MapSet.Hash<E, String>();
-       Set<E> propagation = new HashSet<E>();
-       Stack<E> stack = new Stack<E>();
-       
+       // Reusable containers for data synchronisation
+       List<Pair<E, String>> rem = new ArrayList<Pair<E,String>>();  // Removed objects
+       List<Pair<E, String>> add = new ArrayList<Pair<E,String>>();  // Added objects
+       MapSet<E, String> mod = new MapSet.Hash<E, String>();         // Modified objects
+       Set<E> propagation = new HashSet<E>();                        // Objects with propagated changes 
+       Stack<E> stack = new Stack<E>();                              // Stack for handling propagation
+       Set<E> delete = Collections.synchronizedSet(new HashSet<E>()); // Objects to be completely deleted
+       Set<E> deleteUC = new HashSet<E>();
        
        @Override
        public synchronized void preRender() {
                updateCycle();
        }
        
+       
+       /**
+        * When objects are removed (either from Java or Graph), after remove processing the Java objects remain in mapping cache.
+        * This causes problems with Undo and Redo, which cause re-using the removed objects from mapping cache.
+        * 
+        * This code here synchronizes removed and added objects to collect deletable objects. (a deletable object is one which is removed but not added).  
+        * 
+        */
+       protected void syncDeletes() {
+               deleteUC.clear();
+               for (Pair<E, String> n : removed) {
+                       deleteUC.add(n.first);   
+               }
+               for (Pair<E, String> n : added) {
+                       deleteUC.remove(n.first);   
+               } 
+               if (DEBUG && deleteUC.size() > 0) {
+                       System.out.println("Delete sync");
+                       for (E n : delete) {
+                               System.out.println(debugString(n));
+                       }
+               }
+               delete.addAll(deleteUC);
+               deleteUC.clear();
+       }
+
+       /**
+        * Clears deletable objects from mapping cache.
+        */
+       protected void clearDeletes() {
+               if (DEBUG && delete.size() > 0) System.out.println("Delete");
+               for (E n : delete) {
+                       if (DEBUG) System.out.println(debugString(n));
+                       mapping.getRange().remove(n);
+               }
+               delete.clear();
+       }
+       
+       protected String debugString(E n) {
+               return n + "@" + Integer.toHexString(n.hashCode());
+       }
+       
+       protected boolean filterChange(List<Pair<E,String>> list,E n) {
+           for (int i = list.size()-1; i >= 0; i--) {
+            if (list.get(i).first == n) {
+                list.remove(i);
+                return true;
+            }
+        }
+           return false;
+       }
+       
        @SuppressWarnings("unchecked")
        protected void updateCycle() {
                rem.clear();
@@ -306,7 +463,30 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                mod.clear();
                propagation.clear();
                
+               
                synchronized (syncMutex) {
+                   // Check for overlapping additions and deletions, prevent deleting objects that are also added and vice versa.
+                   Deque<E> stack = new ArrayDeque<E>();
+               for (Pair<E, String> n : added) {
+                   stack.add(n.first);
+               }
+               while (!stack.isEmpty()) {
+                   E n = stack.pop();
+                   boolean conflict = filterChange(removed, n);
+                   if (conflict) {
+                       System.out.println("Prevent removing " + n);
+                       //filterChange(added, n)
+                       if (filterChange(added, n))
+                           System.out.println("Prevent adding " + n);
+                   }
+                   if (n instanceof ParentNode) {
+                       ParentNode<INode> pn = (ParentNode<INode>)n;
+                       for (INode cn : pn.getNodes()) {
+                           stack.push((E)cn);
+                       }
+                   }
+               }
+               
                        rem.addAll(removed);
                        add.addAll(added);
                        for (E e : updated.getKeys()) {
@@ -314,16 +494,17 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                                        mod.add(e, s);
                                }
                        }
-                       
+                       syncDeletes();
                        removed.clear();
                        added.clear();
                        updated.clear();
                }
                
+               
+               
                for (Pair<E, String> n : rem) {
                        stopListening(n.first);
                        removeActor(n.first);
-               
                }
                
                for (Pair<E, String> n : add) {
@@ -363,19 +544,19 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        }
                }
                
-//             synchronized (syncMutex) {
-//                     rem.addAll(removed);
-//                     add.addAll(added);
-//                     //mod.addAll(updated);
-//                     for (E e : updated.getKeys()) {
-//                             for (String s : updated.getValues(e))
-//                                     mod.add(e, s);
-//                     }
-//                     
-//                     removed.clear();
-//                     added.clear();
-//                     updated.clear();
-//             }
+//      synchronized (syncMutex) {
+//          rem.addAll(removed);
+//          add.addAll(added);
+//          //mod.addAll(updated);
+//          for (E e : updated.getKeys()) {
+//              for (String s : updated.getValues(e))
+//                  mod.add(e, s);
+//          }
+//          
+//          removed.clear();
+//          added.clear();
+//          updated.clear();
+//      }
                
                for (E e : mod.getKeys()) {
                        Set<String> ids = mod.getValues(e);
@@ -391,15 +572,16 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        for (NodeListener l : nodeListeners)
                                l.nodeAdded(n.first.getParent(), n.first, n.second);
                }
-//             for (Pair<E, String> n : mod) {
-//                     for (NodeListener l : nodeListeners)
-//                             l.propertyChanged(n.first, n.second);
-//             }
+//      for (Pair<E, String> n : mod) {
+//          for (NodeListener l : nodeListeners)
+//              l.propertyChanged(n.first, n.second);
+//      }
                for (E e : mod.getKeys()) {
                        for (NodeListener l : nodeListeners)
                                for (String s : mod.getValues(e))
                                        l.propertyChanged(e, s);
                }
+               
                synchronized (syncMutex) {
                        if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
                                rangeModified = false;
@@ -459,6 +641,9 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        @Override
        public void delete() {
+               if (undoRedoSupport != null)
+                       undoRedoSupport.cancel(this);
+               
                changeTracking = false;
                view.removeListener(this);
                mapping.removeMappingListener(this);
@@ -493,7 +678,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                
        }
        
-       public IMapping<Object,E> getMapping() {
+       public IMapping<DBObject,E> getMapping() {
                return mapping;
        }