jdk/src/share/classes/javax/swing/tree/TreePath.java
author darcy
Thu, 03 Jul 2014 15:24:27 -0700
changeset 25568 b906a74c6882
parent 24969 afa6934dd8e8
permissions -rw-r--r--
8043550: Fix raw and unchecked lint warnings in javax.swing.* Reviewed-by: pchelko, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.tree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * {@code TreePath} represents an array of objects that uniquely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * identify the path to a node in a tree. The elements of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * are ordered with the root as the first element of the array. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * example, a file on the file system is uniquely identified based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the array of parent directories and the name of the file. The path
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@code /tmp/foo/bar} could be represented by a {@code TreePath} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@code new TreePath(new Object[] {"tmp", "foo", "bar"})}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@code TreePath} is used extensively by {@code JTree} and related classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * For example, {@code JTree} represents the selection as an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@code TreePath}s. When used with {@code JTree}, the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * path are the objects returned from the {@code TreeModel}. When {@code JTree}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is paired with {@code DefaultTreeModel}, the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * path are {@code TreeNode}s. The following example illustrates extracting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the user object from the selection of a {@code JTree}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   DefaultMutableTreeNode root = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   DefaultTreeModel model = new DefaultTreeModel(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   JTree tree = new JTree(model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   TreePath selectedPath = tree.getSelectionPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   DefaultMutableTreeNode selectedNode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *       ((DefaultMutableTreeNode)selectedPath.getLastPathComponent()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *       getUserObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Subclasses typically need override only {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * getLastPathComponent}, and {@code getParentPath}. As {@code JTree}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * internally creates {@code TreePath}s at various points, it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * generally not useful to subclass {@code TreePath} and use with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@code JTree}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * While {@code TreePath} is serializable, a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * NotSerializableException} is thrown if any elements of the path are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * not serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * For further information and examples of using tree paths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * see <a
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 5506
diff changeset
    69
 href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    77
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    84
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class TreePath extends Object implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Path representing the parent, null if lastPathComponent represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the root. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private TreePath           parentPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Last path component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Object lastPathComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Creates a {@code TreePath} from an array. The array uniquely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * identifies the path to a node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param path an array of objects representing the path to a node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @throws IllegalArgumentException if {@code path} is {@code null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *         empty, or contains a {@code null} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    @ConstructorProperties({"path"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public TreePath(Object[] path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if(path == null || path.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException("path in TreePath must be non null and not empty.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        lastPathComponent = path[path.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (lastPathComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "Last path component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if(path.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            parentPath = new TreePath(path, path.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Creates a {@code TreePath} containing a single element. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * used to construct a {@code TreePath} identifying the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param lastPathComponent the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see #TreePath(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws IllegalArgumentException if {@code lastPathComponent} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public TreePath(Object lastPathComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if(lastPathComponent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new IllegalArgumentException("path in TreePath must be non null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        this.lastPathComponent = lastPathComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        parentPath = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Creates a {@code TreePath} with the specified parent and element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param parent the path to the parent, or {@code null} to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *        the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param lastPathComponent the last path element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @throws IllegalArgumentException if {@code lastPathComponent} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected TreePath(TreePath parent, Object lastPathComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if(lastPathComponent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new IllegalArgumentException("path in TreePath must be non null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        parentPath = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.lastPathComponent = lastPathComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a {@code TreePath} from an array. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@code TreePath} represents the elements of the array from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@code 0} to {@code length - 1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * This constructor is used internally, and generally not useful outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * of subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param path the array to create the {@code TreePath} from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param length identifies the number of elements in {@code path} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *        create the {@code TreePath} from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws NullPointerException if {@code path} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws ArrayIndexOutOfBoundsException if {@code length - 1} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         outside the range of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws IllegalArgumentException if any of the elements from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *         {@code 0} to {@code length - 1} are {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected TreePath(Object[] path, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        lastPathComponent = path[length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (lastPathComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                "Path elements must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if(length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            parentPath = new TreePath(path, length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Creates an empty {@code TreePath}.  This is provided for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * subclasses that represent paths in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * manner. Subclasses that use this constructor must override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * {@code getLastPathComponent}, and {@code getParentPath}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected TreePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns an ordered array of the elements of this {@code TreePath}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * The first element is the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return an array of the elements in this {@code TreePath}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Object[] getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int            i = getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Object[]       result = new Object[i--];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for(TreePath path = this; path != null; path = path.getParentPath()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            result[i--] = path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns the last element of this path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the last element in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Object getLastPathComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return lastPathComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the number of elements in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the number of elements in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public int getPathCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int        result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        for(TreePath path = this; path != null; path = path.getParentPath()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            result++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the path element at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param index the index of the element requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return the element at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws IllegalArgumentException if the index is outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         range of this path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public Object getPathComponent(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int          pathLength = getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if(index < 0 || index >= pathLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException("Index " + index +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                           " is out of the specified range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        TreePath         path = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for(int i = pathLength-1; i != index; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            path = path.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return path.getLastPathComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Compares this {@code TreePath} to the specified object. This returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * {@code true} if {@code o} is a {@code TreePath} with the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * same elements (as determined by using {@code equals} on each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * element of the path).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param o the object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if(o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if(o instanceof TreePath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            TreePath            oTreePath = (TreePath)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if(getPathCount() != oTreePath.getPathCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for(TreePath path = this; path != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    path = path.getParentPath()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (!(path.getLastPathComponent().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                      (oTreePath.getLastPathComponent()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                oTreePath = oTreePath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns the hash code of this {@code TreePath}. The hash code of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * {@code TreePath} is the hash code of the last element in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the hashCode for the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return getLastPathComponent().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Returns true if <code>aTreePath</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * descendant of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * {@code TreePath}. A {@code TreePath} {@code P1} is a descendant of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * {@code TreePath} {@code P2}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * if {@code P1} contains all of the elements that make up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * {@code P2's} path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * For example, if this object has the path {@code [a, b]},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * and <code>aTreePath</code> has the path {@code [a, b, c]},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * then <code>aTreePath</code> is a descendant of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * However, if <code>aTreePath</code> has the path {@code [a]},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * then it is not a descendant of this object.  By this definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * a {@code TreePath} is always considered a descendant of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * That is, <code>aTreePath.isDescendant(aTreePath)</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param aTreePath the {@code TreePath} to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return true if <code>aTreePath</code> is a descendant of this path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public boolean isDescendant(TreePath aTreePath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if(aTreePath == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if(aTreePath != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            int                 pathLength = getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            int                 oPathLength = aTreePath.getPathCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if(oPathLength < pathLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // Can't be a descendant, has fewer components in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            while(oPathLength-- > pathLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                aTreePath = aTreePath.getParentPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return equals(aTreePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns a new path containing all the elements of this path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * plus <code>child</code>. <code>child</code> is the last element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * of the newly created {@code TreePath}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
24495
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   323
     * @param   child   the path element to add
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   324
     * @throws          NullPointerException if {@code child} is {@code null}
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   325
     * @return          a new path containing all the elements of this path
a5c854a00679 8042089: Fix doclint warnings from javax.swing.tree and javax.swing.undo packages
yan
parents: 22574
diff changeset
   326
     *                  plus {@code child}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public TreePath pathByAddingChild(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if(child == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new NullPointerException("Null child not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return new TreePath(this, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns the {@code TreePath} of the parent. A return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * {@code null} indicates this is the root node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return the parent path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public TreePath getParentPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return parentPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns a string that displays and identifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * object's properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return a String representation of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24495
diff changeset
   352
        StringBuilder tempSpot = new StringBuilder("[");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        for(int counter = 0, maxCounter = getPathCount();counter < maxCounter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if(counter > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                tempSpot.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            tempSpot.append(getPathComponent(counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        tempSpot.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return tempSpot.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}