javax.swing.tree

Class DefaultMutableTreeNode

Implemented Interfaces:
Cloneable, MutableTreeNode, Serializable, TreeNode
Known Direct 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

static Enumeration
EMPTY_ENUMERATION
An empty enumeration, returned by children() if a node has no children.
protected boolean
allowsChildren
allowsChildren
protected Vector
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<E>
breadthFirstEnumeration()
breadthFirstEnumeration
Enumeration<E>
children()
Returns an enumeration containing all children of this node.
Object
clone()
Returns a clone of the node.
Enumeration<E>
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<E>
pathFromAncestorEnumeration(TreeNode node)
pathFromAncestorEnumeration
Enumeration<E>
postorderEnumeration()
postorderEnumeration
Enumeration<E>
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

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

EMPTY_ENUMERATION

public static final Enumeration EMPTY_ENUMERATION
An empty enumeration, returned by children() if a node has no children.

allowsChildren

protected boolean allowsChildren
allowsChildren

children

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

parent

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

userObject

protected Object userObject
userObject

Constructor Details

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 Details

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).

breadthFirstEnumeration

public Enumeration<E> breadthFirstEnumeration()
breadthFirstEnumeration
Returns:
Enumeration

children

public Enumeration<E> 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

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 interface Object
Returns:
A clone of the node.

depthFirstEnumeration

public Enumeration<E> depthFirstEnumeration()
depthFirstEnumeration
Returns:
Enumeration

getAllowsChildren

public boolean getAllowsChildren()
getAllowsChildren
Specified by:
getAllowsChildren in interface TreeNode
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.
Specified by:
getChildAt in interface TreeNode
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.
Specified by:
getChildCount in interface TreeNode
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.
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.

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.
Specified by:
getParent in interface TreeNode
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.
Specified by:
insert in interface MutableTreeNode
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.
Specified by:
isLeaf in interface TreeNode
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:
  • this tree node;
  • the parent node (if there is one);
  • any ancestor of the parent node;
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:
  • this tree node;
  • the child nodes belonging to this tree node, if there are any;
  • any descendants of the child nodes;
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<E> pathFromAncestorEnumeration(TreeNode node)
pathFromAncestorEnumeration
Parameters:
node - TODO
Returns:
Enumeration

postorderEnumeration

public Enumeration<E> postorderEnumeration()
postorderEnumeration
Returns:
Enumeration

preorderEnumeration

public Enumeration<E> preorderEnumeration()
preorderEnumeration
Returns:
Enumeration

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.

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.

removeAllChildren

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

removeFromParent

public void removeFromParent()
Removes this node from its parent.
Specified by:
removeFromParent in interface MutableTreeNode

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.
Specified by:
setParent in interface MutableTreeNode
Parameters:
node - the parent node

setUserObject

public void setUserObject(Object userObject)
Sets the user object for this node
Specified by:
setUserObject in interface MutableTreeNode
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.
Overrides:
toString in interface Object
Returns:
A string representation of the node (possibly null).

DefaultMutableTreeNode.java -- Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.