javax.swing.tree

Class DefaultMutableTreeNode

public class DefaultMutableTreeNode extends Object implements Cloneable, MutableTreeNode, Serializable

A default implementation of the {@link MutableTreeNode} interface.
Field Summary
protected booleanallowsChildren
allowsChildren
protected Vector<MutableTreeNode>children
The child nodes for this node (may be empty).
static Enumeration<TreeNode>EMPTY_ENUMERATION
An empty enumeration, returned by {@link #children()} if a node has no children.
protected MutableTreeNodeparent
The parent of this node (possibly null).
protected ObjectuserObject
userObject
Constructor Summary
DefaultMutableTreeNode()
Creates a DefaultMutableTreeNode object.
DefaultMutableTreeNode(Object userObject)
Creates a DefaultMutableTreeNode object with the given user object attached to it.
DefaultMutableTreeNode(Object userObject, boolean allowsChildren)
Creates a DefaultMutableTreeNode object with the given user object attached to it.
Method Summary
voidadd(MutableTreeNode child)
Adds a new child node to this node and sets this node as the parent of the child node.
EnumerationbreadthFirstEnumeration()
breadthFirstEnumeration
Enumerationchildren()
Returns an enumeration containing all children of this node.
Objectclone()
Returns a clone of the node.
EnumerationdepthFirstEnumeration()
depthFirstEnumeration
booleangetAllowsChildren()
getAllowsChildren
TreeNodegetChildAfter(TreeNode node)
Returns the next child after the specified node, or null if there is no child after the specified node.
TreeNodegetChildAt(int index)
Returns the child node at a given index.
TreeNodegetChildBefore(TreeNode node)
Returns the previous child before the specified node, or null if there is no child before the specified node.
intgetChildCount()
Returns the number of children of this node.
intgetDepth()
getDepth
TreeNodegetFirstChild()
Returns the first child node belonging to this tree node.
DefaultMutableTreeNodegetFirstLeaf()
Returns the first leaf node that is a descendant of this node.
intgetIndex(TreeNode node)
Returns the index of the specified child node, or -1 if the node is not in fact a child of this node.
TreeNodegetLastChild()
Returns the last child node belonging to this tree node.
DefaultMutableTreeNodegetLastLeaf()
Returns the last leaf node that is a descendant of this node.
intgetLeafCount()
getLeafCount
intgetLevel()
getLevel
DefaultMutableTreeNodegetNextLeaf()
Returns the next leaf node after this tree node.
DefaultMutableTreeNodegetNextNode()
getNextNode
DefaultMutableTreeNodegetNextSibling()
Returns the next sibling for this tree node.
TreeNodegetParent()
Returns the parent node of this node.
TreeNode[]getPath()
Returns a path to this node from the root.
protected TreeNode[]getPathToRoot(TreeNode node, int depth)
getPathToRoot
DefaultMutableTreeNodegetPreviousLeaf()
Returns the previous leaf node before this tree node.
DefaultMutableTreeNodegetPreviousNode()
getPreviousNode
DefaultMutableTreeNodegetPreviousSibling()
Returns the previous sibling for this tree node.
TreeNodegetRoot()
Returns the root node by iterating the parents of this node.
TreeNodegetSharedAncestor(DefaultMutableTreeNode node)
getSharedAncestor
intgetSiblingCount()
Returns the number of siblings for this tree node.
ObjectgetUserObject()
Returns the user object attached to this node.
Object[]getUserObjectPath()
getUserObjectPath
voidinsert(MutableTreeNode node, int index)
Inserts given child node at the given index.
booleanisLeaf()
Returns true if this tree node is a lead node (that is, it has no children), and false.
booleanisNodeAncestor(TreeNode node)
Returns true if node is an ancestor of this tree node, and false otherwise.
booleanisNodeChild(TreeNode node)
Returns true if node is a child of this tree node, and false otherwise.
booleanisNodeDescendant(DefaultMutableTreeNode node)
Returns true if node is a descendant of this tree node, and false otherwise.
booleanisNodeRelated(DefaultMutableTreeNode node)
isNodeRelated
booleanisNodeSibling(TreeNode node)
Returns true if this tree node and node share the same parent.
booleanisRoot()
Tells whether this node is the root node or not.
EnumerationpathFromAncestorEnumeration(TreeNode node)
pathFromAncestorEnumeration
EnumerationpostorderEnumeration()
postorderEnumeration
EnumerationpreorderEnumeration()
preorderEnumeration
voidremove(int index)
Removes the child with the given index from this node.
voidremove(MutableTreeNode node)
Removes the given child from this node and sets its parent to null.
voidremoveAllChildren()
Removes all child nodes from this node.
voidremoveFromParent()
Removes this node from its parent.
voidsetAllowsChildren(boolean allowsChildren)
Sets the flag that controls whether or not this node allows the addition / insertion of child nodes.
voidsetParent(MutableTreeNode node)
Set the parent node for this node.
voidsetUserObject(Object userObject)
Sets the user object for this node
StringtoString()
Returns a string representation of the node.

Field Detail

allowsChildren

protected boolean allowsChildren
allowsChildren

children

protected Vector<MutableTreeNode> children
The child nodes for this node (may be empty).

EMPTY_ENUMERATION

public static final Enumeration<TreeNode> EMPTY_ENUMERATION
An empty enumeration, returned by {@link #children()} if a node has no children.

parent

protected MutableTreeNode parent
The parent of this node (possibly null).

userObject

protected transient Object userObject
userObject

Constructor Detail

DefaultMutableTreeNode

public DefaultMutableTreeNode()
Creates a DefaultMutableTreeNode object. This is equivalent to DefaultMutableTreeNode(null, true).

DefaultMutableTreeNode

public DefaultMutableTreeNode(Object userObject)
Creates a DefaultMutableTreeNode object with the given user object attached to it. This is equivalent to DefaultMutableTreeNode(userObject, true).

Parameters: userObject the user object (null permitted).

DefaultMutableTreeNode

public DefaultMutableTreeNode(Object userObject, boolean allowsChildren)
Creates a DefaultMutableTreeNode object with the given user object attached to it.

Parameters: userObject the user object (null permitted). allowsChildren true if the code allows to add child nodes, false otherwise

Method Detail

add

public void add(MutableTreeNode child)
Adds a new child node to this node and sets this node as the parent of the child node. The child node must not be an ancestor of this node. If the tree uses the {@link DefaultTreeModel}, you must subsequently call {@link DefaultTreeModel#reload(TreeNode)}.

Parameters: child the child node (null not permitted).

Throws: IllegalStateException if {@link #getAllowsChildren()} returns false. IllegalArgumentException if {@link #isNodeAncestor} returns true. IllegalArgumentException if child is null.

breadthFirstEnumeration

public Enumeration breadthFirstEnumeration()
breadthFirstEnumeration

Returns: Enumeration

children

public Enumeration children()
Returns an enumeration containing all children of this node. EMPTY_ENUMERATION is returned if this node has no children.

Returns: an enumeration of tree nodes

clone

public Object clone()
Returns a clone of the node. The clone contains a shallow copy of the user object, and does not copy the parent node or the child nodes.

Returns: A clone of the node.

depthFirstEnumeration

public Enumeration depthFirstEnumeration()
depthFirstEnumeration

Returns: Enumeration

getAllowsChildren

public boolean getAllowsChildren()
getAllowsChildren

Returns: boolean

getChildAfter

public TreeNode getChildAfter(TreeNode node)
Returns the next child after the specified node, or null if there is no child after the specified node.

Parameters: node a child of this node (null not permitted).

Returns: The next child, or null.

Throws: IllegalArgumentException if node is not a child of this node, or is null.

getChildAt

public TreeNode getChildAt(int index)
Returns the child node at a given index.

Parameters: index the index

Returns: the child node

getChildBefore

public TreeNode getChildBefore(TreeNode node)
Returns the previous child before the specified node, or null if there is no child before the specified node.

Parameters: node a child of this node (null not permitted).

Returns: The previous child, or null.

Throws: IllegalArgumentException if node is not a child of this node, or is null.

getChildCount

public int getChildCount()
Returns the number of children of this node.

Returns: the number of children

getDepth

public int getDepth()
getDepth

Returns: int

getFirstChild

public TreeNode getFirstChild()
Returns the first child node belonging to this tree node.

Returns: The first child node.

Throws: NoSuchElementException if this tree node has no children.

getFirstLeaf

public DefaultMutableTreeNode getFirstLeaf()
Returns the first leaf node that is a descendant of this node. Recall that a node is its own descendant, so if this node has no children then it is returned as the first leaf.

Returns: The first leaf node.

getIndex

public int getIndex(TreeNode node)
Returns the index of the specified child node, or -1 if the node is not in fact a child of this node.

Parameters: node the node (null not permitted).

Returns: The index of the specified child node, or -1.

Throws: IllegalArgumentException if node is null.

getLastChild

public TreeNode getLastChild()
Returns the last child node belonging to this tree node.

Returns: The last child node.

Throws: NoSuchElementException if this tree node has no children.

getLastLeaf

public DefaultMutableTreeNode getLastLeaf()
Returns the last leaf node that is a descendant of this node. Recall that a node is its own descendant, so if this node has no children then it is returned as the last leaf.

Returns: The first leaf node.

getLeafCount

public int getLeafCount()
getLeafCount

Returns: int

getLevel

public int getLevel()
getLevel

Returns: int

getNextLeaf

public DefaultMutableTreeNode getNextLeaf()
Returns the next leaf node after this tree node.

Returns: The next leaf node, or null.

getNextNode

public DefaultMutableTreeNode getNextNode()
getNextNode

Returns: DefaultMutableTreeNode

getNextSibling

public DefaultMutableTreeNode getNextSibling()
Returns the next sibling for this tree node. If this node has no parent, or this node is the last child of its parent, this method returns null.

Returns: The next sibling, or null.

getParent

public TreeNode getParent()
Returns the parent node of this node.

Returns: The parent node (possibly null).

getPath

public TreeNode[] getPath()
Returns a path to this node from the root.

Returns: an array of tree nodes

getPathToRoot

protected TreeNode[] getPathToRoot(TreeNode node, int depth)
getPathToRoot

Parameters: node TODO depth TODO

Returns: TreeNode[]

getPreviousLeaf

public DefaultMutableTreeNode getPreviousLeaf()
Returns the previous leaf node before this tree node.

Returns: The previous leaf node, or null.

getPreviousNode

public DefaultMutableTreeNode getPreviousNode()
getPreviousNode

Returns: DefaultMutableTreeNode

getPreviousSibling

public DefaultMutableTreeNode getPreviousSibling()
Returns the previous sibling for this tree node. If this node has no parent, or this node is the first child of its parent, this method returns null.

Returns: The previous sibling, or null.

getRoot

public TreeNode getRoot()
Returns the root node by iterating the parents of this node.

Returns: the root node

getSharedAncestor

public TreeNode getSharedAncestor(DefaultMutableTreeNode node)
getSharedAncestor

Parameters: node TODO

Returns: TreeNode

getSiblingCount

public int getSiblingCount()
Returns the number of siblings for this tree node. If the tree node has a parent, this method returns the child count for the parent, otherwise it returns 1.

Returns: The sibling count.

getUserObject

public Object getUserObject()
Returns the user object attached to this node. null is returned when no user object is set.

Returns: the user object

getUserObjectPath

public Object[] getUserObjectPath()
getUserObjectPath

Returns: Object[]

insert

public void insert(MutableTreeNode node, int index)
Inserts given child node at the given index.

Parameters: node the child node (null not permitted). index the index.

Throws: IllegalArgumentException if node is null.

isLeaf

public boolean isLeaf()
Returns true if this tree node is a lead node (that is, it has no children), and false.

Returns: A boolean.

isNodeAncestor

public boolean isNodeAncestor(TreeNode node)
Returns true if node is an ancestor of this tree node, and false otherwise. An ancestor node is any of: If node is null, this method returns false.

Parameters: node the node (null permitted).

Returns: A boolean.

isNodeChild

public boolean isNodeChild(TreeNode node)
Returns true if node is a child of this tree node, and false otherwise. If node is null, this method returns false.

Parameters: node the node (null permitted).

Returns: A boolean.

isNodeDescendant

public boolean isNodeDescendant(DefaultMutableTreeNode node)
Returns true if node is a descendant of this tree node, and false otherwise. A descendant node is any of: If node is null, this method returns false.

Parameters: node the node (null permitted).

Returns: A boolean.

isNodeRelated

public boolean isNodeRelated(DefaultMutableTreeNode node)
isNodeRelated

Parameters: node TODO

Returns: boolean

isNodeSibling

public boolean isNodeSibling(TreeNode node)
Returns true if this tree node and node share the same parent. If node is this tree node, the method returns true and if node is null this method returns false.

Parameters: node the node (null permitted).

Returns: A boolean.

isRoot

public boolean isRoot()
Tells whether this node is the root node or not.

Returns: true if this is the root node, falseotherwise

pathFromAncestorEnumeration

public Enumeration pathFromAncestorEnumeration(TreeNode node)
pathFromAncestorEnumeration

Parameters: node TODO

Returns: Enumeration

postorderEnumeration

public Enumeration postorderEnumeration()
postorderEnumeration

Returns: Enumeration

preorderEnumeration

public Enumeration preorderEnumeration()
preorderEnumeration

Returns: Enumeration

remove

public void remove(int index)
Removes the child with the given index from this node.

Parameters: index the index (in the range 0 to getChildCount() - 1).

Throws: ArrayIndexOutOfBoundsException if index is outside the valid range.

remove

public void remove(MutableTreeNode node)
Removes the given child from this node and sets its parent to null.

Parameters: node the child node (null not permitted).

Throws: IllegalArgumentException if node is not a child of this node. IllegalArgumentException if node is null.

removeAllChildren

public void removeAllChildren()
Removes all child nodes from this node.

removeFromParent

public void removeFromParent()
Removes this node from its parent.

setAllowsChildren

public void setAllowsChildren(boolean allowsChildren)
Sets the flag that controls whether or not this node allows the addition / insertion of child nodes. If the flag is set to false, any existing children are removed.

Parameters: allowsChildren the flag.

setParent

public void setParent(MutableTreeNode node)
Set the parent node for this node.

Parameters: node the parent node

setUserObject

public void setUserObject(Object userObject)
Sets the user object for this node

Parameters: userObject the user object

toString

public String toString()
Returns a string representation of the node. This implementation returns getUserObject().toString(), or null if there is no user object.

Returns: A string representation of the node (possibly null).