X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FDataNode.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FDataNode.java;h=2d3ac267842325333484b68886998471981d8135;hp=0000000000000000000000000000000000000000;hb=f19b48e27a56ebab18687debd9ef52581a03f06d;hpb=dfa4091ccd9f46687c7326aa6bf2eaf588a7cb83 diff --git a/src/be/nikiroo/utils/ui/DataNode.java b/src/be/nikiroo/utils/ui/DataNode.java new file mode 100644 index 0000000..2d3ac26 --- /dev/null +++ b/src/be/nikiroo/utils/ui/DataNode.java @@ -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 { + private DataNode parent; + private List> children; + private T userData; + + public DataNode(List> children, T userData) { + if (children == null) { + children = new ArrayList>(); + } + + this.children = children; + this.userData = userData; + + for (DataNode child : children) { + child.parent = this; + } + } + + public DataNode getRoot() { + DataNode root = this; + while (root.parent != null) { + root = root.parent; + } + + return root; + } + + public DataNode getParent() { + return parent; + } + + public List> getChildren() { + return children; + } + + public int size() { + return children.size(); + } + + public boolean isRoot() { + return this == getRoot(); + } + + public boolean isSiblingOf(DataNode node) { + if (this == node) { + return true; + } + + return node != null && parent != null && parent.children.contains(node); + } + + public boolean isParentOf(DataNode node) { + if (node == null || node.parent == null) + return false; + + if (this == node.parent) + return true; + + return isParentOf(node.parent); + } + + public boolean isChildOf(DataNode 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 child : children) { + s += child.count(); + } + + return s; + } + + @Override + public String toString() { + if (userData == null) { + return ""; + } + + return userData.toString(); + } +} \ No newline at end of file