DefaultMutableTreeNode.java [plain text]
package javax.swing.tree;
import gnu.java.util.EmptyEnumeration;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Stack;
import java.util.Vector;
public class DefaultMutableTreeNode
implements Cloneable, MutableTreeNode, Serializable
{
private static final long serialVersionUID = -4298474751201349152L;
public static final Enumeration EMPTY_ENUMERATION =
EmptyEnumeration.getInstance();
protected MutableTreeNode parent;
protected Vector children = new Vector();
protected transient Object userObject;
protected boolean allowsChildren;
public DefaultMutableTreeNode()
{
this(null, true);
}
public DefaultMutableTreeNode(Object userObject)
{
this(userObject, true);
}
public DefaultMutableTreeNode(Object userObject, boolean allowsChildren)
{
this.userObject = userObject;
this.allowsChildren = allowsChildren;
}
public Object clone()
{
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
}
public String toString()
{
if (userObject == null)
return null;
return userObject.toString();
}
public void add(MutableTreeNode child)
{
if (child == null)
throw new IllegalArgumentException();
if (! allowsChildren)
throw new IllegalStateException();
children.add(child);
child.setParent(this);
}
public TreeNode getParent()
{
return parent;
}
public void remove(int index)
{
children.remove(index);
}
public void remove(MutableTreeNode node)
{
children.remove(node);
}
private void writeObject(ObjectOutputStream stream)
throws IOException
{
}
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
}
public void insert(MutableTreeNode node, int index)
{
children.insertElementAt(node, index);
}
public TreeNode[] getPath()
{
return getPathToRoot(this, 0);
}
public Enumeration children()
{
if (children.size() == 0)
return EMPTY_ENUMERATION;
return children.elements();
}
public void setParent(MutableTreeNode node)
{
parent = node;
}
public TreeNode getChildAt(int index)
{
return (TreeNode) children.elementAt(index);
}
public int getChildCount()
{
return children.size();
}
public int getIndex(TreeNode node)
{
return children.indexOf(node);
}
public void setAllowsChildren(boolean allowsChildren)
{
this.allowsChildren = allowsChildren;
}
public boolean getAllowsChildren()
{
return allowsChildren;
}
public void setUserObject(Object userObject)
{
this.userObject = userObject;
}
public Object getUserObject()
{
return userObject;
}
public void removeFromParent()
{
parent = null;
}
public void removeAllChildren()
{
children.removeAllElements();
}
public boolean isNodeAncestor(TreeNode node)
{
if (node == null)
return false;
TreeNode current = this;
while (current != null
&& current != node)
current = current.getParent();
return current == node;
}
public boolean isNodeDescendant(DefaultMutableTreeNode node)
{
if (node == null)
return false;
TreeNode current = node;
while (current != null
&& current != this)
current = current.getParent();
return current == this;
}
public TreeNode getSharedAncestor(DefaultMutableTreeNode node)
{
TreeNode current = this;
ArrayList list = new ArrayList();
while (current != null)
{
list.add(current);
current = current.getParent();
}
current = node;
while (current != null)
{
if (list.contains(current))
return current;
current = current.getParent();
}
return null;
}
public boolean isNodeRelated(DefaultMutableTreeNode node)
{
if (node == null)
return false;
return node.getRoot() == getRoot();
}
public int getDepth()
{
if ((! allowsChildren)
|| children.size() == 0)
return 0;
Stack stack = new Stack();
stack.push(new Integer(0));
TreeNode node = getChildAt(0);
int depth = 0;
int current = 1;
while (! stack.empty())
{
if (node.getChildCount() != 0)
{
node = node.getChildAt(0);
stack.push(new Integer(0));
current++;
}
else
{
if (current > depth)
depth = current;
int size;
int index;
do
{
node = node.getParent();
size = node.getChildCount();
index = ((Integer) stack.pop()).intValue() + 1;
current--;
}
while (index >= size
&& node != this);
if (index < size)
{
node = node.getChildAt(index);
stack.push(new Integer(index));
current++;
}
}
}
return depth;
}
public int getLevel()
{
int count = -1;
TreeNode current = this;
do
{
current = current.getParent();
count++;
}
while (current != null);
return count;
}
protected TreeNode[] getPathToRoot(TreeNode node, int depth)
{
if (node == null)
{
if (depth == 0)
return null;
return new TreeNode[depth];
}
TreeNode[] path = getPathToRoot(node.getParent(), depth + 1);
path[path.length - depth - 1] = node;
return path;
}
public Object[] getUserObjectPath()
{
TreeNode[] path = getPathToRoot(this, 0);
Object[] object = new Object[path.length];
for (int index = 0; index < path.length; ++index)
object[index] = ((DefaultMutableTreeNode) path[index]).getUserObject();
return object;
}
public TreeNode getRoot()
{
TreeNode current = this;
TreeNode check = current.getParent();
while (check != null)
{
current = check;
check = current.getParent();
}
return current;
}
public boolean isRoot()
{
return parent == null;
}
public DefaultMutableTreeNode getNextNode()
{
if (getChildCount() != 0)
return (DefaultMutableTreeNode) getChildAt(0);
DefaultMutableTreeNode node = this;
DefaultMutableTreeNode sibling;
do
{
sibling = node.getNextSibling();
node = (DefaultMutableTreeNode) node.getParent();
}
while (sibling == null &&
node != null);
return sibling;
}
public DefaultMutableTreeNode getPreviousNode()
{
if (parent == null)
return null;
DefaultMutableTreeNode sibling = getPreviousSibling();
if (sibling == null)
return (DefaultMutableTreeNode) parent;
if (sibling.getChildCount() != 0)
return sibling.getLastLeaf();
return sibling;
}
public Enumeration preorderEnumeration()
{
return null; }
public Enumeration postorderEnumeration()
{
return null; }
public Enumeration breadthFirstEnumeration()
{
return null; }
public Enumeration depthFirstEnumeration()
{
return postorderEnumeration();
}
public Enumeration pathFromAncestorEnumeration(TreeNode node)
{
if (node == null)
throw new IllegalArgumentException();
TreeNode parent = this;
Vector nodes = new Vector();
nodes.add(this);
while (parent != node && parent != null)
{
parent = parent.getParent();
nodes.add(0, parent);
}
if (parent != node)
throw new IllegalArgumentException();
return nodes.elements();
}
public boolean isNodeChild(TreeNode node)
{
if (node == null)
return false;
return node.getParent() == this;
}
public TreeNode getFirstChild()
{
return (TreeNode) children.firstElement();
}
public TreeNode getLastChild()
{
return (TreeNode) children.lastElement();
}
public TreeNode getChildAfter(TreeNode node)
{
if (node == null
|| node.getParent() != this)
throw new IllegalArgumentException();
int index = getIndex(node) + 1;
if (index == getChildCount())
return null;
return getChildAt(index);
}
public TreeNode getChildBefore(TreeNode node)
{
if (node == null
|| node.getParent() != this)
throw new IllegalArgumentException();
int index = getIndex(node) - 1;
if (index < 0)
return null;
return getChildAt(index);
}
public boolean isNodeSibling(TreeNode node)
{
if (node == null)
return false;
return (node.getParent() == getParent()
&& getParent() != null);
}
public int getSiblingCount()
{
if (parent == null)
return 1;
return parent.getChildCount();
}
public DefaultMutableTreeNode getNextSibling()
{
if (parent == null)
return null;
int index = parent.getIndex(this) + 1;
if (index == parent.getChildCount())
return null;
return (DefaultMutableTreeNode) parent.getChildAt(index);
}
public DefaultMutableTreeNode getPreviousSibling()
{
if (parent == null)
return null;
int index = parent.getIndex(this) - 1;
if (index < 0)
return null;
return (DefaultMutableTreeNode) parent.getChildAt(index);
}
public boolean isLeaf()
{
return children.size() == 0;
}
public DefaultMutableTreeNode getFirstLeaf()
{
TreeNode current = this;
while (current.getChildCount() > 0)
current = current.getChildAt(0);
return (DefaultMutableTreeNode) current;
}
public DefaultMutableTreeNode getLastLeaf()
{
TreeNode current = this;
int size = current.getChildCount();
while (size > 0)
{
current = current.getChildAt(size - 1);
size = current.getChildCount();
}
return (DefaultMutableTreeNode) current;
}
public DefaultMutableTreeNode getNextLeaf()
{
if (parent == null)
return null;
return null;
}
public DefaultMutableTreeNode getPreviousLeaf()
{
if (parent == null)
return null;
return null;
}
public int getLeafCount()
{
int count = 0;
Enumeration e = depthFirstEnumeration();
while (e.hasMoreElements())
{
TreeNode current = (TreeNode) e.nextElement();
if (current.isLeaf())
count++;
}
return count;
}
}