| Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
| PREV | NEXT | SHOW LISTS | HIDE LISTS | ||
java.lang.Object
|
+----java.awt.swing.tree.DefaultMutableTreeNode
A DefaultMutableTreeNode is a general-purpose node in a tree data
structure. A tree node may have at most one parent and 0 or more children.
DefaultMutableTreeNode provides operations for examining and modifying a
node's parent and children and also operations for examining the tree that
the node is a part of. A node's tree is the set of all nodes that can be
reached by starting at the node and following all the possible links to
parents and children. A node with no parent is the root of its tree; a
node with no children is a leaf. A tree may consist of many subtrees,
each node acting as the root for its own subtree.
This class provides enumerations for efficiently traversing a tree or
subtree in various orders or for following the path between two nodes.
A DefaultMutableTreeNode may also hold a reference to a user object, the
use of which is left to the user. Asking a DefaultMutableTreeNode for its
string representation with toString() returns the string
representation of its user object.
This is not a thread safe class.If you intend to use a DefaultMutableTreeNode (or a tree of TreeNodes) in more than one thread, you need to do your own synchronizing. A good convention to adopt is synchronizing on the root node of a tree.
While DefaultMutableTreeNode implements the MutableTreeNode interface and will allow you to add in any implementation of MutableTreeNode not all of the methods in DefaultMutableTreeNode will be applicable to all MutableTreeNodes implementations. Especially with some of the enumerations that are provided, using some of these methods assumes the DefaultMutableTreeNode contains only DefaultMutableNodes instances. All of the TreeNode/MutableTreeNode methods will behave as defined no matter what implementations are added.Warning: serialized objects of this class will not be compatible with future swing releases. The current serialization support is appropriate for short term storage or RMI between Swing1.0 applications. It will not be possible to load serialized Swing1.0 objects with future releases of Swing. The JDK1.2 release of Swing will be the compatibility baseline for the serialized form of Swing objects.
| Field Summary | |
| boolean | allowsChildren
|
| Vector | children
|
| static Enumeration | EMPTY_ENUMERATION
|
| MutableTreeNode | parent
|
| Object | userObject
|
| Constructor Summary | |
| DefaultMutableTreeNode()
|
|
DefaultMutableTreeNode(Object userObject)
userObject allowing children.
|
|
DefaultMutableTreeNode(Object userObject,
boolean allowsChildren)
userObject and allows children set to
allowsChildren.
|
|
| Method Summary | |
| void | add(MutableTreeNode newChild)
newChild from its parent and makes it a child of
this node by adding it to the end of this node's child array.
|
| Enumeration | breadthFirstEnumeration()
|
| Enumeration | children()
|
| Object | clone()
|
| Enumeration | depthFirstEnumeration()
|
| boolean | getAllowsChildren()
|
| TreeNode | getChildAfter(TreeNode aChild)
aChild, which must be a child of this node.
|
| TreeNode | getChildAt(int index)
index in this node's child array.
|
| TreeNode | getChildBefore(TreeNode aChild)
aChild, which must be a child of this node.
|
| int | getChildCount()
|
| int | getDepth()
|
| TreeNode | getFirstChild()
|
| DefaultMutableTreeNode | getFirstLeaf()
|
| int | getIndex(TreeNode aChild)
aChild in this node's child array.
|
| TreeNode | getLastChild()
|
| DefaultMutableTreeNode | getLastLeaf()
|
| int | getLeafCount()
|
| int | getLevel()
|
| DefaultMutableTreeNode | getNextLeaf()
|
| DefaultMutableTreeNode | getNextNode()
|
| DefaultMutableTreeNode | getNextSibling()
|
| TreeNode | getParent()
|
| TreeNode[] | getPath()
|
| TreeNode[] | getPathToRoot(TreeNode aNode,
int depth)
|
| DefaultMutableTreeNode | getPreviousLeaf()
|
| DefaultMutableTreeNode | getPreviousNode()
|
| DefaultMutableTreeNode | getPreviousSibling()
|
| TreeNode | getRoot()
|
| TreeNode | getSharedAncestor(DefaultMutableTreeNode aNode)
aNode.
|
| int | getSiblingCount()
|
| Object | getUserObject()
|
| Object[] | getUserObjectPath()
|
| void | insert(MutableTreeNode newChild,
int childIndex)
newChild from its present parent (if it has a
parent), sets the child's parent to this node, and then adds the child
to this node's child array at index childIndex.
|
| boolean | isLeaf()
|
| boolean | isNodeAncestor(TreeNode anotherNode)
anotherNode is an ancestor of this node,
i.e.
|
| boolean | isNodeChild(TreeNode aNode)
aNode is a child of this node.
|
| boolean | isNodeDescendant(DefaultMutableTreeNode anotherNode)
anotherNode is a descendant of this node;
i.e.
|
| boolean | isNodeRelated(DefaultMutableTreeNode aNode)
aNode is in the same tree
as this node.
|
| boolean | isNodeSibling(TreeNode anotherNode)
anotherNode is a sibling of (i.e.
|
| boolean | isRoot()
|
| Enumeration | pathFromAncestorEnumeration(TreeNode ancestor)
ancestor to this node.
|
| Enumeration | postorderEnumeration()
|
| Enumeration | preorderEnumeration()
|
| void | remove(int childIndex)
childIndex from this node's children
index and sets its parent to null.
|
| void | remove(MutableTreeNode aChild)
aChild from this node's child array, giving it a
null parent.
|
| void | removeAllChildren()
|
| void | removeFromParent()
|
| void | setAllowsChildren(boolean allows)
|
| void | setParent(MutableTreeNode newParent)
newParent but does not change
the parent's child array.
|
| void | setUserObject(Object userObject)
userObject.
|
| String | toString()
toString() to this node's
user object, or null if this node has no user object.
|
| Methods inherited from class java.lang.Object |
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final Enumeration EMPTY_ENUMERATION
protected MutableTreeNode parent
protected Vector children
protected transient Object userObject
protected boolean allowsChildren
| Constructor Detail |
public DefaultMutableTreeNode()
public DefaultMutableTreeNode(Object userObject)
userObject allowing children.
public DefaultMutableTreeNode(Object userObject,
boolean allowsChildren)
userObject and allows children set to
allowsChildren.| Method Detail |
public void insert(MutableTreeNode newChild,
int childIndex)
newChild from its present parent (if it has a
parent), sets the child's parent to this node, and then adds the child
to this node's child array at index childIndex.
newChild must not be null and must not be an ancestor of
this node.
newChild
- the node to become a child of this node
childIndex
- the index in this node's child array
where this node should be inserted
childIndex is out of boundsnewChild is null or is an
ancestor of this nodepublic void remove(int childIndex)
childIndex from this node's children
index and sets its parent to null. setParent is messaged
to the child, meaning it must be a MutableTreeNode.
childIndex
- the index in this node's child array
of the child to remove
childIndex is out of boundspublic void setParent(MutableTreeNode newParent)
newParent but does not change
the parent's child array. This method is sent from
insert() and remove() to
reassign a child's parent, it should not be messaged from anywhere
else.
newParent
- this node's new parent
public TreeNode getParent()
public TreeNode getChildAt(int index)
index in this node's child array.
index
- an index into this node's child array
indexindex
is out of boundspublic int getChildCount()
public int getIndex(TreeNode aChild)
aChild in this node's child array.
If aChild is not a child of this node, returns
-1. This method performs a linear search and is O(n)
where n is the number of children.
aChild
- a child to search this node's children for
aChild,
or -1aChild
is nullpublic Enumeration children()
public void setAllowsChildren(boolean allows)
allows is false, all of this node's children are
removed.By default, a node allows children.
allows
- true if and only iff this node should be allowed to
have children
public boolean getAllowsChildren()
public void setUserObject(Object userObject)
userObject.
userObject
- this node's new user object
public Object getUserObject()
public void removeFromParent()
public void remove(MutableTreeNode aChild)
aChild from this node's child array, giving it a
null parent.
aChild
- a child of this node to remove
aChild
is null or is not a child of this nodepublic void removeAllChildren()
public void add(MutableTreeNode newChild)
newChild from its parent and makes it a child of
this node by adding it to the end of this node's child array.
aNode
- node to add as a child of this node
newChild
is nullpublic boolean isNodeAncestor(TreeNode anotherNode)
anotherNode is an ancestor of this node,
i.e. if it is this node, this node's parent, or an ancestor of this
node's parent. Note that a node is considered an ancestor of itself.
If anotherNode is null, this method returns false. This
operation is at worst O(h) where h is the distance from the root to
this node.
anotherNode
- node to test as an ancestor of this node
anotherNodepublic boolean isNodeDescendant(DefaultMutableTreeNode anotherNode)
anotherNode is a descendant of this node;
i.e. 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, returns
false. This operation is at worst O(h) where h is the distance from the
root to anotherNode.
anotherNode
- node to test as descendant of this node
anotherNodepublic TreeNode getSharedAncestor(DefaultMutableTreeNode aNode)
aNode.
Returns null, if no such ancestor exists, i.e. if this node and
aNode are in different trees or if aNode is
null. A node is considered an ancestor of itself.
aNode
- node to find common ancestor with
aNode,
or null if nonepublic boolean isNodeRelated(DefaultMutableTreeNode aNode)
aNode is in the same tree
as this node. Returns false if aNode is null.aNode is in the same tree as this node;
false if aNode is nullpublic int getDepth()
getLevel() because it must effectively traverse the entire
tree rooted at this node.public int getLevel()
public TreeNode[] getPath()
protected TreeNode[] getPathToRoot(TreeNode aNode,
int depth)
public Object[] getUserObjectPath()
public TreeNode getRoot()
public boolean isRoot()
public DefaultMutableTreeNode getNextNode()
public DefaultMutableTreeNode getPreviousNode()
public Enumeration preorderEnumeration()
nextElement() method is this node.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
public Enumeration postorderEnumeration()
nextElement() method is the leftmost leaf. This is the
same as a depth-first traversal.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
public Enumeration breadthFirstEnumeration()
nextElement() method is this node.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
public Enumeration depthFirstEnumeration()
nextElement() method is the leftmost leaf.
This is the same as a postorder traversal.Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
public Enumeration pathFromAncestorEnumeration(TreeNode ancestor)
ancestor to this node. The enumeration's
nextElement() method first returns ancestor,
then the child of ancestor that is an ancestor of this
node, and so on, and finally returns this node. Creation of the
enumeration is O(m) where m is the number of nodes between this node
and ancestor, inclusive. Each nextElement()
message is O(1).Modifying the tree by inserting, removing, or moving a node invalidates any enumerations created before the modification.
ancestor is
not an ancestor of this nodepublic boolean isNodeChild(TreeNode aNode)
aNode is a child of this node. If
aNode is null, this method returns false.aNode is a child of this node; false if
aNode is nullpublic TreeNode getFirstChild()
public TreeNode getLastChild()
public TreeNode getChildAfter(TreeNode aChild)
aChild, which must be a child of this node. If
aChild is the last child, returns null. This method
performs a linear search of this node's children for
aChild and is O(n) where n is the number of children; to
traverse the entire array of children, use an enumeration instead.aChildaChild is
null or is not a child of this nodepublic TreeNode getChildBefore(TreeNode aChild)
aChild, which must be a child of this node. If
aChild is the first child, returns null. This method
performs a linear search of this node's children for aChild
and is O(n) where n is the number of children.aChildaChild is null
or is not a child of this nodepublic boolean isNodeSibling(TreeNode anotherNode)
anotherNode is a sibling of (i.e. has the
same parent as) this node. A node is its own sibling. If
anotherNode is null, returns false.
anotherNode
- node to test as sibling of this node
anotherNode is a sibling of this nodepublic int getSiblingCount()
1).public DefaultMutableTreeNode getNextSibling()
public DefaultMutableTreeNode getPreviousSibling()
public boolean isLeaf()
getAllowsChildrenpublic DefaultMutableTreeNode getFirstLeaf()
public DefaultMutableTreeNode getLastLeaf()
public DefaultMutableTreeNode getNextLeaf()
isLeaf() instead. PENDING: better description?public DefaultMutableTreeNode getPreviousLeaf()
isLeaf() instead. PENDING: better
description?public int getLeafCount()
1. This method is O(n)
where n is the number of descendants of this node.public String toString()
toString() to this node's
user object, or null if this node has no user object.public Object clone()
| Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
| PREV | NEXT | SHOW LISTS | HIDE LISTS | ||