]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
Process changes in smaller chunks
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateTree.java
index a558f84462cf5e8cfe6548f761afa0c85cdb00fe..bf7e0688546beb2e053529ee30e54b41350b738d 100644 (file)
@@ -3,6 +3,8 @@ package  org.simantics.interop.update.model;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
@@ -29,17 +31,30 @@ public class UpdateTree {
                this.updateOps = updateOps;
                this.updateOps.populate(g);
                populate(g);
+               this.rootNode.sort();
        }
        
        public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
+               this(session, changes, updateOps, new NullProgressMonitor());
+       }
+       
+       public UpdateTree(Session session, GraphChanges changes, UpdateOperations updateOps, IProgressMonitor monitor) throws DatabaseException {
                this.changes = changes;
                this.nodes = new HashMap<Resource, UpdateNode>();
                this.rootNode = session.syncRequest(new NodeRequest(UpdateStatus.EXIST, changes.getResource1()));
                nodes.put(changes.getResource1(), rootNode);
                nodes.put(changes.getResource2(), rootNode);
                this.updateOps = updateOps;
-               this.updateOps.populate(session);
-               populate(session);
+               this.updateOps.populate(session, monitor);
+               populate(session, monitor);
+               if (monitor.isCanceled()) {
+                       this.changes = null;
+                       this.nodes.clear();
+                       this.updateOps = null;
+                       this.rootNode = null;
+               } else {
+                       this.rootNode.sort();
+               }
        }
        
        private class NodeRequest extends ResourceRead<UpdateNode> {
@@ -176,9 +191,11 @@ public class UpdateTree {
                }
        }
        
-       protected void populate(Session session) throws DatabaseException{
+       protected void populate(Session session, IProgressMonitor monitor) throws DatabaseException{
                int i = 0;
                while (i < updateOps.getOperations().size()) {
+                       if (monitor.isCanceled())
+                               return;
                        i = session.syncRequest(new PopulateRead(i));
                }
        }