KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > event > TreeSelectionEvent


1 /*
2  * @(#)TreeSelectionEvent.java 1.26 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.event;
9
10 import java.util.EventObject JavaDoc;
11 import javax.swing.tree.TreePath JavaDoc;
12
13 /**
14  * An event that characterizes a change in the current
15  * selection. The change is based on any number of paths.
16  * TreeSelectionListeners will generally query the source of
17  * the event for the new selected status of each potentially
18  * changed row.
19  * <p>
20  * <strong>Warning:</strong>
21  * Serialized objects of this class will not be compatible with
22  * future Swing releases. The current serialization support is
23  * appropriate for short term storage or RMI between applications running
24  * the same version of Swing. As of 1.4, support for long term storage
25  * of all JavaBeans<sup><font size="-2">TM</font></sup>
26  * has been added to the <code>java.beans</code> package.
27  * Please see {@link java.beans.XMLEncoder}.
28  *
29  * @see TreeSelectionListener
30  * @see javax.swing.tree.TreeSelectionModel
31  *
32  * @version 1.26 12/19/03
33  * @author Scott Violet
34  */

35 public class TreeSelectionEvent extends EventObject JavaDoc
36 {
37     /** Paths this event represents. */
38     protected TreePath JavaDoc[] paths;
39     /** For each path identifies if that is path is in fact new. */
40     protected boolean[] areNew;
41     /** leadSelectionPath before the paths changed, may be null. */
42     protected TreePath JavaDoc oldLeadSelectionPath;
43     /** leadSelectionPath after the paths changed, may be null. */
44     protected TreePath JavaDoc newLeadSelectionPath;
45
46     /**
47       * Represents a change in the selection of a TreeSelectionModel.
48       * paths identifies the paths that have been either added or
49       * removed from the selection.
50       *
51       * @param source source of event
52       * @param paths the paths that have changed in the selection
53       */

54     public TreeSelectionEvent(Object JavaDoc source, TreePath JavaDoc[] paths,
55                   boolean[] areNew, TreePath JavaDoc oldLeadSelectionPath,
56                   TreePath JavaDoc newLeadSelectionPath)
57     {
58     super(source);
59     this.paths = paths;
60     this.areNew = areNew;
61     this.oldLeadSelectionPath = oldLeadSelectionPath;
62     this.newLeadSelectionPath = newLeadSelectionPath;
63     }
64
65     /**
66       * Represents a change in the selection of a TreeSelectionModel.
67       * path identifies the path that have been either added or
68       * removed from the selection.
69       *
70       * @param source source of event
71       * @param path the path that has changed in the selection
72       * @param isNew whether or not the path is new to the selection, false
73       * means path was removed from the selection.
74       */

75     public TreeSelectionEvent(Object JavaDoc source, TreePath JavaDoc path, boolean isNew,
76                   TreePath JavaDoc oldLeadSelectionPath,
77                   TreePath JavaDoc newLeadSelectionPath)
78     {
79     super(source);
80     paths = new TreePath JavaDoc[1];
81     paths[0] = path;
82     areNew = new boolean[1];
83     areNew[0] = isNew;
84     this.oldLeadSelectionPath = oldLeadSelectionPath;
85     this.newLeadSelectionPath = newLeadSelectionPath;
86     }
87
88     /**
89       * Returns the paths that have been added or removed from the
90       * selection.
91       */

92     public TreePath JavaDoc[] getPaths()
93     {
94     int numPaths;
95     TreePath JavaDoc[] retPaths;
96
97     numPaths = paths.length;
98     retPaths = new TreePath JavaDoc[numPaths];
99     System.arraycopy(paths, 0, retPaths, 0, numPaths);
100     return retPaths;
101     }
102
103     /**
104       * Returns the first path element.
105       */

106     public TreePath JavaDoc getPath()
107     {
108     return paths[0];
109     }
110
111     /**
112      * Returns true if the first path element has been added to the
113      * selection, a return value of false means the first path has been
114      * removed from the selection.
115      */

116     public boolean isAddedPath() {
117     return areNew[0];
118     }
119
120     /**
121      * Returns true if the path identified by path was added to the
122      * selection. A return value of false means the path was in the
123      * selection but is no longer in the selection. This will raise if
124      * path is not one of the paths identified by this event.
125      */

126     public boolean isAddedPath(TreePath JavaDoc path) {
127     for(int counter = paths.length - 1; counter >= 0; counter--)
128         if(paths[counter].equals(path))
129         return areNew[counter];
130     throw new IllegalArgumentException JavaDoc("path is not a path identified by the TreeSelectionEvent");
131     }
132
133     /**
134      * Returns true if the path identified by <code>index</code> was added to
135      * the selection. A return value of false means the path was in the
136      * selection but is no longer in the selection. This will raise if
137      * index < 0 || >= <code>getPaths</code>.length.
138      *
139      * @since 1.3
140      */

141     public boolean isAddedPath(int index) {
142     if (paths == null || index < 0 || index >= paths.length) {
143         throw new IllegalArgumentException JavaDoc("index is beyond range of added paths identified by TreeSelectionEvent");
144     }
145     return areNew[index];
146     }
147
148     /**
149      * Returns the path that was previously the lead path.
150      */

151     public TreePath JavaDoc getOldLeadSelectionPath() {
152     return oldLeadSelectionPath;
153     }
154
155     /**
156      * Returns the current lead path.
157      */

158     public TreePath JavaDoc getNewLeadSelectionPath() {
159     return newLeadSelectionPath;
160     }
161
162     /**
163      * Returns a copy of the receiver, but with the source being newSource.
164      */

165     public Object JavaDoc cloneWithSource(Object JavaDoc newSource) {
166       // Fix for IE bug - crashing
167
return new TreeSelectionEvent JavaDoc(newSource, paths,areNew,
168                     oldLeadSelectionPath,
169                     newLeadSelectionPath);
170     }
171 }
172
Popular Tags