1 7 8 package javax.swing.tree; 9 10 import java.io.*; 11 import java.util.Vector ; 12 13 39 public class TreePath extends Object implements Serializable { 40 42 private TreePath parentPath; 43 44 transient private Object lastPathComponent; 45 46 58 public TreePath(Object [] path) { 59 if(path == null || path.length == 0) 60 throw new IllegalArgumentException ("path in TreePath must be non null and not empty."); 61 lastPathComponent = path[path.length - 1]; 62 if(path.length > 1) 63 parentPath = new TreePath (path, path.length - 1); 64 } 65 66 73 public TreePath(Object singlePath) { 74 if(singlePath == null) 75 throw new IllegalArgumentException ("path in TreePath must be non null."); 76 lastPathComponent = singlePath; 77 parentPath = null; 78 } 79 80 84 protected TreePath(TreePath parent, Object lastElement) { 85 if(lastElement == null) 86 throw new IllegalArgumentException ("path in TreePath must be non null."); 87 parentPath = parent; 88 lastPathComponent = lastElement; 89 } 90 91 95 protected TreePath(Object [] path, int length) { 96 lastPathComponent = path[length - 1]; 97 if(length > 1) 98 parentPath = new TreePath (path, length - 1); 99 } 100 101 110 protected TreePath() { 111 } 112 113 120 public Object [] getPath() { 121 int i = getPathCount(); 122 Object [] result = new Object [i--]; 123 124 for(TreePath path = this; path != null; path = path.parentPath) { 125 result[i--] = path.lastPathComponent; 126 } 127 return result; 128 } 129 130 137 public Object getLastPathComponent() { 138 return lastPathComponent; 139 } 140 141 146 public int getPathCount() { 147 int result = 0; 148 for(TreePath path = this; path != null; path = path.parentPath) { 149 result++; 150 } 151 return result; 152 } 153 154 164 public Object getPathComponent(int element) { 165 int pathLength = getPathCount(); 166 167 if(element < 0 || element >= pathLength) 168 throw new IllegalArgumentException ("Index " + element + " is out of the specified range"); 169 170 TreePath path = this; 171 172 for(int i = pathLength-1; i != element; i--) { 173 path = path.parentPath; 174 } 175 return path.lastPathComponent; 176 } 177 178 186 public boolean equals(Object o) { 187 if(o == this) 188 return true; 189 if(o instanceof TreePath ) { 190 TreePath oTreePath = (TreePath )o; 191 192 if(getPathCount() != oTreePath.getPathCount()) 193 return false; 194 for(TreePath path = this; path != null; path = path.parentPath) { 195 if (!(path.lastPathComponent.equals 196 (oTreePath.lastPathComponent))) { 197 return false; 198 } 199 oTreePath = oTreePath.parentPath; 200 } 201 return true; 202 } 203 return false; 204 } 205 206 212 public int hashCode() { 213 return lastPathComponent.hashCode(); 214 } 215 216 230 public boolean isDescendant(TreePath aTreePath) { 231 if(aTreePath == this) 232 return true; 233 234 if(aTreePath != null) { 235 int pathLength = getPathCount(); 236 int oPathLength = aTreePath.getPathCount(); 237 238 if(oPathLength < pathLength) 239 return false; 241 while(oPathLength-- > pathLength) 242 aTreePath = aTreePath.getParentPath(); 243 return equals(aTreePath); 244 } 245 return false; 246 } 247 248 255 public TreePath pathByAddingChild(Object child) { 256 if(child == null) 257 throw new NullPointerException ("Null child not allowed"); 258 259 return new TreePath (this, child); 260 } 261 262 266 public TreePath getParentPath() { 267 return parentPath; 268 } 269 270 276 public String toString() { 277 StringBuffer tempSpot = new StringBuffer ("["); 278 279 for(int counter = 0, maxCounter = getPathCount();counter < maxCounter; 280 counter++) { 281 if(counter > 0) 282 tempSpot.append(", "); 283 tempSpot.append(getPathComponent(counter)); 284 } 285 tempSpot.append("]"); 286 return tempSpot.toString(); 287 } 288 289 private void writeObject(ObjectOutputStream s) throws IOException { 291 s.defaultWriteObject(); 292 293 Vector values = new Vector (); 294 boolean writePath = true; 295 296 if(lastPathComponent != null && 297 (lastPathComponent instanceof Serializable)) { 298 values.addElement("lastPathComponent"); 299 values.addElement(lastPathComponent); 300 } 301 s.writeObject(values); 302 } 303 304 private void readObject(ObjectInputStream s) 305 throws IOException, ClassNotFoundException { 306 s.defaultReadObject(); 307 308 Vector values = (Vector )s.readObject(); 309 int indexCounter = 0; 310 int maxCounter = values.size(); 311 312 if(indexCounter < maxCounter && values.elementAt(indexCounter). 313 equals("lastPathComponent")) { 314 lastPathComponent = values.elementAt(++indexCounter); 315 indexCounter++; 316 } 317 } 318 } 319 | Popular Tags |