TreePath.java 8.71 KB
Newer Older
Tom Tromey committed
1
/* TreePath.java --
2
   Copyright (C) 2002, 2005, 2006,  Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.tree;

import java.io.Serializable;
import java.util.Arrays;

/**
 * A <code>TreePath</code> represents a sequence of tree elements that form
 * a path starting from the root of a tree.  A tree element can be represented
 * by any {@link Object}.
 * 
 * @author Andrew Selkirk
 */
public class TreePath implements Serializable
{
  static final long serialVersionUID = 4380036194768077479L;

  /**
56 57
   * The actual patch. The {@link DefaultTreeSelectionModel#clone()}
   * assumes that the TreePath is immutable, so it is marked final here.
Tom Tromey committed
58
   */
59 60 61 62 63 64
  private final Object[] path;
  
  /**
   * The parent path (to be reused).
   */
  private transient TreePath parentPath;
Tom Tromey committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161


  /**
   * Creates a path from the list of objects representing tree elements.  The
   * incoming array is copied so that subsequent changes do not affect this
   * tree path.
   * 
   * @param path  the elements in the path (<code>null</code> not permitted).
   * 
   * @throws IllegalArgumentException if <code>path</code> is <code>null</code>.
   */
  public TreePath(Object[] path)
  {
    if (path == null)
      throw new IllegalArgumentException("Null 'path' not permitted.");
    this.path = new Object[path.length];
    System.arraycopy(path, 0, this.path, 0, path.length);
  }

  /**
   * Creates a new path from a single element.
   * 
   * @param element the element (<code>null</code> not permitted).
   * 
   * @throws IllegalArgumentException if <code>element</code> is 
   *         <code>null</code>.
   */
  public TreePath(Object element)
  {
    path = new Object[1];
    path[0] = element;
  }

  /**
   * Creates a new tree path by adding the specified <code>element</code> to
   * the <code>path</code>.
   * 
   * @param path  a tree path.
   * @param element  a path element.
   */
  protected TreePath(TreePath path, Object element)
  {
    if (element == null)
      throw new NullPointerException("Null 'element' argument.");
    Object[] treepath = path.getPath();

    // Create Tree Path
    this.path = new Object[treepath.length + 1];
    System.arraycopy(treepath, 0, this.path, 0, treepath.length);
    this.path[treepath.length] = element;
  }

  /**
   * Creates a new tree path using the first <code>length</code> elements
   * from the given array.
   * 
   * @param path  the path elements.
   * @param length  the path length.
   */
  protected TreePath(Object[] path, int length)
  {
    // Create Path
    this.path = new Object[length];
    System.arraycopy(path, 0, this.path, 0, length);
  }

  /**
   * Default constructor.
   */
  protected TreePath()
  {
    path = new Object[0];
  }


  /**
   * Returns a hashcode for the path.
   * 
   * @return A hashcode.
   */
  public int hashCode()
  {
    return getLastPathComponent().hashCode();
  }

  /**
   * Tests this path for equality with an arbitrary object.  An object is 
   * considered equal to this path if and only if:
   * <ul>
   * <li>the object is not <code>null</code>;</li>
   * <li>the object is an instanceof {@link TreePath};</li>
   * <li>the object contains the same elements in the same order as this
   * {@link TreePath};</li>
   * </ul>
   * 
   * @param object  the object (<code>null</code> permitted).
   * 
162 163
   * @return <code>true</code> if <code>obj</code> is equal to this tree path,
   *         and <code>false</code> otherwise.
Tom Tromey committed
164 165 166 167 168 169 170 171 172 173 174 175 176
   */
  public boolean equals(Object object)
  {
    Object[] treepath;
    int index;

    if (object instanceof TreePath)
      {
        treepath = ((TreePath) object).getPath();
        if (treepath.length != path.length)
          return false;
        for (index = 0; index < path.length; index++)
          {
177
            if (!path[index].equals(treepath[index]))
Tom Tromey committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
              return false;
          }

        // Tree Path's are equals
        return true;
      }

    // Unequal
    return false;
  }

  /**
   * Returns a string representation of this path.
   * 
   * @return A string representation of this path.
   */
  public String toString()
  {
    if (path.length == 1)
      return String.valueOf(path[0]);
    else
      return Arrays.asList(path).toString();
  }

  /**
   * Returns an array containing the path elements.
   * 
205
   * @return An array containing the path elements.
Tom Tromey committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
   */
  public Object[] getPath()
  {
    return (Object[]) path.clone();
  }

  /**
   * Returns the last object in the path.
   * 
   * @return The last object in the path.
   */
  public Object getLastPathComponent()
  {
    return path[path.length - 1];
  }

  /**
   * Returns the number of elements in the path.
   * 
225
   * @return The number of elements in the path.
Tom Tromey committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
   */
  public int getPathCount()
  {
    return path.length;
  }

  /**
   * Returns the element at the specified position in the path.
   * 
   * @param position the element position (<code>0 &lt N - 1</code>, where 
   *                 <code>N</code> is the number of elements in the path).
   * 
   * @return The element at the specified position.
   * 
   * @throws IllegalArgumentException if <code>position</code> is outside the
   *         valid range.
   */
  public Object getPathComponent(int position)
  {
    if (position < 0 || position >= getPathCount()) 
      throw new IllegalArgumentException("Invalid position: " + position);
    return path[position];
  }

  /**
   * Returns <code>true</code> if <code>path</code> is a descendant of this
   * path, and <code>false</code> otherwise.  If <code>path</code> is 
   * <code>null</code>, this method returns <code>false</code>.
   * 
   * @param path  the path to check (<code>null</code> permitted).
   * 
257 258
   * @return <code>true</code> if <code>path</code> is a descendant of this
   *         path, and <code>false</code> otherwise
Tom Tromey committed
259 260 261 262 263 264
   */
  public boolean isDescendant(TreePath path)
  {
    if (path == null)
      return false;
    int count = getPathCount();
265 266
    int otherPathLength = path.getPathCount();
    if (otherPathLength < count)
Tom Tromey committed
267
      return false;
268 269 270 271 272 273 274
    while (otherPathLength > count)
      {
        otherPathLength--;
        path = path.getParentPath();
      }
    
    return equals(path);
Tom Tromey committed
275 276 277 278 279 280 281 282
  }

  /**
   * Creates a new path that is equivalent to this path plus the specified
   * element.
   * 
   * @param element  the element.
   * 
283
   * @return A tree path.
Tom Tromey committed
284 285 286 287 288 289 290 291 292 293 294
   */
  public TreePath pathByAddingChild(Object element)
  {
    return new TreePath(this, element);
  }

  /**
   * Returns the parent path, which is a path containing all the same elements
   * as this path, except for the last one.  If this path contains only one
   * element, the method returns <code>null</code>.
   * 
295 296
   * @return The parent path, or <code>null</code> if this path has only one
   *         element.
Tom Tromey committed
297 298 299 300 301 302 303
   */
  public TreePath getParentPath()
  {
    // If this path has only one element, then we return null. That
    // is what the JDK does.
    if (path.length <= 1)
      return null;
304 305 306 307 308 309 310
    
    // Reuse the parent path, if possible. The parent path is requested
    // during the tree repainting, so reusing generates a lot less garbage.
    if (parentPath == null)
      parentPath = new TreePath(this.getPath(), path.length - 1);
    
    return parentPath;
Tom Tromey committed
311 312
  }
}