javax.swing.tree
Class DefaultMutableTreeNode

java.lang.Object
  extended by javax.swing.tree.DefaultMutableTreeNode
All Implemented Interfaces:
Serializable, Cloneable, MutableTreeNode, TreeNode
Direct Known Subclasses:
JTree.DynamicUtilTreeNode

public class DefaultMutableTreeNode
extends Object
implements Cloneable, MutableTreeNode, Serializable

A default implementation of the MutableTreeNode interface.

See Also:
Serialized Form

Field Summary
protected  boolean allowsChildren
          allowsChildren
protected  Vector<MutableTreeNode> children
          The child nodes for this node (may be empty).
protected  MutableTreeNode parent
          The parent of this node (possibly null).
protected  Object userObject
          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
 void add(MutableTreeNode child)
          Adds a new child node to this node and sets this node as the parent of the child node.
 Enumeration breadthFirstEnumeration()
          breadthFirstEnumeration
 Enumeration children()
          Returns an enumeration containing all children of this node.
 Object clone()
          Returns a clone of the node.
 Enumeration depthFirstEnumeration()
          depthFirstEnumeration
 boolean getAllowsChildren()
          getAllowsChildren
 TreeNode getChildAfter(TreeNode node)
          Returns the next child after the specified node, or null if there is no child after the specified node.
 TreeNode getChildAt(int index)
          Returns the child node at a given index.
 TreeNode getChildBefore(TreeNode node)
          Returns the previous child before the specified node, or null if there is no child before the specified node.
 int getChildCount()
          Returns the number of children of this node.
 int getDepth()
          getDepth
 TreeNode getFirstChild()
          Returns the first child node belonging to this tree node.
 DefaultMutableTreeNode getFirstLeaf()
          Returns the first leaf node that is a descendant of this node.
 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.
 TreeNode getLastChild()
          Returns the last child node belonging to this tree node.
 DefaultMutableTreeNode getLastLeaf()
          Returns the last leaf node that is a descendant of this node.
 int getLeafCount()
          getLeafCount
 int getLevel()
          getLevel
 DefaultMutableTreeNode getNextLeaf()
          Returns the next leaf node after this tree node.
 DefaultMutableTreeNode getNextNode()
          getNextNode
 DefaultMutableTreeNode getNextSibling()
          Returns the next sibling for this tree node.
 TreeNode getParent()
          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
 DefaultMutableTreeNode getPreviousLeaf()
          Returns the previous leaf node before this tree node.
 DefaultMutableTreeNode getPreviousNode()
          getPreviousNode
 DefaultMutableTreeNode getPreviousSibling()
          Returns the previous sibling for this tree node.
 TreeNode getRoot()
          Returns the root node by iterating the parents of this node.
 TreeNode getSharedAncestor(DefaultMutableTreeNode node)
          getSharedAncestor
 int getSiblingCount()
          Returns the number of siblings for this tree node.
 Object getUserObject()
          Returns the user object attached to this node.
 Object[] getUserObjectPath()
          getUserObjectPath
 void insert(MutableTreeNode node, int index)
          Inserts given child node at the given index.
 boolean isLeaf()
          Returns true if this tree node is a lead node (that is, it has no children), and false.
 boolean isNodeAncestor(TreeNode node)
          Returns true if node is an ancestor of this tree node, and false otherwise.
 boolean isNodeChild(TreeNode node)
          Returns true if node is a child of this tree node, and false otherwise.
 boolean isNodeDescendant(DefaultMutableTreeNode node)
          Returns true if node is a descendant of this tree node, and false otherwise.
 boolean isNodeRelated(DefaultMutableTreeNode node)
          isNodeRelated
 boolean isNodeSibling(TreeNode node)
          Returns true if this tree node and node share the same parent.
 boolean isRoot()
          Tells whether this node is the root node or not.
 Enumeration pathFromAncestorEnumeration(TreeNode node)
          pathFromAncestorEnumeration
 Enumeration postorderEnumeration()
          postorderEnumeration
 Enumeration preorderEnumeration()
          preorderEnumeration
 void remove(int index)
          Removes the child with the given index from this node.
 void remove(MutableTreeNode node)
          Removes the given child from this node and sets its parent to null.
 void removeAllChildren()
          Removes all child nodes from this node.
 void removeFromParent()
          Removes this node from its parent.
 void setAllowsChildren(boolean allowsChildren)
          Sets the flag that controls whether or not this node allows the addition / insertion of child nodes.
 void setParent(MutableTreeNode node)
          Set the parent node for this node.
 void setUserObject(Object userObject)
          Sets the user object for this node
 String toString()
          Returns a string representation of the node.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parent

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


children

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


userObject

protected transient Object userObject
userObject


allowsChildren

protected boolean allowsChildren
allowsChildren

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

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.

Overrides:
clone in class Object
Returns:
A clone of the node.
See Also:
Cloneable

toString

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

