]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/TreeParent.java
Update license headers
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui / src / main / java / org / argeo / eclipse / ui / TreeParent.java
index 8989d69ad744c8af5b8f63c435f99637be524b53..26bdbe615135a0bd9a6b0a76ad6280623c07de84 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ * Copyright (C) 2007-2012 Mathieu Baudier
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.argeo.eclipse.ui;
 
 import java.util.ArrayList;
 import java.util.List;
 
-public class TreeParent extends TreeObject {
+/** Parent / children semantic to be used for simple UI Tree structure */
+public class TreeParent {
+       private String name;
+       private TreeParent parent;
+
        private List<Object> children;
 
-       private boolean loaded;
+       /** False until at least one child has been added, then true until cleared */
+       private boolean loaded = false;
 
        public TreeParent(String name) {
-               super(name);
+               this.name = name;
                children = new ArrayList<Object>();
-               loaded = false;
        }
 
        public synchronized void addChild(Object child) {
@@ -37,21 +40,49 @@ public class TreeParent extends TreeObject {
                        ((TreeParent) child).setParent(this);
        }
 
+       /**
+        * Remove this child. The child is disposed.
+        */
        public synchronized void removeChild(Object child) {
                children.remove(child);
-               if (child instanceof TreeParent)
-                       ((TreeParent) child).setParent(null);
+               if (child instanceof TreeParent) {
+                       ((TreeParent) child).dispose();
+               }
        }
 
        public synchronized void clearChildren() {
+               for (Object obj : children) {
+                       if (obj instanceof TreeParent)
+                               ((TreeParent) obj).dispose();
+               }
                loaded = false;
                children.clear();
        }
 
+       /**
+        * If overridden, <code>super.dispose()</code> must be called, typically
+        * after custom cleaning.
+        */
+       public synchronized void dispose() {
+               clearChildren();
+               parent = null;
+               children = null;
+       }
+
        public synchronized Object[] getChildren() {
                return children.toArray(new Object[children.size()]);
        }
 
+       @SuppressWarnings("unchecked")
+       public synchronized <T> List<T> getChildrenOfType(Class<T> clss) {
+               List<T> lst = new ArrayList<T>();
+               for (Object obj : children) {
+                       if (clss.isAssignableFrom(obj.getClass()))
+                               lst.add((T) obj);
+               }
+               return lst;
+       }
+
        public synchronized boolean hasChildren() {
                return children.size() > 0;
        }
@@ -67,4 +98,35 @@ public class TreeParent extends TreeObject {
        public synchronized Boolean isLoaded() {
                return loaded;
        }
+
+       public String getName() {
+               return name;
+       }
+
+       public void setParent(TreeParent parent) {
+               this.parent = parent;
+       }
+
+       public TreeParent getParent() {
+               return parent;
+       }
+
+       public String toString() {
+               return getName();
+       }
+
+       public int compareTo(TreeParent o) {
+               return name.compareTo(o.name);
+       }
+
+       @Override
+       public int hashCode() {
+               return name.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               return name.equals(obj.toString());
+       }
+
 }