1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing.event; 27 28 import java.util.EventObject; 29 import javax.swing.tree.TreePath; 30 31 /** 32 * An event that characterizes a change in the current 33 * selection. The change is based on any number of paths. 34 * TreeSelectionListeners will generally query the source of 35 * the event for the new selected status of each potentially 36 * changed row. 37 * <p> 38 * <strong>Warning:</strong> 39 * Serialized objects of this class will not be compatible with 40 * future Swing releases. The current serialization support is 41 * appropriate for short term storage or RMI between applications running 42 * the same version of Swing. As of 1.4, support for long term storage 43 * of all JavaBeans™ 44 * has been added to the <code>java.beans</code> package. 45 * Please see {@link java.beans.XMLEncoder}. 46 * 47 * @see TreeSelectionListener 48 * @see javax.swing.tree.TreeSelectionModel 49 * 50 * @author Scott Violet 51 */ 52 @SuppressWarnings("serial") // Same-version serialization only 53 public class TreeSelectionEvent extends EventObject 54 { 55 /** Paths this event represents. */ 56 protected TreePath[] paths; 57 /** For each path identifies if that path is in fact new. */ 58 protected boolean[] areNew; 59 /** leadSelectionPath before the paths changed, may be null. */ 60 protected TreePath oldLeadSelectionPath; 61 /** leadSelectionPath after the paths changed, may be null. */ 62 protected TreePath newLeadSelectionPath; 63 64 /** 65 * Represents a change in the selection of a TreeSelectionModel. 66 * paths identifies the paths that have been either added or 67 * removed from the selection. 68 * 69 * @param source source of event 70 * @param paths the paths that have changed in the selection 71 */ 72 public TreeSelectionEvent(Object source, TreePath[] paths, 73 boolean[] areNew, TreePath oldLeadSelectionPath, 74 TreePath newLeadSelectionPath) 75 { 76 super(source); 77 this.paths = paths; 78 this.areNew = areNew; 79 this.oldLeadSelectionPath = oldLeadSelectionPath; 80 this.newLeadSelectionPath = newLeadSelectionPath; 81 } 82 83 /** 84 * Represents a change in the selection of a TreeSelectionModel. 85 * path identifies the path that have been either added or 86 * removed from the selection. 87 * 88 * @param source source of event 89 * @param path the path that has changed in the selection 90 * @param isNew whether or not the path is new to the selection, false 91 * means path was removed from the selection. 92 */ 93 public TreeSelectionEvent(Object source, TreePath path, boolean isNew, 94 TreePath oldLeadSelectionPath, 95 TreePath newLeadSelectionPath) 96 { 97 super(source); 98 paths = new TreePath[1]; 99 paths[0] = path; 100 areNew = new boolean[1]; 101 areNew[0] = isNew; 102 this.oldLeadSelectionPath = oldLeadSelectionPath; 103 this.newLeadSelectionPath = newLeadSelectionPath; 104 } 105 106 /** 107 * Returns the paths that have been added or removed from the 108 * selection. 109 */ 110 public TreePath[] getPaths() 111 { 112 int numPaths; 113 TreePath[] retPaths; 114 115 numPaths = paths.length; 116 retPaths = new TreePath[numPaths]; 117 System.arraycopy(paths, 0, retPaths, 0, numPaths); 118 return retPaths; 119 } 120 121 /** 122 * Returns the first path element. 123 */ 124 public TreePath getPath() 125 { 126 return paths[0]; 127 } 128 129 /** 130 * Returns whether the path identified by {@code getPath} was 131 * added to the selection. A return value of {@code true} 132 * indicates the path identified by {@code getPath} was added to 133 * the selection. A return value of {@code false} indicates {@code 134 * getPath} was selected, but is no longer selected. 135 * 136 * @return {@code true} if {@code getPath} was added to the selection, 137 * {@code false} otherwise 138 */ 139 public boolean isAddedPath() { 140 return areNew[0]; 141 } 142 143 /** 144 * Returns whether the specified path was added to the selection. 145 * A return value of {@code true} indicates the path identified by 146 * {@code path} was added to the selection. A return value of 147 * {@code false} indicates {@code path} is no longer selected. This method 148 * is only valid for the paths returned from {@code getPaths()}; invoking 149 * with a path not included in {@code getPaths()} throws an 150 * {@code IllegalArgumentException}. 151 * 152 * @param path the path to test 153 * @return {@code true} if {@code path} was added to the selection, 154 * {@code false} otherwise 155 * @throws IllegalArgumentException if {@code path} is not contained 156 * in {@code getPaths} 157 * @see #getPaths 158 */ 159 public boolean isAddedPath(TreePath path) { 160 for(int counter = paths.length - 1; counter >= 0; counter--) 161 if(paths[counter].equals(path)) 162 return areNew[counter]; 163 throw new IllegalArgumentException("path is not a path identified by the TreeSelectionEvent"); 164 } 165 166 /** 167 * Returns whether the path at {@code getPaths()[index]} was added 168 * to the selection. A return value of {@code true} indicates the 169 * path was added to the selection. A return value of {@code false} 170 * indicates the path is no longer selected. 171 * 172 * @param index the index of the path to test 173 * @return {@code true} if the path was added to the selection, 174 * {@code false} otherwise 175 * @throws IllegalArgumentException if index is outside the range of 176 * {@code getPaths} 177 * @see #getPaths 178 * 179 * @since 1.3 180 */ 181 public boolean isAddedPath(int index) { 182 if (paths == null || index < 0 || index >= paths.length) { 183 throw new IllegalArgumentException("index is beyond range of added paths identified by TreeSelectionEvent"); 184 } 185 return areNew[index]; 186 } 187 188 /** 189 * Returns the path that was previously the lead path. 190 */ 191 public TreePath getOldLeadSelectionPath() { 192 return oldLeadSelectionPath; 193 } 194 195 /** 196 * Returns the current lead path. 197 */ 198 public TreePath getNewLeadSelectionPath() { 199 return newLeadSelectionPath; 200 } 201 202 /** 203 * Returns a copy of the receiver, but with the source being newSource. 204 */ 205 public Object cloneWithSource(Object newSource) { 206 // Fix for IE bug - crashing 207 return new TreeSelectionEvent(newSource, paths,areNew, 208 oldLeadSelectionPath, 209 newLeadSelectionPath); 210 } 211 }