Overrides:
toString in class Object
Returns:
A string representation of the node (possibly null).
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)

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 DefaultTreeModel, you must subsequently call DefaultTreeModel.reload(TreeNode).

Parameters:
child - the child node (null not permitted).
Throws:
IllegalStateException - if getAllowsChildren() returns false.
IllegalArgumentException - if isNodeAncestor(javax.swing.tree.TreeNode) returns true.
IllegalArgumentException - if child is null.

getParent

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

Specified by:
getParent in interface TreeNode
Returns:
The parent node (possibly null).

remove

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

Specified by:
remove in interface MutableTreeNode
Parameters:
index - the index (in the range 0 to getChildCount() - 1).
Throws:
ArrayIndexOutOfBoundsException - if index is outside the valid range.
See Also:
MutableTreeNode.insert(MutableTreeNode,int), MutableTreeNode.remove(MutableTreeNode), MutableTreeNode.removeFromParent()

remove

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

Specified by:
remove in interface MutableTreeNode
Parameters:
node - the child node (null not permitted).
Throws:
IllegalArgumentException - if node is not a child of this node.
IllegalArgumentException - if node is null.
See Also:
MutableTreeNode.insert(MutableTreeNode,int), MutableTreeNode.remove(int), MutableTreeNode.removeFromParent()

insert

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

Specified by:
insert in interface MutableTreeNode
Parameters:
node - the child node (null not permitted).
index - the index.
Throws:
IllegalArgumentException - if node is null.
See Also:
MutableTreeNode.remove(int), MutableTreeNode.remove(MutableTreeNode), MutableTreeNode.setParent(MutableTreeNode)

getPath

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

Returns:
an array of tree nodes

children

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

Specified by:
children in interface TreeNode
Returns:
an enumeration of tree nodes

setParent

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

Specified by:
setParent in interface MutableTreeNode
Parameters:
node - the parent node
See Also:
MutableTreeNode.insert(MutableTreeNode,int)

getChildAt

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

Specified by:
getChildAt in interface TreeNode
Parameters:
index - the index
Returns:
the child node

getChildCount

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

Specified by:
getChildCount in interface TreeNode
Returns:
the number of children

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.

Specified by:
getIndex in interface TreeNode
Parameters:
node - the node (null not permitted).
Returns:
The index of the specified child node, or -1.
Throws:
IllegalArgumentException - if node is null.

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.

getAllowsChildren

public boolean getAllowsChildren()
getAllowsChildren

Specified by:
getAllowsChildren in interface TreeNode
Returns:
boolean

setUserObject

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

Specified by:
setUserObject in interface MutableTreeNode
Parameters:
userObject - the user object

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

removeFromParent

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

Specified by:
removeFromParent in interface MutableTreeNode
See Also:
MutableTreeNode.remove(int), MutableTreeNode.remove(MutableTreeNode)

removeAllChildren

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


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.

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.

getSharedAncestor

public TreeNode getSharedAncestor(DefaultMutableTreeNode node)
getSharedAncestor

Parameters:
node - TODO
Returns:
TreeNode

isNodeRelated

public boolean isNodeRelated(DefaultMutableTreeNode node)
isNodeRelated

Parameters:
node - TODO
Returns:
boolean

getDepth

public int getDepth()
getDepth

Returns:
int

getLevel

public int getLevel()
getLevel

Returns:
int

getPathToRoot

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

Parameters:
node - TODO
depth - TODO
Returns:
TreeNode[]

getUserObjectPath

public Object[] getUserObjectPath()
getUserObjectPath

Returns:
Object[]

getRoot

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

Returns:
the root node

isRoot

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

Returns:
true if this is the root node, falseotherwise

getNextNode

public DefaultMutableTreeNode getNextNode()
getNextNode

Returns:
DefaultMutableTreeNode

getPreviousNode

public DefaultMutableTreeNode getPreviousNode()
getPreviousNode

Returns:
DefaultMutableTreeNode

preorderEnumeration

public Enumeration preorderEnumeration()
preorderEnumeration

Returns:
Enumeration

postorderEnumeration

public Enumeration postorderEnumeration()
postorderEnumeration

Returns:
Enumeration

breadthFirstEnumeration

public Enumeration breadthFirstEnumeration()
breadthFirstEnumeration

Returns:
Enumeration

depthFirstEnumeration

public Enumeration depthFirstEnumeration()
depthFirstEnumeration

Returns:
Enumeration

pathFromAncestorEnumeration

public Enumeration pathFromAncestorEnumeration(TreeNode node)
pathFromAncestorEnumeration

Parameters:
node - TODO
Returns:
Enumeration

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.

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.

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.

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.

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.

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.

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.

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.

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.

isLeaf

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

Specified by:
isLeaf in interface TreeNode
Returns:
A boolean.

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.

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.

getNextLeaf

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

Returns:
The next leaf node, or null.

getPreviousLeaf

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

Returns:
The previous leaf node, or null.

getLeafCount

public int getLeafCount()
getLeafCount

Returns:
int