Source for javax.swing.tree.TreePath

   1: /* TreePath.java --
   2:    Copyright (C) 2002, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.tree;
  40: 
  41: import java.io.IOException;
  42: import java.io.ObjectInputStream;
  43: import java.io.ObjectOutputStream;
  44: import java.io.Serializable;
  45: import java.util.Arrays;
  46: 
  47: /**
  48:  * TreePath
  49:  * @author Andrew Selkirk
  50:  */
  51: public class TreePath implements Serializable
  52: {
  53:   static final long serialVersionUID = 4380036194768077479L;
  54: 
  55:   /**
  56:    * path
  57:    */
  58:   private Object[] path = null;
  59: 
  60: 
  61:   /**
  62:    * Constructor TreePath
  63:    * @param path TODO
  64:    */
  65:   public TreePath(Object[] path)
  66:   {
  67:     // Create Path
  68:     this.path = new Object[path.length];
  69:     System.arraycopy(path, 0, this.path, 0, path.length);
  70:   }
  71: 
  72:   /**
  73:    * Constructor TreePath
  74:    * @param element TODO
  75:    */
  76:   public TreePath(Object element)
  77:   {
  78:     // Create Path
  79:     path = new Object[1];
  80:     path[0] = element;
  81:   }
  82: 
  83:   /**
  84:    * Constructor TreePath
  85:    * @param path TODO
  86:    * @param element TODO
  87:    */
  88:   protected TreePath(TreePath path, Object element)
  89:   {
  90:     // Variables
  91:     Object[]    treepath;
  92: 
  93:     // Get Tree Path
  94:     treepath = path.getPath();
  95: 
  96:     // Create Tree Path
  97:     this.path = new Object[treepath.length + 1];
  98:     System.arraycopy(treepath, 0, this.path, 0, treepath.length);
  99:     this.path[treepath.length] = element;
 100:   }
 101: 
 102:   /**
 103:    * Constructor TreePath
 104:    * @param path TODO
 105:    * @param length TODO
 106:    */
 107:   protected TreePath(Object[] path, int length)
 108:   {
 109:     // Create Path
 110:     this.path = new Object[length];
 111:     System.arraycopy(path, 0, this.path, 0, length);
 112:   }
 113: 
 114:   /**
 115:    * Constructor TreePath
 116:    */
 117:   protected TreePath()
 118:   {
 119:     path = new Object[0];
 120:   }
 121: 
 122: 
 123:   /**
 124:    * hashCode
 125:    * @returns int
 126:    */
 127:   public int hashCode()
 128:   {
 129:     return getLastPathComponent().hashCode();
 130:   }
 131: 
 132:   /**
 133:    * equals
 134:    * @param object TODO
 135:    * @returns boolean
 136:    */
 137:   public boolean equals(Object object)
 138:   {
 139:     // Variables
 140:     Object[]    treepath;
 141:     int            index;
 142: 
 143:     // Check for TreePath
 144:     if (object instanceof TreePath)
 145:       {
 146:         // Get Path Elements
 147:         treepath = ((TreePath) object).getPath();
 148: 
 149:         // Check length
 150:         if (treepath.length != path.length)
 151:           return false;
 152: 
 153:         // Check Elements
 154:         for (index = 0; index < path.length; index++)
 155:           {
 156:             if (treepath[index] != path[index])
 157:               return false;
 158:           }
 159: 
 160:         // Tree Path's are equals
 161:         return true;
 162:       }
 163: 
 164:     // Unequal
 165:     return false;
 166:   }
 167: 
 168:   /**
 169:    * toString
 170:    * @returns String
 171:    */
 172:   public String toString()
 173:   {
 174:     if (path.length == 1)
 175:       return String.valueOf(path[0]);
 176:     else
 177:       return Arrays.asList(path).toString();
 178:   }
 179: 
 180:   /**
 181:    * writeObject
 182:    * @param value0 TODO
 183:    * @exception IOException TODO
 184:    */
 185:   private void writeObject(ObjectOutputStream value0)
 186:     throws IOException
 187:   {
 188:     // TODO
 189:   }
 190: 
 191:   /**
 192:    * readObject
 193:    * @param value0 TODO
 194:    * @exception IOException TODO
 195:    * @exception ClassNotFoundException TODO
 196:    */
 197:   private void readObject(ObjectInputStream value0)
 198:     throws IOException, ClassNotFoundException
 199:   {
 200:     // TODO
 201:   }
 202: 
 203:   /**
 204:    * getPath
 205:    * @returns Object[]
 206:    */
 207:   public Object[] getPath()
 208:   {
 209:     return path;
 210:   }
 211: 
 212:   /**
 213:    * getLastPathComponent
 214:    * @returns Object
 215:    */
 216:   public Object getLastPathComponent()
 217:   {
 218:     return path[path.length - 1];
 219:   }
 220: 
 221:   /**
 222:    * getPathCount
 223:    * @returns int
 224:    */
 225:   public int getPathCount()
 226:   {
 227:     return path.length;
 228:   }
 229: 
 230:   /**
 231:    * getPathComponent
 232:    * @param position TODO
 233:    * @returns Object
 234:    */
 235:   public Object getPathComponent(int position)
 236:   {
 237:     return path[position];
 238:   }
 239: 
 240:   /**
 241:    * isDescendant
 242:    * @param path TODO
 243:    * @returns boolean
 244:    */
 245:   public boolean isDescendant(TreePath path)
 246:   {
 247: 
 248:     // Variables
 249:     Object[]    treepath;
 250:     int            index;
 251:     int            index2;
 252: 
 253:     // Get Descendant path
 254:     treepath = path.getPath();
 255: 
 256:     // Locate Start Index
 257:     index = 0;
 258:     index2 = 0;
 259:     while (treepath[index] != this.path[index2])
 260:       index++;
 261: 
 262:     // Verify Paths
 263:     while (treepath[index] == this.path[index2])
 264:       {
 265:         index++;
 266:         index2++;
 267:       }
 268: 
 269:     // Check for descendant
 270:     if (index2 != this.path.length)
 271:       return false;
 272: 
 273:     // Is Descendant
 274:     return true;
 275: 
 276:   }
 277: 
 278:   /**
 279:    * pathByAddingChild
 280:    * @param element TODO
 281:    * @returns TreePath
 282:    */
 283:   public TreePath pathByAddingChild(Object element)
 284:   {
 285:     return new TreePath(this, element);
 286:   }
 287: 
 288:   /**
 289:    * getParentPath
 290:    * @returns TreePath
 291:    */
 292:   public TreePath getParentPath()
 293:   {
 294:     // If this path has only one element, then we return null. That
 295:     // is what the JDK does.
 296:     if (path.length <= 1)
 297:       return null;
 298: 
 299:     return new TreePath(this.getPath(), path.length - 1);
 300:   }
 301: }