001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.event;
027
028 import java.util.EventObject;
029 import javax.swing.tree.TreePath;
030
031 /**
032 * An event that characterizes a change in the current
033 * selection. The change is based on any number of paths.
034 * TreeSelectionListeners will generally query the source of
035 * the event for the new selected status of each potentially
036 * changed row.
037 * <p>
038 * <strong>Warning:</strong>
039 * Serialized objects of this class will not be compatible with
040 * future Swing releases. The current serialization support is
041 * appropriate for short term storage or RMI between applications running
042 * the same version of Swing. As of 1.4, support for long term storage
043 * of all JavaBeans<sup><font size="-2">TM</font></sup>
044 * has been added to the <code>java.beans</code> package.
045 * Please see {@link java.beans.XMLEncoder}.
046 *
047 * @see TreeSelectionListener
048 * @see javax.swing.tree.TreeSelectionModel
049 *
050 * @version 1.35 05/05/07
051 * @author Scott Violet
052 */
053 public class TreeSelectionEvent extends EventObject {
054 /** Paths this event represents. */
055 protected TreePath[] paths;
056 /** For each path identifies if that path is in fact new. */
057 protected boolean[] areNew;
058 /** leadSelectionPath before the paths changed, may be null. */
059 protected TreePath oldLeadSelectionPath;
060 /** leadSelectionPath after the paths changed, may be null. */
061 protected TreePath newLeadSelectionPath;
062
063 /**
064 * Represents a change in the selection of a TreeSelectionModel.
065 * paths identifies the paths that have been either added or
066 * removed from the selection.
067 *
068 * @param source source of event
069 * @param paths the paths that have changed in the selection
070 */
071 public TreeSelectionEvent(Object source, TreePath[] paths,
072 boolean[] areNew, TreePath oldLeadSelectionPath,
073 TreePath newLeadSelectionPath) {
074 super (source);
075 this .paths = paths;
076 this .areNew = areNew;
077 this .oldLeadSelectionPath = oldLeadSelectionPath;
078 this .newLeadSelectionPath = newLeadSelectionPath;
079 }
080
081 /**
082 * Represents a change in the selection of a TreeSelectionModel.
083 * path identifies the path that have been either added or
084 * removed from the selection.
085 *
086 * @param source source of event
087 * @param path the path that has changed in the selection
088 * @param isNew whether or not the path is new to the selection, false
089 * means path was removed from the selection.
090 */
091 public TreeSelectionEvent(Object source, TreePath path,
092 boolean isNew, TreePath oldLeadSelectionPath,
093 TreePath newLeadSelectionPath) {
094 super (source);
095 paths = new TreePath[1];
096 paths[0] = path;
097 areNew = new boolean[1];
098 areNew[0] = isNew;
099 this .oldLeadSelectionPath = oldLeadSelectionPath;
100 this .newLeadSelectionPath = newLeadSelectionPath;
101 }
102
103 /**
104 * Returns the paths that have been added or removed from the
105 * selection.
106 */
107 public TreePath[] getPaths() {
108 int numPaths;
109 TreePath[] retPaths;
110
111 numPaths = paths.length;
112 retPaths = new TreePath[numPaths];
113 System.arraycopy(paths, 0, retPaths, 0, numPaths);
114 return retPaths;
115 }
116
117 /**
118 * Returns the first path element.
119 */
120 public TreePath getPath() {
121 return paths[0];
122 }
123
124 /**
125 * Returns whether the path identified by {@code getPath} was
126 * added to the selection. A return value of {@code true}
127 * indicates the path identified by {@code getPath} was added to
128 * the selection. A return value of {@code false} indicates {@code
129 * getPath} was selected, but is no longer selected.
130 *
131 * @return {@code true} if {@code getPath} was added to the selection,
132 * {@code false} otherwise
133 */
134 public boolean isAddedPath() {
135 return areNew[0];
136 }
137
138 /**
139 * Returns whether the specified path was added to the selection.
140 * A return value of {@code true} indicates the path identified by
141 * {@code path} was added to the selection. A return value of
142 * {@code false} indicates {@code path} is no longer selected. This method
143 * is only valid for the paths returned from {@code getPaths()}; invoking
144 * with a path not included in {@code getPaths()} throws an
145 * {@code IllegalArgumentException}.
146 *
147 * @param path the path to test
148 * @return {@code true} if {@code path} was added to the selection,
149 * {@code false} otherwise
150 * @throws IllegalArgumentException if {@code path} is not contained
151 * in {@code getPaths}
152 * @see #getPaths
153 */
154 public boolean isAddedPath(TreePath path) {
155 for (int counter = paths.length - 1; counter >= 0; counter--)
156 if (paths[counter].equals(path))
157 return areNew[counter];
158 throw new IllegalArgumentException(
159 "path is not a path identified by the TreeSelectionEvent");
160 }
161
162 /**
163 * Returns whether the path at {@code getPaths()[index]} was added
164 * to the selection. A return value of {@code true} indicates the
165 * path was added to the selection. A return value of {@code false}
166 * indicates the path is no longer selected.
167 *
168 * @param index the index of the path to test
169 * @return {@code true} if the path was added to the selection,
170 * {@code false} otherwise
171 * @throws IllegalArgumentException if index is outside the range of
172 * {@code getPaths}
173 * @see #getPaths
174 *
175 * @since 1.3
176 */
177 public boolean isAddedPath(int index) {
178 if (paths == null || index < 0 || index >= paths.length) {
179 throw new IllegalArgumentException(
180 "index is beyond range of added paths identified by TreeSelectionEvent");
181 }
182 return areNew[index];
183 }
184
185 /**
186 * Returns the path that was previously the lead path.
187 */
188 public TreePath getOldLeadSelectionPath() {
189 return oldLeadSelectionPath;
190 }
191
192 /**
193 * Returns the current lead path.
194 */
195 public TreePath getNewLeadSelectionPath() {
196 return newLeadSelectionPath;
197 }
198
199 /**
200 * Returns a copy of the receiver, but with the source being newSource.
201 */
202 public Object cloneWithSource(Object newSource) {
203 // Fix for IE bug - crashing
204 return new TreeSelectionEvent(newSource, paths, areNew,
205 oldLeadSelectionPath, newLeadSelectionPath);
206 }
207 }
|