home *** CD-ROM | disk | FTP | other *** search
- package javax.awt.swing.tree;
-
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.Vector;
-
- public class TreePath implements Serializable {
- protected transient Object[] path;
-
- public TreePath(Object[] path) {
- if (path == null) {
- throw new IllegalArgumentException("path in TreePath must be non null.");
- } else {
- this.path = path;
- }
- }
-
- public TreePath(Object singlePath) {
- if (singlePath == null) {
- throw new IllegalArgumentException("path in TreePath must be non null.");
- } else {
- this.path = new Object[1];
- this.path[0] = singlePath;
- }
- }
-
- public Object[] getPath() {
- int pathLength = this.path.length;
- Object[] retPath = new Object[pathLength];
- System.arraycopy(this.path, 0, retPath, 0, pathLength);
- return retPath;
- }
-
- public Object getLastPathComponent() {
- return this.path.length > 0 ? this.path[this.path.length - 1] : null;
- }
-
- public boolean equals(Object o) {
- if (!(o instanceof TreePath)) {
- return false;
- } else {
- TreePath oTreePath = (TreePath)o;
- Object[] oPath = oTreePath.path;
- if ((oPath == null || this.path != null) && (oPath != null || this.path == null)) {
- if (this.path != null) {
- int pathLength = this.path.length;
- if (pathLength != oPath.length) {
- return false;
- }
-
- for(int counter = 0; counter < pathLength; ++counter) {
- if (!this.path[counter].equals(oPath[counter])) {
- return false;
- }
- }
- }
-
- return true;
- } else {
- return false;
- }
- }
- }
-
- public boolean isDescendant(TreePath aTreePath) {
- if (this.path != null) {
- Object[] otherPath = aTreePath.getPath();
- int pathLength = this.path.length;
- if (otherPath.length >= pathLength && pathLength > 0) {
- for(int counter = 0; counter < pathLength; ++counter) {
- if (!this.path[counter].equals(otherPath[counter])) {
- return false;
- }
- }
-
- return true;
- }
- }
-
- return false;
- }
-
- public String toString() {
- StringBuffer tempSpot = new StringBuffer("[");
-
- for(int counter = 0; counter < this.path.length; ++counter) {
- if (counter > 0) {
- tempSpot.append(", ");
- }
-
- tempSpot.append(this.path[counter]);
- }
-
- tempSpot.append("]");
- return tempSpot.toString();
- }
-
- private void writeObject(ObjectOutputStream s) throws IOException {
- s.defaultWriteObject();
- Vector values = new Vector();
- boolean writePath = true;
- int counter = 0;
-
- for(int maxCounter = this.path.length; counter < maxCounter; ++counter) {
- if (!(this.path[counter] instanceof Serializable)) {
- writePath = false;
- break;
- }
- }
-
- if (writePath) {
- values.addElement("path");
- values.addElement(this.path);
- }
-
- s.writeObject(values);
- }
-
- private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
- s.defaultReadObject();
- Vector values = (Vector)s.readObject();
- int indexCounter = 0;
- int maxCounter = values.size();
- if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("path")) {
- ++indexCounter;
- this.path = values.elementAt(indexCounter);
- ++indexCounter;
- } else {
- this.path = new Object[0];
- }
-
- }
- }
-