Usually, tree nodes have a value property which allows access to the value stored in the node. In contrast to this approach, the TreeNode<T> class is designed to be used as a base class which provides the tree traversing functionality via inheritance.
Example usage: private class BasicTreeNode extends TreeNode<BasicTreeNode> { public BasicTreeNode() { super(BasicTreeNode.class); } }
This class started out as a copy of javax.swing.tree.DefaultMutableTreeNode.
| Modifier and Type | Field and Description |
|---|---|
| private final ObservableList | children
Array of children, may be empty if this node has no children (but never null) |
| private Consumer | onDescendantChanged
The function which is invoked when something changed in the subtree. |
| private T | parent
This node's parent, or null if this node has no parent |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public T | Returns: the child nodethe node to add child)Adds the node at the end the children collection. |
| public T | Returns: the child nodethe node to add child, int the position where the node should be added index)Adds the node at the given position in the children collection. |
| protected boolean | Returns: returns true when index is at least 0 and less then the count of childrenthe index to be tested index)Returns whether the specified index is a valid index for a child. |
| public abstract T | Returns: a deep copy of this nodeCreates a copy of this node, completely separated from the tree (i.e. no children and no parent) |
| public T | Returns: a deep copy of the subtreeCreates a deep copy of this node and all of its children. |
| public List | findChildrenSatisfying(Predicate<T> matcher)
Returns the group and any of its children in the tree satisfying the given condition. |
| public Optional | Returns: the node in this node's children collection at the specified index, or an empty Optional if the index does not point to a childan index into this node's children collection index)Returns the child at the specified index in this node's children collection. |
| public ObservableList | Returns: a list of this node's childrenGets a forward-order list of this node's children. |
| public Optional | Returns: descendant found by evaluating indexedPathsequence of child indices that describe a path from this node to one of its descendants.
Be aware that if indexedPath was obtained by getIndexedPathFromRoot(), this node should
usually be the root node. indexedPath)Get the descendant of this node as indicated by the indexedPath. |
| public Optional | Returns: the first child of this nodeReturns this node's first child if it exists (otherwise returns an empty Optional). |
| public List | Returns: a list of numbers which represent an indexed path from the root node to this nodeGet the path from the root node to this node. |
| public Optional | Returns: an integer giving the index of the node in this node's child list or an empty Optional if the specified node is a not a child of this nodethe node to search for among this node's children childNode)Gets the index of the specified child in this node's child list. |
| public Optional | Returns: the last child of this nodeReturns this node's last child if it exists (otherwise returns an empty Optional). |
| public int | Returns: an int giving the number of levels above this nodeGets the number of levels above this node, i.e. the distance from the root to this node. |
| public Optional | Returns: the sibling of this node that immediately follows this nodeReturns the next sibling of this node in the parent's children list. |
| public int | Returns: an int giving the number of children of this nodeReturns the number of children of this node. |
| public Optional | Returns: this node's parent T, or an empty Optional if this node has no parentReturns this node's parent or an empty Optional if this node has no parent. |
| public List | Returns: a list of nodes giving the path, where the first element in the path is the root and the last element is this node.Returns the path from the root, to get to this node. |
| public int | Returns: the child index of this node in its parentGet the child index of this node in its parent. |
| public Optional | Returns: the sibling of this node that immediately precedes this nodeReturns the previous sibling of this node in the parent's children list. |
| private Optional | |
| public T | Returns: the root of the tree that contains this nodeReturns the root of the tree that contains this node. |
| public boolean | Returns: true if anotherNode is a descendant of this nodenode to test anotherNode)Returns true if this node is an ancestor of the given node. |
| public boolean | |
| public boolean | Returns: true if this node is an ancestor of anotherNodenode to test as descendant of this node anotherNode)Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children. |
| public boolean | Returns: true if this node is the root of its treeReturns true if this node is the root of the tree. |
| public void | moveAllChildrenTo(T
the new parent target, int the position where the children should be inserted targetIndex)Removes all children from this node and makes them a child of the specified node by adding it to the specified position in the children list. |
| public void | moveTo(T
the new parent target)Removes this node from its parent and makes it a child of the specified node by adding it to the end of children list. |
| public void | moveTo(T
the new parent target, int the position where the children should be inserted targetIndex)Removes this node from its parent and makes it a child of the specified node by adding it to the specified position in the children list. |
| protected void | notifyAboutDescendantChange(T
the node which changed source)Helper method which notifies all subscribers about a change in the subtree and bubbles the event to all parents. |
| public void | |
| public void | removeChild(T
a child of this node to remove child)Removes the given child from this node's child list, giving it an empty parent. |
| public void | removeChild(int
the index in this node's child array of the child to remove childIndex)Removes the child at the specified index from this node's children and sets that node's parent to empty. |
| public void | removeFromParent()
Removes the subtree rooted at this node from the tree, giving this node an empty parent. |
| protected void | |
| public void | sortChildren(Comparator<? super T>
the comparator used to compare the child nodes comparator, boolean if true the whole subtree is sorted recursive)Sorts the list of children according to the order induced by the specified |
| public void | subscribeToDescendantChanged(Consumer<T>
function to be invoked upon a change subscriber)Adds the given function to the list of subscribers which are notified when something changes in the subtree. |
| children | back to summary |
|---|---|
| private final ObservableList<T> children Array of children, may be empty if this node has no children (but never null) | |
| onDescendantChanged | back to summary |
|---|---|
| private Consumer<T> onDescendantChanged The function which is invoked when something changed in the subtree. | |
| parent | back to summary |
|---|---|
| private T parent This node's parent, or null if this node has no parent | |
| TreeNode | back to summary |
|---|---|
| public TreeNode(Class<T> derivingClass) Constructs a tree node without parent and no children.
| |
| addChild | back to summary |
|---|---|
| public T addChild(T child) Adds the node at the end the children collection. Also sets the parent of the given node to this node. The given node is not allowed to already be in a tree (i.e. it has to have no parent).
| |
| addChild | back to summary |
|---|---|
| public T addChild(T child, int index) Adds the node at the given position in the children collection. Also sets the parent of the given node to this node. The given node is not allowed to already be in a tree (i.e. it has to have no parent).
| |
| childIndexExists | back to summary |
|---|---|
| protected boolean childIndexExists(int index) Returns whether the specified index is a valid index for a child.
| |
| copyNode | back to summary |
|---|---|
| public abstract T copyNode() Creates a copy of this node, completely separated from the tree (i.e. no children and no parent)
| |
| copySubtree | back to summary |
|---|---|
| public T copySubtree() Creates a deep copy of this node and all of its children.
| |
| findChildrenSatisfying | back to summary |
|---|---|
| public List Returns the group and any of its children in the tree satisfying the given condition. | |
| getChildAt | back to summary |
|---|---|
| public Optional Returns the child at the specified index in this node's children collection.
| |
| getChildren | back to summary |
|---|---|
| public ObservableList Gets a forward-order list of this node's children. The returned list is unmodifiable - use the add and remove methods to modify the nodes children. However, changing the nodes children (for example by calling moveTo) is reflected in a change of the list returned by getChildren. In other words, getChildren provides a read-only view on the children but not a copy.
| |
| getDescendant | back to summary |
|---|---|
| public Optional Get the descendant of this node as indicated by the indexedPath. If the path could not be traversed completely (i.e. one of the child indices did not exist), an empty Optional will be returned.
| |
| getFirstChild | back to summary |
|---|---|
| public Optional Returns this node's first child if it exists (otherwise returns an empty Optional).
| |
| getIndexedPathFromRoot | back to summary |
|---|---|
| public List Get the path from the root node to this node. The elements in the returned list represent the child index of each node in the path, starting at the root. If this node is the root node, the returned list has zero elements. | |
| getIndexOfChild | back to summary |
|---|---|
| public Optional Gets the index of the specified child in this node's child list. If the specified node is not a child of this node, returns an empty Optional. This method performs a linear search and is O(n) where n is the number of children.
| |
| getLastChild | back to summary |
|---|---|
| public Optional Returns this node's last child if it exists (otherwise returns an empty Optional).
| |
| getLevel | back to summary |
|---|---|
| public int getLevel() Gets the number of levels above this node, i.e. the distance from the root to this node. If this node is the root, returns 0.
| |
| getNextSibling | back to summary |
|---|---|
| public Optional Returns the next sibling of this node in the parent's children list. Returns an empty Optional if this node has no parent or if it is the parent's last child. This method performs a linear search that is O(n) where n is the number of children. To traverse the entire children collection, use the parent's getChildren() instead.
| |
| getNumberOfChildren | back to summary |
|---|---|
| public int getNumberOfChildren() Returns the number of children of this node.
| |
| getParent | back to summary |
|---|---|
| public Optional Returns this node's parent or an empty Optional if this node has no parent.
| |
| getPathFromRoot | back to summary |
|---|---|
| public List Returns the path from the root, to get to this node. The last element in the path is this node.
| |
| getPositionInParent | back to summary |
|---|---|
| public int getPositionInParent() Get the child index of this node in its parent. If this node is a root, then an UnsupportedOperationException is thrown. Use the isRoot method to check for this case.
| |
| getPreviousSibling | back to summary |
|---|---|
| public Optional Returns the previous sibling of this node in the parent's children list. Returns an empty Optional if this node has no parent or is the parent's first child. This method performs a linear search that is O(n) where n is the number of children.
| |
| getRelativeSibling | back to summary |
|---|---|
| private Optional Returns the sibling which is shiftIndex away from this node. | |
| getRoot | back to summary |
|---|---|
| public T getRoot() Returns the root of the tree that contains this node. The root is the ancestor with an empty parent. Thus a node without a parent is considered its own root.
| |
| isAncestorOf | back to summary |
|---|---|
| public boolean isAncestorOf(T anotherNode) Returns true if this node is an ancestor of the given node. A node is considered an ancestor of itself.
| |
| isLeaf | back to summary |
|---|---|
| public boolean isLeaf() Returns true if this node has no children.
| |
| isNodeDescendant | back to summary |
|---|---|
| public boolean isNodeDescendant(T anotherNode) Returns true if anotherNode is a descendant of this node -- if it is this node, one of this node's children, or a descendant of one of this node's children. Note that a node is considered a descendant of itself. If anotherNode is null, an exception is thrown.
| |
| isRoot | back to summary |
|---|---|
| public boolean isRoot() Returns true if this node is the root of the tree. The root is the only node in the tree with an empty parent; every tree has exactly one root.
| |
| moveAllChildrenTo | back to summary |
|---|---|
| public void moveAllChildrenTo(T target, int targetIndex) Removes all children from this node and makes them a child of the specified node by adding it to the specified position in the children list.
| |
| moveTo | back to summary |
|---|---|
| public void moveTo(T target) Removes this node from its parent and makes it a child of the specified node by adding it to the end of children list. In this way the whole subtree based at this node is moved to the given node.
| |
| moveTo | back to summary |
|---|---|
| public void moveTo(T target, int targetIndex) Removes this node from its parent and makes it a child of the specified node by adding it to the specified position in the children list. In this way the whole subtree based at this node is moved to the given node.
| |
| notifyAboutDescendantChange | back to summary |
|---|---|
| protected void notifyAboutDescendantChange(T source) Helper method which notifies all subscribers about a change in the subtree and bubbles the event to all parents.
| |
| removeAllChildren | back to summary |
|---|---|
| public void removeAllChildren() Removes all of this node's children, setting their parents to empty. If this node has no children, this method does nothing. | |
| removeChild | back to summary |
|---|---|
| public void removeChild(T child) Removes the given child from this node's child list, giving it an empty parent.
| |
| removeChild | back to summary |
|---|---|
| public void removeChild(int childIndex) Removes the child at the specified index from this node's children and sets that node's parent to empty. Does nothing if the index does not point to a child.
| |
| removeFromParent | back to summary |
|---|---|
| public void removeFromParent() Removes the subtree rooted at this node from the tree, giving this node an empty parent. Does nothing if this node is the root of it tree. | |
| setParent | back to summary |
|---|---|
| protected void setParent(T parent) Sets the parent node of this node. This method does not add this node to the children collection of the new parent nor does it remove this node from the old parent. You should probably call moveTo or remove to change the tree.
| |
| sortChildren | back to summary |
|---|---|
| public void sortChildren(Comparator<? super T> comparator, boolean recursive) Sorts the list of children according to the order induced by the specified
All children must be mutually comparable using the specified comparator
(that is,
| |
| subscribeToDescendantChanged | back to summary |
|---|---|
| public void subscribeToDescendantChanged(Consumer<T> subscriber) Adds the given function to the list of subscribers which are notified when something changes in the subtree. The following events are supported (the text in parentheses specifies which node is passed as the source): - addChild (new parent) - removeChild (old parent) - move (old parent and new parent)
| |