home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / TreePath.java < prev    next >
Text File  |  1998-11-09  |  8KB  |  248 lines

  1. /*
  2.  * @(#)TreePath.java    1.15 98/02/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package javax.awt.swing.tree;
  22.  
  23. import java.io.*;
  24. import java.util.Vector;
  25.  
  26. /**
  27.  * Represents a path to a node. TreePath is Serializable, but if any 
  28.  * components of the path are not serializable, it will not be written 
  29.  * out.
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate 
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  *
  38.  * @version 1.15 02/05/98
  39.  * @author Scott Violet
  40.  */
  41. public class TreePath extends Object implements Serializable
  42. {
  43.     /** Path this instance represents. */
  44.     transient protected Object[]        path;
  45.  
  46.     /**
  47.      * Constructs a path from an array of Objects, uniquely identifying 
  48.      * the path from the root of the tree to a specific node, as returned
  49.      * by the tree's data model.
  50.      * <p>
  51.      * The model is free to return an array of any Objects it needs to 
  52.      * represent the path. The DefaultTreeModel returns an array of 
  53.      * TreeNode objects. The first TreeNode in the path is the root of the
  54.      * tree, the last TreeNode is the node identified by the path.
  55.      *
  56.      * @param path  an array of Objects representing the path to a node
  57.      */
  58.     public TreePath(Object[] path) {
  59.         if(path == null)
  60.             throw new IllegalArgumentException("path in TreePath must be non null.");
  61.         this.path = path;
  62.     }
  63.  
  64.     /**
  65.      * Constructs a TreePath when there is only item in the path.
  66.      * <p>
  67.      * @param singlePath  an Object representing the path to a node
  68.      * @see #TreePath(Object[])
  69.      */
  70.     public TreePath(Object singlePath) {
  71.         if(singlePath == null)
  72.             throw new IllegalArgumentException("path in TreePath must be non null.");
  73.         path = new Object[1];
  74.         path[0] = singlePath;
  75.     }
  76.  
  77.     /**
  78.      * Returns an array of Objects containing the components of this
  79.      * TreePath.
  80.      *
  81.      * @return an array of Objects representing the TreePath
  82.      * @see #TreePath(Object[])
  83.      */
  84.     public Object[] getPath() {
  85.         int             pathLength = path.length;
  86.         Object[]        retPath = new Object[pathLength];
  87.  
  88.         System.arraycopy(path, 0, retPath, 0, pathLength);
  89.         return retPath;
  90.     }
  91.  
  92.     /**
  93.      * Returns the last component of this path. For a path
  94.      * returned by the DefaultTreeModel, that is the TreeNode object
  95.      * for the node specified by the path.
  96.      *
  97.      * @return the Object at the end of the path
  98.      * @see #TreePath(Object[])
  99.      */
  100.     public Object getLastPathComponent() {
  101.         if(path.length > 0)
  102.             return path[path.length - 1];
  103.         return null;
  104.     }
  105.  
  106.     /**
  107.      * Returns the number of elements in the path.
  108.      *
  109.      * @return an int giving a count of items the path
  110.      */
  111.     public int getPathCount() {
  112.         return path.length;
  113.     }
  114.  
  115.     /**
  116.      * Returns the path component at the specified index.
  117.      *
  118.      * @param element  an int specifying an element in the path, where
  119.      *                 0 is the first element in the path
  120.      * @return the Object at that index location
  121.      * @throws IllegalArgumentException if the index is beyond the length
  122.      *         of the path
  123.      * @see #TreePath(Object[])
  124.      */
  125.     public Object getPathComponent(int element) {
  126.         if(element < 0 || element >= getPathCount())
  127.             throw new IllegalArgumentException("Index " + element + " is greater than path length");
  128.         return path[element];
  129.     }
  130.  
  131.     /**
  132.      * Tests two TreePaths for equality by checking each element of the
  133.      * paths for equality.
  134.      *
  135.      * @param o the Object to compare
  136.      */
  137.     public boolean equals(Object o) {
  138.         if(o instanceof TreePath) {
  139.             TreePath            oTreePath = (TreePath)o;
  140.             Object[]             oPath = oTreePath.path;
  141.  
  142.             if((oPath != null && path == null) ||
  143.                (oPath == null && path != null))
  144.                 return false;
  145.             if(path != null) {
  146.                 int        pathLength = path.length;
  147.  
  148.                 if(pathLength != oPath.length)
  149.                     return false;
  150.                 for(int counter = 0; counter < pathLength; counter++)
  151.                     if(!path[counter].equals(oPath[counter]))
  152.                         return false;
  153.             }
  154.             return true;
  155.         }
  156.         return false;
  157.     }
  158.  
  159.     /**
  160.      * Returns the hashCode for the object.  This must be defined
  161.      * here to ensure 100% pure.
  162.      *
  163.      * @return the hashCode for the object
  164.      */
  165.     public int hashCode() { 
  166.     return super.hashCode();
  167.     }
  168.  
  169.     /**
  170.      * Returns true if the specified node is a descendant of this
  171.      * TreePath. A TreePath, child, is a descendent of another TreePath,
  172.      * parent, if child contains all of the components that make up 
  173.      * parent's path.
  174.      *
  175.      * @return true if aTreePath is a descendant of the receiver.
  176.      */
  177.     public boolean isDescendant(TreePath aTreePath) {
  178.         if(path != null) {
  179.             Object[]            otherPath = aTreePath.getPath();
  180.             int                 pathLength = path.length;
  181.  
  182.             if(otherPath.length >= pathLength && pathLength > 0) {
  183.                 for(int counter = 0; counter < pathLength; counter++)
  184.                     if(!path[counter].equals(otherPath[counter]))
  185.                         return false;
  186.                 return true;
  187.             }
  188.         }
  189.         return false;
  190.     }
  191.  
  192.     /**
  193.      * Returns a string that displays and identifies this
  194.      * object's properties.
  195.      *
  196.      * @return a String representation of this object
  197.      */
  198.     public String toString() {
  199.         StringBuffer tempSpot = new StringBuffer("[");
  200.  
  201.         for(int counter = 0; counter < path.length; counter++) {
  202.             if(counter > 0)
  203.                 tempSpot.append(", ");
  204.             tempSpot.append(path[counter]);
  205.         }
  206.         tempSpot.append("]");
  207.         return tempSpot.toString();
  208.     }
  209.  
  210.     // Serialization support.  
  211.     private void writeObject(ObjectOutputStream s) throws IOException {
  212.         s.defaultWriteObject();
  213.  
  214.         Vector      values = new Vector();
  215.         boolean     writePath = true;
  216.  
  217.         for(int counter = 0, maxCounter = path.length; counter < maxCounter;
  218.             counter++) {
  219.             if(!(path[counter] instanceof Serializable)) {
  220.                 writePath = false;
  221.                 break;
  222.             }
  223.         }
  224.         if(writePath) {
  225.             values.addElement("path");
  226.             values.addElement(path);
  227.         }
  228.         s.writeObject(values);
  229.     }
  230.  
  231.     private void readObject(ObjectInputStream s) 
  232.         throws IOException, ClassNotFoundException {
  233.         s.defaultReadObject();
  234.  
  235.         Vector          values = (Vector)s.readObject();
  236.         int             indexCounter = 0;
  237.         int             maxCounter = values.size();
  238.  
  239.         if(indexCounter < maxCounter && values.elementAt(indexCounter).
  240.            equals("path")) {
  241.             path = (Object[])values.elementAt(++indexCounter);
  242.             indexCounter++;
  243.         }
  244.         else
  245.             path = new Object[0];
  246.     }
  247. }
  248.