home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / debug / databind.jar / javax / awt / swing / tree / TreePath.class (.txt) < prev   
Encoding:
Java Class File  |  1998-03-19  |  3.2 KB  |  136 lines

  1. package javax.awt.swing.tree;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.Serializable;
  7. import java.util.Vector;
  8.  
  9. public class TreePath implements Serializable {
  10.    protected transient Object[] path;
  11.  
  12.    public TreePath(Object[] path) {
  13.       if (path == null) {
  14.          throw new IllegalArgumentException("path in TreePath must be non null.");
  15.       } else {
  16.          this.path = path;
  17.       }
  18.    }
  19.  
  20.    public TreePath(Object singlePath) {
  21.       if (singlePath == null) {
  22.          throw new IllegalArgumentException("path in TreePath must be non null.");
  23.       } else {
  24.          this.path = new Object[1];
  25.          this.path[0] = singlePath;
  26.       }
  27.    }
  28.  
  29.    public Object[] getPath() {
  30.       int pathLength = this.path.length;
  31.       Object[] retPath = new Object[pathLength];
  32.       System.arraycopy(this.path, 0, retPath, 0, pathLength);
  33.       return retPath;
  34.    }
  35.  
  36.    public Object getLastPathComponent() {
  37.       return this.path.length > 0 ? this.path[this.path.length - 1] : null;
  38.    }
  39.  
  40.    public boolean equals(Object o) {
  41.       if (!(o instanceof TreePath)) {
  42.          return false;
  43.       } else {
  44.          TreePath oTreePath = (TreePath)o;
  45.          Object[] oPath = oTreePath.path;
  46.          if ((oPath == null || this.path != null) && (oPath != null || this.path == null)) {
  47.             if (this.path != null) {
  48.                int pathLength = this.path.length;
  49.                if (pathLength != oPath.length) {
  50.                   return false;
  51.                }
  52.  
  53.                for(int counter = 0; counter < pathLength; ++counter) {
  54.                   if (!this.path[counter].equals(oPath[counter])) {
  55.                      return false;
  56.                   }
  57.                }
  58.             }
  59.  
  60.             return true;
  61.          } else {
  62.             return false;
  63.          }
  64.       }
  65.    }
  66.  
  67.    public boolean isDescendant(TreePath aTreePath) {
  68.       if (this.path != null) {
  69.          Object[] otherPath = aTreePath.getPath();
  70.          int pathLength = this.path.length;
  71.          if (otherPath.length >= pathLength && pathLength > 0) {
  72.             for(int counter = 0; counter < pathLength; ++counter) {
  73.                if (!this.path[counter].equals(otherPath[counter])) {
  74.                   return false;
  75.                }
  76.             }
  77.  
  78.             return true;
  79.          }
  80.       }
  81.  
  82.       return false;
  83.    }
  84.  
  85.    public String toString() {
  86.       StringBuffer tempSpot = new StringBuffer("[");
  87.  
  88.       for(int counter = 0; counter < this.path.length; ++counter) {
  89.          if (counter > 0) {
  90.             tempSpot.append(", ");
  91.          }
  92.  
  93.          tempSpot.append(this.path[counter]);
  94.       }
  95.  
  96.       tempSpot.append("]");
  97.       return tempSpot.toString();
  98.    }
  99.  
  100.    private void writeObject(ObjectOutputStream s) throws IOException {
  101.       s.defaultWriteObject();
  102.       Vector values = new Vector();
  103.       boolean writePath = true;
  104.       int counter = 0;
  105.  
  106.       for(int maxCounter = this.path.length; counter < maxCounter; ++counter) {
  107.          if (!(this.path[counter] instanceof Serializable)) {
  108.             writePath = false;
  109.             break;
  110.          }
  111.       }
  112.  
  113.       if (writePath) {
  114.          values.addElement("path");
  115.          values.addElement(this.path);
  116.       }
  117.  
  118.       s.writeObject(values);
  119.    }
  120.  
  121.    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
  122.       s.defaultReadObject();
  123.       Vector values = (Vector)s.readObject();
  124.       int indexCounter = 0;
  125.       int maxCounter = values.size();
  126.       if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("path")) {
  127.          ++indexCounter;
  128.          this.path = values.elementAt(indexCounter);
  129.          ++indexCounter;
  130.       } else {
  131.          this.path = new Object[0];
  132.       }
  133.  
  134.    }
  135. }
  136.