jdk/src/share/classes/javax/swing/event/TreeSelectionEvent.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
child 25154 a4252397d312
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
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.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.tree.TreePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An event that characterizes a change in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * selection.  The change is based on any number of paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * TreeSelectionListeners will generally query the source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the event for the new selected status of each potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * changed row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * 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: 5506
diff changeset
    43
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see TreeSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see javax.swing.tree.TreeSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    52
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class TreeSelectionEvent extends EventObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** Paths this event represents. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected TreePath[]     paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** For each path identifies if that path is in fact new. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected boolean[]       areNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** leadSelectionPath before the paths changed, may be null. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected TreePath        oldLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** leadSelectionPath after the paths changed, may be null. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected TreePath        newLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      * Represents a change in the selection of a TreeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      * paths identifies the paths that have been either added or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      * removed from the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      * @param source source of event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      * @param paths the paths that have changed in the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public TreeSelectionEvent(Object source, TreePath[] paths,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                              boolean[] areNew, TreePath oldLeadSelectionPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                              TreePath newLeadSelectionPath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.paths = paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.areNew = areNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.oldLeadSelectionPath = oldLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.newLeadSelectionPath = newLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      * Represents a change in the selection of a TreeSelectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      * path identifies the path that have been either added or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      * removed from the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      * @param source source of event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * @param path the path that has changed in the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      * @param isNew whether or not the path is new to the selection, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      * means path was removed from the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public TreeSelectionEvent(Object source, TreePath path, boolean isNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                              TreePath oldLeadSelectionPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                              TreePath newLeadSelectionPath)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        paths = new TreePath[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        paths[0] = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        areNew = new boolean[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        areNew[0] = isNew;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.oldLeadSelectionPath = oldLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.newLeadSelectionPath = newLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      * Returns the paths that have been added or removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      * selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public TreePath[] getPaths()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int                  numPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        TreePath[]          retPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        numPaths = paths.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        retPaths = new TreePath[numPaths];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.arraycopy(paths, 0, retPaths, 0, numPaths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return retPaths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      * Returns the first path element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public TreePath getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return paths[0];
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
     * Returns whether the path identified by {@code getPath} was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * added to the selection.  A return value of {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * indicates the path identified by {@code getPath} was added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * the selection. A return value of {@code false} indicates {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * getPath} was selected, but is no longer selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return {@code true} if {@code getPath} was added to the selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public boolean isAddedPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return areNew[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns whether the specified path was added to the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * A return value of {@code true} indicates the path identified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * {@code path} was added to the selection. A return value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@code false} indicates {@code path} is no longer selected. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * is only valid for the paths returned from {@code getPaths()}; invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * with a path not included in {@code getPaths()} throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@code IllegalArgumentException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param path the path to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return {@code true} if {@code path} was added to the selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws IllegalArgumentException if {@code path} is not contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         in {@code getPaths}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see #getPaths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean isAddedPath(TreePath path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for(int counter = paths.length - 1; counter >= 0; counter--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if(paths[counter].equals(path))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return areNew[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        throw new IllegalArgumentException("path is not a path identified by the TreeSelectionEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns whether the path at {@code getPaths()[index]} was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * to the selection.  A return value of {@code true} indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * path was added to the selection. A return value of {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * indicates the path is no longer selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param index the index of the path to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return {@code true} if the path was added to the selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws IllegalArgumentException if index is outside the range of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         {@code getPaths}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @see #getPaths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public boolean isAddedPath(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (paths == null || index < 0 || index >= paths.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new IllegalArgumentException("index is beyond range of added paths identified by TreeSelectionEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return areNew[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Returns the path that was previously the lead path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public TreePath getOldLeadSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return oldLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the current lead path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public TreePath getNewLeadSelectionPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return newLeadSelectionPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns a copy of the receiver, but with the source being newSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public Object cloneWithSource(Object newSource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      // Fix for IE bug - crashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
      return new TreeSelectionEvent(newSource, paths,areNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                    oldLeadSelectionPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                    newLeadSelectionPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}