/*
- * 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 {
- private List<TreeParent> children;
+/** 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);
- children = new ArrayList<TreeParent>();
- loaded = false;
+ this.name = name;
+ children = new ArrayList<Object>();
}
- public synchronized void addChild(TreeParent child) {
+ public synchronized void addChild(Object child) {
loaded = true;
children.add(child);
- child.setParent(this);
+ if (child instanceof TreeParent)
+ ((TreeParent) child).setParent(this);
}
- public synchronized void removeChild(TreeParent child) {
+ /**
+ * Remove this child. The child is disposed.
+ */
+ public synchronized void removeChild(Object child) {
children.remove(child);
- 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();
}
- public synchronized TreeObject[] getChildren() {
- return (TreeObject[]) children.toArray(new TreeParent[children.size()]);
+ /**
+ * 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;
}
- public TreeParent getChildByName(String name) {
- for (TreeParent child : children) {
- if (child.getName().equals(name))
+ public Object getChildByName(String name) {
+ for (Object child : children) {
+ if (child.toString().equals(name))
return child;
}
return null;
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());
+ }
+
}