]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/common/AbstractVTKNodeMap.java
Remove/Split action removes pipeline components without reconnecting
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / common / AbstractVTKNodeMap.java
index 6194ec822a0d8ef51fa7a202be5bbd0e1eff3f62..d9b754b45e115bb0f7be78fc6c3f56c327abae37 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;
@@ -23,6 +25,7 @@ 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;
@@ -60,7 +63,9 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        protected UndoRedoSupport undoRedoSupport;
        protected int undoOpCount = 0;
+       protected int redoOpCount = 0;
        protected boolean runUndo = false;
+       protected boolean runRedo = false;
        public AbstractVTKNodeMap(Session session, IMapping<Object,E> mapping, VtkView view, ParentNode<E> rootNode) {
                this.session = session;
                this.mapping = mapping;
@@ -73,9 +78,11 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                undoRedoSupport = session.getService(UndoRedoSupport.class);
                undoRedoSupport.subscribe(this);
                try {
-                   undoOpCount = undoRedoSupport.getUndoContext(session).getAll().size();
+                       UndoContext undoContext = undoRedoSupport.getUndoContext(session); 
+                       undoOpCount = undoContext.getAll().size();
+                       redoOpCount = undoContext.getRedoList().size();
                } catch(DatabaseException e) {
-                   e.printStackTrace();
+                       e.printStackTrace();
                }
        }
        
@@ -132,33 +139,41 @@ 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;
        
        @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();
-        }
-           
-           
+               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)) {
@@ -172,12 +187,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();
@@ -215,24 +230,24 @@ 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 " + 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;
@@ -252,13 +267,13 @@ 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 (DEBUG) System.out.println("Commit " + commitMessage);
                                if (commitMessage != null) {
-                                   Layer0Utils.addCommentMetadata(graph, commitMessage);
-                                   graph.markUndoPoint();
-                                   commitMessage = null;
+                                       Layer0Utils.addCommentMetadata(graph, commitMessage);
+                                       graph.markUndoPoint();
+                                       commitMessage = null;
                                }
-                       commit(graph);
+                               commit(graph);
                        }
                        
                }, new Callback<DatabaseException>() {
@@ -278,7 +293,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        mapping.updateDomain(graph);
                        graphUpdates = false;
                        clearDeletes();
-               if (DEBUG) System.out.println("Commit done");
+                       if (DEBUG) System.out.println("Commit done");
                }
        }
        
@@ -301,42 +316,41 @@ 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;
-        }
+               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 {
-           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 (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() && !runUndo) // FIXME : redo?
-           if (mapping.isRangeModified())
+               if (mapping.isRangeModified() && !runUndo && !runRedo)
                        commit((String)null);
                if (DEBUG) System.out.println("Graph update done");
        }
@@ -381,37 +395,37 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
         * 
         */
        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();
+               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();
-    }
+               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());
+               return n + "@" + Integer.toHexString(n.hashCode());
        }
        
        @SuppressWarnings("unchecked")
@@ -423,6 +437,27 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                
                
                synchronized (syncMutex) {
+                   // Check for overlapping additions and deletions, prevent deleting objects that are also added.
+                   Deque<E> stack = new ArrayDeque<E>();
+               for (Pair<E, String> n : added) {
+                   stack.add(n.first);
+               }
+               while (!stack.isEmpty()) {
+                   E n = stack.pop();
+                   for (int i = removed.size()-1; i >= 0; i--) {
+                       if (removed.get(i).first == n) {
+                           removed.remove(i);
+                           break;
+                       }
+                   }
+                   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()) {
@@ -436,6 +471,8 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                        updated.clear();
                }
                
+               
+               
                for (Pair<E, String> n : rem) {
                        stopListening(n.first);
                        removeActor(n.first);
@@ -478,19 +515,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);
@@ -506,10 +543,10 @@ 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))
@@ -517,7 +554,7 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
                }
                
                synchronized (syncMutex) {
-                   if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
+                       if (added.isEmpty() && removed.isEmpty() && updated.getKeys().size() == 0)
                                rangeModified = false;
                }
        }
@@ -575,9 +612,9 @@ public abstract class AbstractVTKNodeMap<E extends INode> implements VTKNodeMap<
        
        @Override
        public void delete() {
-           if (undoRedoSupport != null)
-               undoRedoSupport.cancel(this);
-        
+               if (undoRedoSupport != null)
+                       undoRedoSupport.cancel(this);
+               
                changeTracking = false;
                view.removeListener(this);
                mapping.removeMappingListener(this);