]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateOp.java
Getters for specific class UpdateOps
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateOp.java
index 72e17856a4a28589a02b09e21795097ea1da93b5..3b633c28cdfd5a017995ff7e2c1a364b9cc4d22f 100644 (file)
@@ -2,8 +2,10 @@ package  org.simantics.interop.update.model;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 
 import org.simantics.db.Resource;
+import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.interop.test.GraphChanges;
@@ -21,6 +23,8 @@ public abstract class UpdateOp {
        private boolean selected = false;
        private boolean manualSelection = false;
        protected boolean applied = false;
+       protected boolean visible = true;
+       protected boolean enabled = true;
        
        private Collection<UpdateOp> parentOps = new ArrayList<UpdateOp>();
        private Collection<UpdateOp> subOps = new ArrayList<UpdateOp>();
@@ -34,10 +38,26 @@ public abstract class UpdateOp {
                return parentOps;
        }
        
+       public <T extends UpdateOp> Collection<T> getParentOpsWithClass(Class<T> cls) {
+               List<T> ops = new ArrayList<T>(parentOps.size());
+               for (UpdateOp op : parentOps)
+                       if (cls.isAssignableFrom(op.getClass()))
+                               ops.add((T)op);
+               return ops;
+       }
+       
        public Collection<UpdateOp> getSubOps() {
                return subOps;
        }
        
+       public <T extends UpdateOp> Collection<T> getSubOpsWithClass(Class<T> cls) {
+               List<T> ops = new ArrayList<T>(subOps.size());
+               for (UpdateOp op : subOps)
+                       if (cls.isAssignableFrom(op.getClass()))
+                               ops.add((T)op);
+               return ops;
+       }
+       
        public void addParentOp(UpdateOp op) {
                assert (!op.equals(this));
                parentOps.add(op);
@@ -66,80 +86,98 @@ public abstract class UpdateOp {
        public boolean isChange() {
                return isAdd() || isDelete();
        }
-       
-       /**
-        * Is parent operation applied before this.
-        * @return
-        */
-       public abstract boolean requiresParentOps();
-       /**
-        * Is child operation applied before this.
-        * @return
-        */
-       public abstract boolean requiresSubOps(); 
-       
+               
        /**
-        * Is parent operation selected when this is selected
+        * Should given operation to be applied before this operation.
+        * @param op
         * @return
         */
-       public boolean selectParentOps() {
-               return requiresParentOps();
+       public boolean requiresOp(UpdateOp op) {
+           return false;
        }
        
        /**
-        * Is child operation selected when this is selected
+        * Should selection state to be propagated to given op.
+        * @param op parent or sub op of this.
+        * @param select selection flag.
         * @return
         */
-       public boolean selectSubOps() {
-               return requiresSubOps();
-       }
+       public boolean selectOp(UpdateOp op, boolean select) {
+           return requiresOp(op);
+    }
        
        public boolean select(boolean select) {
+           if (!enabled)
+               return false;
+           if (!isChange())
+               return false;
                boolean b = _select(select);
                if (b)
                        manualSelection = true;
                return b;
        }
-       
+
        private boolean _select(boolean select) {
                if (select == selected)
                        return true;
+               if (applied)
+                       return false;
                if (select) {
-                       if (selectParentOps()) {
-                               for (UpdateOp op : parentOps)
-                                       op._select(true);
-                       }
-                       
-                       selected = true;
-                       manualSelection = false;
-                       if (selectSubOps()) {
-                               for (UpdateOp op : subOps)
-                                       op._select(true);
-                       }
-                       
+                   selected = true;
+            manualSelection = false;
+                   for (UpdateOp op : parentOps) {
+                       if (selectOp(op,true))
+                   op._select(true);
+            }
+                       for (UpdateOp op : subOps) {
+                           if (selectOp(op,true))
+                   op._select(true);
+            }
+                       return true;
                } else {
                        selected = false;
                        manualSelection = false;
                        for (UpdateOp op : subOps) {
-                               if (op.selectParentOps())
-                                       op._select(false);
-                               else if (!op.manualSelection)
-                                       op._select(false);
-                       }
+                           if (selectOp(op, false))
+                    op._select(false);
+                else if (!op.manualSelection)
+                    op._select(false);
+            }
                        for (UpdateOp op : parentOps)
-                               if (op.selectSubOps())
-                                       op._select(false);
+                          if (selectOp(op, false))
+                  op._select(false);
                        return true;
                }
-               return false;
        }
+       
        public boolean selected() {
+           if (!isChange())
+               // Non change operations are not really selected, but the selected flag may be used for selection propagation
+               return false;
                return selected;
        }
        
        public boolean applied() {
                return applied;
        }
+       
+       public boolean isVisible() {
+               return visible;
+       }
+       
+       /**
+     * Is change enabled. Disabled changes do not allow changing selected state.
+     * @return
+     */
+    public boolean enabled() {
+        return enabled;
+    }
+    
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+    }
+       
+       
        public void apply(WriteGraph g) throws DatabaseException {
                if (applied)
                        return;
@@ -162,10 +200,30 @@ public abstract class UpdateOp {
         */
        public abstract Resource getResource();
        
+       /**
+        * Returns resource that this operation is changing.
+        * @return
+        */
+       public abstract Statement getStatement();
+       
        /**
         * Returns resource that this operation created during apply operation. If operation did not add anything, this returns null.
         * @return
         */
        public abstract Resource getCreatedResource();
+       
+       @Override
+    public String toString() {
+        String s = this.getClass().getSimpleName();
+        if (selected)
+            s += " selected";
+        if (enabled)
+            s += " enabled";
+        if (visible)
+            s += " visible";
+        if (applied)
+            s += " applied";
+        return s;
+    }
 
 }