Merge commit 'ee020e75a5de0c1e54557db79f77edc50e05ea04'
[fanfix.git] / src / be / nikiroo / utils / ui / DataNode.java
diff --git a/src/be/nikiroo/utils/ui/DataNode.java b/src/be/nikiroo/utils/ui/DataNode.java
new file mode 100644 (file)
index 0000000..2d3ac26
--- /dev/null
@@ -0,0 +1,98 @@
+package be.nikiroo.utils.ui;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.swing.Icon;
+
+public class DataNode<T> {
+       private DataNode<T> parent;
+       private List<? extends DataNode<T>> children;
+       private T userData;
+
+       public DataNode(List<? extends DataNode<T>> children, T userData) {
+               if (children == null) {
+                       children = new ArrayList<DataNode<T>>();
+               }
+
+               this.children = children;
+               this.userData = userData;
+
+               for (DataNode<T> child : children) {
+                       child.parent = this;
+               }
+       }
+
+       public DataNode<T> getRoot() {
+               DataNode<T> root = this;
+               while (root.parent != null) {
+                       root = root.parent;
+               }
+
+               return root;
+       }
+
+       public DataNode<T> getParent() {
+               return parent;
+       }
+
+       public List<? extends DataNode<T>> getChildren() {
+               return children;
+       }
+
+       public int size() {
+               return children.size();
+       }
+
+       public boolean isRoot() {
+               return this == getRoot();
+       }
+
+       public boolean isSiblingOf(DataNode<T> node) {
+               if (this == node) {
+                       return true;
+               }
+
+               return node != null && parent != null && parent.children.contains(node);
+       }
+
+       public boolean isParentOf(DataNode<T> node) {
+               if (node == null || node.parent == null)
+                       return false;
+
+               if (this == node.parent)
+                       return true;
+
+               return isParentOf(node.parent);
+       }
+
+       public boolean isChildOf(DataNode<T> node) {
+               if (node == null || node.size() == 0)
+                       return false;
+
+               return node.isParentOf(this);
+       }
+
+       public T getUserData() {
+               return userData;
+       }
+
+       protected int count() {
+               int s = 0;
+               for (DataNode<T> child : children) {
+                       s += child.count();
+               }
+
+               return s;
+       }
+
+       @Override
+       public String toString() {
+               if (userData == null) {
+                       return "";
+               }
+
+               return userData.toString();
+       }
+}
\ No newline at end of file