]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Sort UpdateNodes
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateNode.java
index cc996a28239159d5ef94d666f49f2a9c1bae6f1f..5bf0cd4ec8e298d963a0267bd434888032da78e0 100644 (file)
@@ -1,7 +1,9 @@
 package org.simantics.interop.update.model;
 
 import java.util.ArrayList;
-import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
 
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.simantics.db.ReadGraph;
@@ -12,13 +14,15 @@ import org.simantics.layer0.Layer0;
 
 public class UpdateNode {
 
+    private UpdateNode parent;
        private UpdateStatus status;
        private UpdateOp op;
        private Resource r;
        private String label;
+       private boolean visible = true;
        
        
-       private Collection<UpdateNode> children = new ArrayList<UpdateNode>();
+       private List<UpdateNode> children;
        /**
         * 
         * @param resource old Resource if status is DELETED or EXISTS.
@@ -30,7 +34,7 @@ public class UpdateNode {
                this.status = status;
                this.op = op;
                this.r = op.getResource();
-               this.label = getLabel(g, r);
+               init(g);
        }
        
        public UpdateNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
@@ -38,6 +42,10 @@ public class UpdateNode {
                this.status = status;
                this.op = null;
                this.r = r;
+               init(g);
+       }
+       
+       protected void init(ReadGraph g) throws DatabaseException {
                this.label = getLabel(g, r);
        }
        
@@ -46,6 +54,11 @@ public class UpdateNode {
        }
        
        public Resource getParentResource(ReadGraph g) throws DatabaseException {
+               if (op != null) {
+                       Resource parent = op.getParentResource(g);
+                       if (parent != null)
+                               return parent;
+               }
                Layer0 l0 = Layer0.getInstance(g);
                return g.getPossibleObject(r, l0.PartOf);
        }
@@ -58,12 +71,18 @@ public class UpdateNode {
                return status;
        }
        
-       public Collection<UpdateNode> getChildren() {
+       @SuppressWarnings("unchecked")
+       public List<UpdateNode> getChildren() {
+               if (children == null)
+                       return Collections.EMPTY_LIST;
                return children;
        }
        
        public void addChild(UpdateNode node) {
+               if (children == null)
+                       children = new ArrayList<UpdateNode>(2);
                children.add(node);
+               node.parent = this;
                if (op != null && node.op != null) {
                        if (!op.getSubOps().contains(node.op)) {
                                op.addSubOp(node.op);
@@ -71,6 +90,30 @@ public class UpdateNode {
                        }
                }
        }
+       
+       public void sort() {
+               if (children == null)
+                       return;
+               Comparator<UpdateNode> comparator = new Comparator<UpdateNode>() {
+                       @Override
+                       public int compare(UpdateNode o1, UpdateNode o2) {
+                               return o1.getLabel().compareTo(o2.getLabel());
+                       }
+               };
+               Collections.sort(this.children, comparator );
+               for (UpdateNode n : this.children) {
+                       n.sort(comparator);
+               }
+       }
+       
+       public void sort(Comparator<UpdateNode> comparator ) {
+               if (children == null)
+                       return;
+               Collections.sort(this.children, comparator );
+               for (UpdateNode n : this.children) {
+                       n.sort(comparator);
+               }
+       }
 
        public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
                return null;
@@ -97,5 +140,32 @@ public class UpdateNode {
        public UpdateOp getOp() {
                return op;
        }
+       
+       public boolean isVisible() {
+               return visible;
+       }
+       
+       public void setVisible(boolean visible) {
+               this.visible = visible;
+               if (op != null)
+                       op.visible = visible;
+               if (visible) {
+                   if (parent != null && !parent.visible)
+                       parent.setVisible(true);
+               } else {
+                       if (children != null)
+                               for (UpdateNode n : children)
+                                       n.setVisible(false);
+               }
+       }
+       
+       public void setAllVisible(boolean visible) {
+               this.visible = visible;
+               if (op != null)
+                       op.visible = visible;
+               if (children != null)
+                       for (UpdateNode n : children)
+                               n.setAllVisible(visible);
+       }
 
 }