Constructors Summary |
---|
public TreePath(Object[] path)Constructs a path from an array of Objects, uniquely identifying
the path from the root of the tree to a specific node, as returned
by the tree's data model.
The model is free to return an array of any Objects it needs to
represent the path. The DefaultTreeModel returns an array of
TreeNode objects. The first TreeNode in the path is the root of the
tree, the last TreeNode is the node identified by the path.
if(path == null || path.length == 0)
throw new IllegalArgumentException("path in TreePath must be non null and not empty.");
lastPathComponent = path[path.length - 1];
if(path.length > 1)
parentPath = new TreePath(path, path.length - 1);
|
public TreePath(Object singlePath)Constructs a TreePath containing only a single element. This is
usually used to construct a TreePath for the the root of the TreeModel.
if(singlePath == null)
throw new IllegalArgumentException("path in TreePath must be non null.");
lastPathComponent = singlePath;
parentPath = null;
|
protected TreePath(TreePath parent, Object lastElement)Constructs a new TreePath, which is the path identified by
parent ending in lastElement .
if(lastElement == null)
throw new IllegalArgumentException("path in TreePath must be non null.");
parentPath = parent;
lastPathComponent = lastElement;
|
protected TreePath(Object[] path, int length)Constructs a new TreePath with the identified path components of
length length .
lastPathComponent = path[length - 1];
if(length > 1)
parentPath = new TreePath(path, length - 1);
|
protected TreePath()Primarily provided for subclasses
that represent paths in a different manner.
If a subclass uses this constructor, it should also override
the getPath ,
getPathCount , and
getPathComponent methods,
and possibly the equals method.
|
Methods Summary |
---|
public boolean | equals(java.lang.Object o)Tests two TreePaths for equality by checking each element of the
paths for equality. Two paths are considered equal if they are of
the same length, and contain
the same elements (.equals ).
if(o == this)
return true;
if(o instanceof TreePath) {
TreePath oTreePath = (TreePath)o;
if(getPathCount() != oTreePath.getPathCount())
return false;
for(TreePath path = this; path != null; path = path.parentPath) {
if (!(path.lastPathComponent.equals
(oTreePath.lastPathComponent))) {
return false;
}
oTreePath = oTreePath.parentPath;
}
return true;
}
return false;
|
public java.lang.Object | getLastPathComponent()Returns the last component of this path. For a path returned by
DefaultTreeModel this will return an instance of TreeNode.
return lastPathComponent;
|
public javax.swing.tree.TreePath | getParentPath()Returns a path containing all the elements of this object, except
the last path component.
return parentPath;
|
public java.lang.Object[] | getPath()Returns an ordered array of Objects containing the components of this
TreePath. The first element (index 0) is the root.
int i = getPathCount();
Object[] result = new Object[i--];
for(TreePath path = this; path != null; path = path.parentPath) {
result[i--] = path.lastPathComponent;
}
return result;
|
public java.lang.Object | getPathComponent(int element)Returns the path component at the specified index.
int pathLength = getPathCount();
if(element < 0 || element >= pathLength)
throw new IllegalArgumentException("Index " + element + " is out of the specified range");
TreePath path = this;
for(int i = pathLength-1; i != element; i--) {
path = path.parentPath;
}
return path.lastPathComponent;
|
public int | getPathCount()Returns the number of elements in the path.
int result = 0;
for(TreePath path = this; path != null; path = path.parentPath) {
result++;
}
return result;
|
public int | hashCode()Returns the hashCode for the object. The hash code of a TreePath
is defined to be the hash code of the last component in the path.
return lastPathComponent.hashCode();
|
public boolean | isDescendant(javax.swing.tree.TreePath aTreePath)Returns true if aTreePath is a
descendant of this
TreePath. A TreePath P1 is a descendant of a TreePath P2
if P1 contains all of the components that make up
P2's path.
For example, if this object has the path [a, b],
and aTreePath has the path [a, b, c],
then aTreePath is a descendant of this object.
However, if aTreePath has the path [a],
then it is not a descendant of this object. By this definition
a TreePath is always considered a descendant of itself. That is,
aTreePath.isDescendant(aTreePath) returns true.
if(aTreePath == this)
return true;
if(aTreePath != null) {
int pathLength = getPathCount();
int oPathLength = aTreePath.getPathCount();
if(oPathLength < pathLength)
// Can't be a descendant, has fewer components in the path.
return false;
while(oPathLength-- > pathLength)
aTreePath = aTreePath.getParentPath();
return equals(aTreePath);
}
return false;
|
public javax.swing.tree.TreePath | pathByAddingChild(java.lang.Object child)Returns a new path containing all the elements of this object
plus child . child will be the last element
of the newly created TreePath.
This will throw a NullPointerException
if child is null.
if(child == null)
throw new NullPointerException("Null child not allowed");
return new TreePath(this, child);
|
private void | readObject(java.io.ObjectInputStream s)
s.defaultReadObject();
Vector values = (Vector)s.readObject();
int indexCounter = 0;
int maxCounter = values.size();
if(indexCounter < maxCounter && values.elementAt(indexCounter).
equals("lastPathComponent")) {
lastPathComponent = values.elementAt(++indexCounter);
indexCounter++;
}
|
public java.lang.String | toString()Returns a string that displays and identifies this
object's properties.
StringBuffer tempSpot = new StringBuffer("[");
for(int counter = 0, maxCounter = getPathCount();counter < maxCounter;
counter++) {
if(counter > 0)
tempSpot.append(", ");
tempSpot.append(getPathComponent(counter));
}
tempSpot.append("]");
return tempSpot.toString();
|
private void | writeObject(java.io.ObjectOutputStream s)
s.defaultWriteObject();
Vector values = new Vector();
boolean writePath = true;
if(lastPathComponent != null &&
(lastPathComponent instanceof Serializable)) {
values.addElement("lastPathComponent");
values.addElement(lastPathComponent);
}
s.writeObject(values);
|