1 package be
.nikiroo
.utils
.ui
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
7 import javax
.swing
.Icon
;
9 public class DataNode
<T
> {
10 private DataNode
<T
> parent
;
11 private List
<?
extends DataNode
<T
>> children
;
14 public DataNode(List
<?
extends DataNode
<T
>> children
, T userData
) {
15 if (children
== null) {
16 children
= new ArrayList
<DataNode
<T
>>();
19 this.children
= children
;
20 this.userData
= userData
;
22 for (DataNode
<T
> child
: children
) {
27 public DataNode
<T
> getRoot() {
28 DataNode
<T
> root
= this;
29 while (root
.parent
!= null) {
36 public DataNode
<T
> getParent() {
40 public List
<?
extends DataNode
<T
>> getChildren() {
45 return children
.size();
48 public boolean isRoot() {
49 return this == getRoot();
52 public boolean isSiblingOf(DataNode
<T
> node
) {
57 return node
!= null && parent
!= null && parent
.children
.contains(node
);
60 public boolean isParentOf(DataNode
<T
> node
) {
61 if (node
== null || node
.parent
== null)
64 if (this == node
.parent
)
67 return isParentOf(node
.parent
);
70 public boolean isChildOf(DataNode
<T
> node
) {
71 if (node
== null || node
.size() == 0)
74 return node
.isParentOf(this);
77 public T
getUserData() {
82 * The total number of nodes present in this {@link DataNode} (including
83 * itself and descendants).
89 for (DataNode
<T
> child
: children
) {
97 public String
toString() {
98 if (userData
== null) {
102 return userData
.toString();