Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TreeModelSupport |
|
| 2.3333333333333335;2.333 |
1 | /* $Id: TreeModelSupport.java 17841 2010-01-12 19:17:52Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2008 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.ui; | |
40 | ||
41 | import javax.swing.event.EventListenerList; | |
42 | import javax.swing.event.TreeModelEvent; | |
43 | import javax.swing.event.TreeModelListener; | |
44 | ||
45 | /** | |
46 | * Helper class for tree models that provides tree event handling.<p> | |
47 | * | |
48 | * @author alexb | |
49 | * @since 0.13.5, Created on 15 April 2003 | |
50 | */ | |
51 | public class TreeModelSupport extends PerspectiveSupport { | |
52 | ||
53 | /** tree model listener list. */ | |
54 | 5400 | private EventListenerList listenerList = new EventListenerList(); |
55 | ||
56 | /** | |
57 | * The constructor. | |
58 | * | |
59 | * @param name the name that will be localized | |
60 | */ | |
61 | public TreeModelSupport(String name) { | |
62 | 5400 | super(name); |
63 | 5400 | } |
64 | ||
65 | // ---------------- listener management ---------------- | |
66 | ||
67 | /** | |
68 | * Add a TreeModelListener to the list of listeners. | |
69 | * | |
70 | * @param l the listener to be added | |
71 | */ | |
72 | public void addTreeModelListener(TreeModelListener l) { | |
73 | 3423 | listenerList.add(TreeModelListener.class, l); |
74 | 3423 | } |
75 | ||
76 | /** | |
77 | * Remove a TreeModelListener from the list of listeners.. | |
78 | * | |
79 | * @param l the listener to be removed | |
80 | */ | |
81 | public void removeTreeModelListener(TreeModelListener l) { | |
82 | 900 | listenerList.remove(TreeModelListener.class, l); |
83 | 900 | } |
84 | ||
85 | // --------------- tree nodes ------------------------- | |
86 | ||
87 | /** | |
88 | * Notify all listeners that a node (or a set of siblings) has changed in | |
89 | * some way. The node(s) have not changed locations in the tree or altered | |
90 | * their children arrays, but other attributes have changed and may affect | |
91 | * presentation. | |
92 | * <p> | |
93 | * To indicate the root has changed, childIndices and children will be null. | |
94 | * <p> | |
95 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
96 | * Swing/AWT event thread. | |
97 | * | |
98 | * @param source the Object responsible for generating the event (typically | |
99 | * the creator of the event object passes this for its value) | |
100 | * @param path an array of Object identifying the path to the parent of the | |
101 | * modified item(s), where the first element of the array is | |
102 | * the Object stored at the root node and the last element is | |
103 | * the Object stored at the parent node | |
104 | * @param childIndices an array of int that specifies the index values of | |
105 | * the removed items. The indices must be in sorted order, | |
106 | * from lowest to highest | |
107 | * @param children an array of Object containing the inserted, removed, or | |
108 | * changed objects | |
109 | * @see TreeModelListener#treeNodesChanged(TreeModelEvent) | |
110 | */ | |
111 | protected void fireTreeNodesChanged( | |
112 | final Object source, | |
113 | final Object[] path, | |
114 | final int[] childIndices, | |
115 | final Object[] children) { | |
116 | ||
117 | // Guaranteed to return a non-null array | |
118 | 0 | Object[] listeners = listenerList.getListenerList(); |
119 | 0 | TreeModelEvent e = null; |
120 | // Process the listeners last to first, notifying | |
121 | // those that are interested in this event | |
122 | 0 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
123 | 0 | if (listeners[i] == TreeModelListener.class) { |
124 | // Lazily create the event: | |
125 | 0 | if (e == null) { |
126 | 0 | e = |
127 | new TreeModelEvent( | |
128 | source, | |
129 | path, | |
130 | childIndices, | |
131 | children); | |
132 | } | |
133 | 0 | ((TreeModelListener) listeners[i + 1]).treeNodesChanged(e); |
134 | } | |
135 | } | |
136 | 0 | } |
137 | ||
138 | /** | |
139 | * Notify all listeners a node has been inserted. The event instance | |
140 | * is lazily created using the parameters passed into | |
141 | * the fire method. | |
142 | * <p> | |
143 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
144 | * Swing/AWT event thread. | |
145 | * | |
146 | * @param source the Object responsible for generating the event (typically | |
147 | * the creator of the event object passes this for its value) | |
148 | * @param path an array of Object identifying the path to the parent of the | |
149 | * modified item(s), where the first element of the array is | |
150 | * the Object stored at the root node and the last element is | |
151 | * the Object stored at the parent node | |
152 | * @param childIndices an array of int that specifies the index values of | |
153 | * the removed items. The indices must be in sorted order, | |
154 | * from lowest to highest | |
155 | * @param children an array of Object containing the inserted, removed, or | |
156 | * changed objects | |
157 | * @see TreeModelListener#treeNodesChanged(TreeModelEvent) | |
158 | */ | |
159 | protected void fireTreeNodesInserted( | |
160 | Object source, | |
161 | Object[] path, | |
162 | int[] childIndices, | |
163 | Object[] children) { | |
164 | ||
165 | // Guaranteed to return a non-null array | |
166 | 1967 | Object[] listeners = listenerList.getListenerList(); |
167 | 1967 | TreeModelEvent e = null; |
168 | // Process the listeners last to first, notifying | |
169 | // those that are interested in this event | |
170 | 6054 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
171 | 4087 | if (listeners[i] == TreeModelListener.class) { |
172 | // Lazily create the event: | |
173 | 4087 | if (e == null) { |
174 | 1967 | e = |
175 | new TreeModelEvent( | |
176 | source, | |
177 | path, | |
178 | childIndices, | |
179 | children); | |
180 | } | |
181 | 4087 | ((TreeModelListener) listeners[i + 1]).treeNodesInserted(e); |
182 | } | |
183 | } | |
184 | 1967 | } |
185 | ||
186 | /** | |
187 | * Notify all listeners that nodes have been removed from the tree. Note | |
188 | * that if a subtree is removed from the tree, this method may only be | |
189 | * invoked once for the root of the removed subtree, not once for each | |
190 | * individual set of siblings removed. | |
191 | * <p> | |
192 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
193 | * Swing/AWT event thread. | |
194 | * | |
195 | * @param source the Object responsible for generating the event (typically | |
196 | * the creator of the event object passes this for its value) | |
197 | * @param path an array of Object identifying the path to the parent of the | |
198 | * modified item(s), where the first element of the array is | |
199 | * the Object stored at the root node and the last element is | |
200 | * the Object stored at the parent node | |
201 | * @param childIndices an array of int that specifies the index values of | |
202 | * the removed items. The indices must be in sorted order, | |
203 | * from lowest to highest | |
204 | * @param children an array of Object containing the inserted, removed, or | |
205 | * changed objects | |
206 | * @see TreeModelListener#treeNodesChanged(TreeModelEvent) | |
207 | * @see EventListenerList | |
208 | */ | |
209 | protected void fireTreeNodesRemoved( | |
210 | Object source, | |
211 | Object[] path, | |
212 | int[] childIndices, | |
213 | Object[] children) { | |
214 | ||
215 | // Guaranteed to return a non-null array | |
216 | 0 | Object[] listeners = listenerList.getListenerList(); |
217 | 0 | TreeModelEvent e = null; |
218 | // Process the listeners last to first, notifying | |
219 | // those that are interested in this event | |
220 | 0 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
221 | 0 | if (listeners[i] == TreeModelListener.class) { |
222 | // Lazily create the event: | |
223 | 0 | if (e == null) { |
224 | 0 | e = |
225 | new TreeModelEvent( | |
226 | source, | |
227 | path, | |
228 | childIndices, | |
229 | children); | |
230 | } | |
231 | 0 | ((TreeModelListener) listeners[i + 1]).treeNodesRemoved(e); |
232 | } | |
233 | } | |
234 | 0 | } |
235 | ||
236 | // ------------- tree structure ----------------- | |
237 | ||
238 | /** | |
239 | * Notify all listeners that the tree has drastically changed | |
240 | * structure from a given node down. If the path returned by e.getPath() is | |
241 | * of length one and the first element does not identify the current root | |
242 | * node the first element should become the new root of the tree. | |
243 | * <p> | |
244 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
245 | * Swing/AWT event thread. | |
246 | * | |
247 | * @param path an array of Object identifying the path to the parent of the | |
248 | * modified item(s), where the first element of the array is | |
249 | * the Object stored at the root node and the last element is | |
250 | * the Object stored at the parent node | |
251 | * @see TreeModelListener#treeStructureChanged(TreeModelEvent) | |
252 | */ | |
253 | protected void fireTreeStructureChanged(Object[] path) { | |
254 | 159 | fireTreeStructureChanged(this, path); |
255 | 159 | } |
256 | ||
257 | /** | |
258 | * Notify all listeners that the tree has drastically changed | |
259 | * structure from a given node down. If the path returned by e.getPath() is | |
260 | * of length one and the first element does not identify the current root | |
261 | * node the first element should become the new root of the tree. | |
262 | * <p> | |
263 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
264 | * Swing/AWT event thread. | |
265 | * | |
266 | * @param source the Object responsible for generating the event (typically | |
267 | * the creator of the event object passes this for its value) | |
268 | * @param path an array of Object identifying the path to the parent of the | |
269 | * modified item(s), where the first element of the array is | |
270 | * the Object stored at the root node and the last element is | |
271 | * the Object stored at the parent node | |
272 | * @see TreeModelListener#treeStructureChanged(TreeModelEvent) | |
273 | */ | |
274 | protected void fireTreeStructureChanged(Object source, Object[] path) { | |
275 | 159 | fireTreeStructureChanged(source, path, null, null); |
276 | 159 | } |
277 | ||
278 | /** | |
279 | * Notify all listeners that the tree has drastically changed structure from | |
280 | * a given node down. If the path returned by e.getPath() is of length one | |
281 | * and the first element does not identify the current root node the first | |
282 | * element should become the new root of the tree. | |
283 | * <p> | |
284 | * <em>NOTE:</em> This is a Swing method which must be invoked on the | |
285 | * Swing/AWT event thread. | |
286 | * | |
287 | * @param source the Object responsible for generating the event (typically | |
288 | * the creator of the event object passes this for its value) | |
289 | * @param path an array of Object identifying the path to the parent of the | |
290 | * modified item(s), where the first element of the array is | |
291 | * the Object stored at the root node and the last element is | |
292 | * the Object stored at the parent node | |
293 | * @param childIndices an array of int that specifies the index values of | |
294 | * the removed items. The indices must be in sorted order, | |
295 | * from lowest to highest | |
296 | * @param children an array of Object containing the inserted, removed, or | |
297 | * changed objects | |
298 | * @see TreeModelListener#treeStructureChanged(TreeModelEvent) | |
299 | */ | |
300 | public void fireTreeStructureChanged( | |
301 | Object source, | |
302 | Object[] path, | |
303 | int[] childIndices, | |
304 | Object[] children) { | |
305 | ||
306 | // Guaranteed to return a non-null array | |
307 | 159 | Object[] listeners = listenerList.getListenerList(); |
308 | 159 | TreeModelEvent e = null; |
309 | // Process the listeners last to first, notifying | |
310 | // those that are interested in this event | |
311 | 614 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
312 | 455 | if (listeners[i] == TreeModelListener.class) { |
313 | // Lazily create the event: | |
314 | 455 | if (e == null) { |
315 | 159 | e = |
316 | new TreeModelEvent( | |
317 | source, | |
318 | path, | |
319 | childIndices, | |
320 | children); | |
321 | } | |
322 | 455 | ((TreeModelListener) listeners[i + 1]).treeStructureChanged(e); |
323 | } | |
324 | } | |
325 | 159 | } |
326 | ||
327 | } |