2 Copyright (C) 2002, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax.swing.tree;
41 import java.io.Serializable;
42 import java.util.Arrays;
45 * A <code>TreePath</code> represents a sequence of tree elements that form
46 * a path starting from the root of a tree. A tree element can be represented
47 * by any {@link Object}.
49 * @author Andrew Selkirk
51 public class TreePath implements Serializable
53 static final long serialVersionUID = 4380036194768077479L;
56 * The actual patch. The {@link DefaultTreeSelectionModel#clone()}
57 * assumes that the TreePath is immutable, so it is marked final here.
59 private final Object[] path;
62 * The parent path (to be reused).
64 private transient TreePath parentPath;
68 * Creates a path from the list of objects representing tree elements. The
69 * incoming array is copied so that subsequent changes do not affect this
72 * @param path the elements in the path (<code>null</code> not permitted).
74 * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
76 public TreePath(Object[] path)
79 throw new IllegalArgumentException("Null 'path' not permitted.");
80 this.path = new Object[path.length];
81 System.arraycopy(path, 0, this.path, 0, path.length);
85 * Creates a new path from a single element.
87 * @param element the element (<code>null</code> not permitted).
89 * @throws IllegalArgumentException if <code>element</code> is
92 public TreePath(Object element)
99 * Creates a new tree path by adding the specified <code>element</code> to
100 * the <code>path</code>.
102 * @param path a tree path.
103 * @param element a path element.
105 protected TreePath(TreePath path, Object element)
108 throw new NullPointerException("Null 'element' argument.");
109 Object[] treepath = path.getPath();
112 this.path = new Object[treepath.length + 1];
113 System.arraycopy(treepath, 0, this.path, 0, treepath.length);
114 this.path[treepath.length] = element;
118 * Creates a new tree path using the first <code>length</code> elements
119 * from the given array.
121 * @param path the path elements.
122 * @param length the path length.
124 protected TreePath(Object[] path, int length)
127 this.path = new Object[length];
128 System.arraycopy(path, 0, this.path, 0, length);
132 * Default constructor.
136 path = new Object[0];
141 * Returns a hashcode for the path.
143 * @return A hashcode.
145 public int hashCode()
147 return getLastPathComponent().hashCode();
151 * Tests this path for equality with an arbitrary object. An object is
152 * considered equal to this path if and only if:
154 * <li>the object is not <code>null</code>;</li>
155 * <li>the object is an instanceof {@link TreePath};</li>
156 * <li>the object contains the same elements in the same order as this
157 * {@link TreePath};</li>
160 * @param object the object (<code>null</code> permitted).
162 * @return <code>true</code> if <code>obj</code> is equal to this tree path,
163 * and <code>false</code> otherwise.
165 public boolean equals(Object object)
170 if (object instanceof TreePath)
172 treepath = ((TreePath) object).getPath();
173 if (treepath.length != path.length)
175 for (index = 0; index < path.length; index++)
177 if (!treepath[index].equals(path[index]))
181 // Tree Path's are equals
190 * Returns a string representation of this path.
192 * @return A string representation of this path.
194 public String toString()
196 if (path.length == 1)
197 return String.valueOf(path[0]);
199 return Arrays.asList(path).toString();
203 * Returns an array containing the path elements.
205 * @return An array containing the path elements.
207 public Object[] getPath()
209 return (Object[]) path.clone();
213 * Returns the last object in the path.
215 * @return The last object in the path.
217 public Object getLastPathComponent()
219 return path[path.length - 1];
223 * Returns the number of elements in the path.
225 * @return The number of elements in the path.
227 public int getPathCount()
233 * Returns the element at the specified position in the path.
235 * @param position the element position (<code>0 < N - 1</code>, where
236 * <code>N</code> is the number of elements in the path).
238 * @return The element at the specified position.
240 * @throws IllegalArgumentException if <code>position</code> is outside the
243 public Object getPathComponent(int position)
245 if (position < 0 || position >= getPathCount())
246 throw new IllegalArgumentException("Invalid position: " + position);
247 return path[position];
251 * Returns <code>true</code> if <code>path</code> is a descendant of this
252 * path, and <code>false</code> otherwise. If <code>path</code> is
253 * <code>null</code>, this method returns <code>false</code>.
255 * @param path the path to check (<code>null</code> permitted).
257 * @return <code>true</code> if <code>path</code> is a descendant of this
258 * path, and <code>false</code> otherwise
260 public boolean isDescendant(TreePath path)
264 int count = getPathCount();
265 int otherPathLength = path.getPathCount();
266 if (otherPathLength < count)
268 while (otherPathLength > count)
271 path = path.getParentPath();
278 * Creates a new path that is equivalent to this path plus the specified
281 * @param element the element.
283 * @return A tree path.
285 public TreePath pathByAddingChild(Object element)
287 return new TreePath(this, element);
291 * Returns the parent path, which is a path containing all the same elements
292 * as this path, except for the last one. If this path contains only one
293 * element, the method returns <code>null</code>.
295 * @return The parent path, or <code>null</code> if this path has only one
298 public TreePath getParentPath()
300 // If this path has only one element, then we return null. That
301 // is what the JDK does.
302 if (path.length <= 1)
305 // Reuse the parent path, if possible. The parent path is requested
306 // during the tree repainting, so reusing generates a lot less garbage.
307 if (parentPath == null)
308 parentPath = new TreePath(this.getPath(), path.length - 1);