]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java
Clear removed objects from mapping cache
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / common / AbstractVTKNodeMap.java
index dab2040edde8be9947723d35a3c7528fd6825254..6194ec822a0d8ef51fa7a202be5bbd0e1eff3f62 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.g3d.vtk.common;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -27,11 +28,13 @@ 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;
@@ -42,7 +45,7 @@ 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<E extends INode> implements VTKNodeMap<E>, IMappingListener, RenderListener, NodeListener, UndoRedoSupport.ChangeListener{
 
        private static final boolean DEBUG = false;
        
@@ -55,6 +58,9 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
 
        protected ParentNode<E> rootNode;
        
+       protected UndoRedoSupport undoRedoSupport;
+       protected int undoOpCount = 0;
+       protected boolean runUndo = false;
        public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, VtkView view, ParentNode<E> rootNode) {
                this.session = session;
                this.mapping = mapping;
@@ -63,9 +69,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 {
+                   undoOpCount = undoRedoSupport.getUndoContext(session).getAll().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);
@@ -122,6 +137,25 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
 
        private boolean rangeModified = false;
        
+       @Override
+       public void onChanged() {
+           try {
+               int count = undoRedoSupport.getUndoContext(session).getAll().size();
+            if (count < undoOpCount) {
+                runUndo = true;
+            } else {
+                runUndo = false;
+            }
+            undoOpCount = count;
+            if (DEBUG) System.out.println("Undo " + runUndo);
+        } catch (DatabaseException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+           
+           
+       }
+       
        @SuppressWarnings("unchecked")
        @Override
        public void updateRenderObjectsFor(INode node) {
@@ -153,7 +187,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))
@@ -170,7 +204,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        @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))
@@ -186,7 +220,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        @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))
@@ -218,12 +252,13 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        
                        @Override
                        public void perform(WriteGraph graph) throws DatabaseException {
-                               commit(graph);
+                           if (DEBUG) System.out.println("Commit " + commitMessage);
                                if (commitMessage != null) {
                                    Layer0Utils.addCommentMetadata(graph, commitMessage);
+                                   graph.markUndoPoint();
                                    commitMessage = null;
                                }
-                       graph.markUndoPoint();
+                       commit(graph);
                        }
                        
                }, new Callback<DatabaseException>() {
@@ -242,9 +277,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)
@@ -261,21 +300,45 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                
        }
        
+       protected void reset(ReadGraph graph) throws MappingException {
+           if (DEBUG) System.out.println("Reset");
+        synchronized (syncMutex) {
+            graphUpdates = true;
+            mapping.getRangeModified().clear();
+            for (Object o : mapping.getDomain())
+                mapping.domainModified(o);
+            mapping.updateRange(graph);
+            graphModified.clear();
+            graphUpdates = false;
+        }
+       }
+       
+       private boolean useFullSyncWithUndo = false;
+       
        protected void update(ReadGraph graph) throws DatabaseException {
-               synchronized (syncMutex) {
-                       graphUpdates = true;
-                       for (Object domainObject : mapping.getDomainModified()) {
-                               E rangeObject = mapping.get(domainObject);
-                               if (rangeObject != null)
-                                       graphModified.add(rangeObject);
-                       }
-                       mapping.updateRange(graph);
-                       graphModified.clear();
-                       graphUpdates = false;
-               }
+           if (DEBUG) System.out.println("Graph update start");
+           if (runUndo && useFullSyncWithUndo) {
+               reset(graph);
+           } else {
+               synchronized (syncMutex) {
+                       graphUpdates = true;
+                       for (Object 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("Graph sync");
+               //if (mapping.isRangeModified() && !runUndo) // FIXME : redo?
+           if (mapping.isRangeModified())
+                       commit((String)null);
+               if (DEBUG) System.out.println("Graph update done");
        }
        
        @Override
@@ -295,18 +358,62 @@ 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, whcih the end up 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());
+       }
+       
        @SuppressWarnings("unchecked")
        protected void updateCycle() {
                rem.clear();
@@ -314,6 +421,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                mod.clear();
                propagation.clear();
                
+               
                synchronized (syncMutex) {
                        rem.addAll(removed);
                        add.addAll(added);
@@ -322,7 +430,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                                        mod.add(e, s);
                                }
                        }
-                       
+                       syncDeletes();
                        removed.clear();
                        added.clear();
                        updated.clear();
@@ -331,7 +439,6 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                for (Pair<E, String> n : rem) {
                        stopListening(n.first);
                        removeActor(n.first);
-               
                }
                
                for (Pair<E, String> n : add) {
@@ -408,8 +515,9 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                                for (String s : mod.getValues(e))
                                        l.propertyChanged(e, s);
                }
+               
                synchronized (syncMutex) {
-                       if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
+                   if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
                                rangeModified = false;
                }
        }
@@ -467,6 +575,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);