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; } /** * The total number of nodes present in this {@link DataNode} (including * itself and descendants). * * @return the number */ public int count() { int s = 1; for (DataNode child : children) { s += child.count(); } return s; } @Override public String toString() { if (userData == null) { return ""; } return userData.toString(); } }