58 * getLastPathComponent}, and {@code getParentPath}. As {@code JTree}
59 * internally creates {@code TreePath}s at various points, it's
60 * generally not useful to subclass {@code TreePath} and use with
61 * {@code JTree}.
62 * <p>
63 * While {@code TreePath} is serializable, a {@code
64 * NotSerializableException} is thrown if any elements of the path are
65 * not serializable.
66 * <p>
67 * For further information and examples of using tree paths,
68 * see <a
69 href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
70 * in <em>The Java Tutorial.</em>
71 * <p>
72 * <strong>Warning:</strong>
73 * Serialized objects of this class will not be compatible with
74 * future Swing releases. The current serialization support is
75 * appropriate for short term storage or RMI between applications running
76 * the same version of Swing. As of 1.4, support for long term storage
77 * of all JavaBeans™
78 * has been added to the <code>java.beans</code> package.
79 * Please see {@link java.beans.XMLEncoder}.
80 *
81 * @author Scott Violet
82 * @author Philip Milne
83 */
84 @SuppressWarnings("serial") // Same-version serialization only
85 public class TreePath extends Object implements Serializable {
86 /** Path representing the parent, null if lastPathComponent represents
87 * the root. */
88 private TreePath parentPath;
89 /** Last path component. */
90 private Object lastPathComponent;
91
92 /**
93 * Creates a {@code TreePath} from an array. The array uniquely
94 * identifies the path to a node.
95 *
96 * @param path an array of objects representing the path to a node
97 * @throws IllegalArgumentException if {@code path} is {@code null},
98 * empty, or contains a {@code null} value
262 return false;
263 }
264 oTreePath = oTreePath.getParentPath();
265 }
266 return true;
267 }
268 return false;
269 }
270
271 /**
272 * Returns the hash code of this {@code TreePath}. The hash code of a
273 * {@code TreePath} is the hash code of the last element in the path.
274 *
275 * @return the hashCode for the object
276 */
277 public int hashCode() {
278 return getLastPathComponent().hashCode();
279 }
280
281 /**
282 * Returns true if <code>aTreePath</code> is a
283 * descendant of this
284 * {@code TreePath}. A {@code TreePath} {@code P1} is a descendant of a
285 * {@code TreePath} {@code P2}
286 * if {@code P1} contains all of the elements that make up
287 * {@code P2's} path.
288 * For example, if this object has the path {@code [a, b]},
289 * and <code>aTreePath</code> has the path {@code [a, b, c]},
290 * then <code>aTreePath</code> is a descendant of this object.
291 * However, if <code>aTreePath</code> has the path {@code [a]},
292 * then it is not a descendant of this object. By this definition
293 * a {@code TreePath} is always considered a descendant of itself.
294 * That is, <code>aTreePath.isDescendant(aTreePath)</code> returns
295 * {@code true}.
296 *
297 * @param aTreePath the {@code TreePath} to check
298 * @return true if <code>aTreePath</code> is a descendant of this path
299 */
300 public boolean isDescendant(TreePath aTreePath) {
301 if(aTreePath == this)
302 return true;
303
304 if(aTreePath != null) {
305 int pathLength = getPathCount();
306 int oPathLength = aTreePath.getPathCount();
307
308 if(oPathLength < pathLength)
309 // Can't be a descendant, has fewer components in the path.
310 return false;
311 while(oPathLength-- > pathLength)
312 aTreePath = aTreePath.getParentPath();
313 return equals(aTreePath);
314 }
315 return false;
316 }
317
318 /**
319 * Returns a new path containing all the elements of this path
320 * plus <code>child</code>. <code>child</code> is the last element
321 * of the newly created {@code TreePath}.
322 *
323 * @param child the path element to add
324 * @throws NullPointerException if {@code child} is {@code null}
325 * @return a new path containing all the elements of this path
326 * plus {@code child}
327 */
328 public TreePath pathByAddingChild(Object child) {
329 if(child == null)
330 throw new NullPointerException("Null child not allowed");
331
332 return new TreePath(this, child);
333 }
334
335 /**
336 * Returns the {@code TreePath} of the parent. A return value of
337 * {@code null} indicates this is the root node.
338 *
339 * @return the parent path
340 */
|
58 * getLastPathComponent}, and {@code getParentPath}. As {@code JTree}
59 * internally creates {@code TreePath}s at various points, it's
60 * generally not useful to subclass {@code TreePath} and use with
61 * {@code JTree}.
62 * <p>
63 * While {@code TreePath} is serializable, a {@code
64 * NotSerializableException} is thrown if any elements of the path are
65 * not serializable.
66 * <p>
67 * For further information and examples of using tree paths,
68 * see <a
69 href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
70 * in <em>The Java Tutorial.</em>
71 * <p>
72 * <strong>Warning:</strong>
73 * Serialized objects of this class will not be compatible with
74 * future Swing releases. The current serialization support is
75 * appropriate for short term storage or RMI between applications running
76 * the same version of Swing. As of 1.4, support for long term storage
77 * of all JavaBeans™
78 * has been added to the {@code java.beans} package.
79 * Please see {@link java.beans.XMLEncoder}.
80 *
81 * @author Scott Violet
82 * @author Philip Milne
83 */
84 @SuppressWarnings("serial") // Same-version serialization only
85 public class TreePath extends Object implements Serializable {
86 /** Path representing the parent, null if lastPathComponent represents
87 * the root. */
88 private TreePath parentPath;
89 /** Last path component. */
90 private Object lastPathComponent;
91
92 /**
93 * Creates a {@code TreePath} from an array. The array uniquely
94 * identifies the path to a node.
95 *
96 * @param path an array of objects representing the path to a node
97 * @throws IllegalArgumentException if {@code path} is {@code null},
98 * empty, or contains a {@code null} value
262 return false;
263 }
264 oTreePath = oTreePath.getParentPath();
265 }
266 return true;
267 }
268 return false;
269 }
270
271 /**
272 * Returns the hash code of this {@code TreePath}. The hash code of a
273 * {@code TreePath} is the hash code of the last element in the path.
274 *
275 * @return the hashCode for the object
276 */
277 public int hashCode() {
278 return getLastPathComponent().hashCode();
279 }
280
281 /**
282 * Returns true if {@code aTreePath} is a
283 * descendant of this
284 * {@code TreePath}. A {@code TreePath P1} is a descendant of a
285 * {@code TreePath P2}
286 * if {@code P1} contains all of the elements that make up
287 * {@code P2's} path.
288 * For example, if this object has the path {@code [a, b]},
289 * and {@code aTreePath} has the path {@code [a, b, c]},
290 * then {@code aTreePath} is a descendant of this object.
291 * However, if {@code aTreePath} has the path {@code [a]},
292 * then it is not a descendant of this object. By this definition
293 * a {@code TreePath} is always considered a descendant of itself.
294 * That is, {@code aTreePath.isDescendant(aTreePath)} returns
295 * {@code true}.
296 *
297 * @param aTreePath the {@code TreePath} to check
298 * @return true if {@code aTreePath} is a descendant of this path
299 */
300 public boolean isDescendant(TreePath aTreePath) {
301 if(aTreePath == this)
302 return true;
303
304 if(aTreePath != null) {
305 int pathLength = getPathCount();
306 int oPathLength = aTreePath.getPathCount();
307
308 if(oPathLength < pathLength)
309 // Can't be a descendant, has fewer components in the path.
310 return false;
311 while(oPathLength-- > pathLength)
312 aTreePath = aTreePath.getParentPath();
313 return equals(aTreePath);
314 }
315 return false;
316 }
317
318 /**
319 * Returns a new path containing all the elements of this path
320 * plus {@code child}. {@code child} is the last element
321 * of the newly created {@code TreePath}.
322 *
323 * @param child the path element to add
324 * @throws NullPointerException if {@code child} is {@code null}
325 * @return a new path containing all the elements of this path
326 * plus {@code child}
327 */
328 public TreePath pathByAddingChild(Object child) {
329 if(child == null)
330 throw new NullPointerException("Null child not allowed");
331
332 return new TreePath(this, child);
333 }
334
335 /**
336 * Returns the {@code TreePath} of the parent. A return value of
337 * {@code null} indicates this is the root node.
338 *
339 * @return the parent path
340 */
|