svn-gvsig-desktop / tags / v1_1_Build_1015 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / EventBuffer.java @ 13679
History | View | Annotate | Download (10.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap; |
42 |
|
43 |
import com.iver.cit.gvsig.fmap.layers.CancelationException; |
44 |
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent; |
45 |
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener; |
46 |
import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
47 |
import com.iver.cit.gvsig.fmap.layers.LayerListener; |
48 |
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent; |
49 |
import com.iver.cit.gvsig.fmap.layers.LegendListener; |
50 |
import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
51 |
import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
52 |
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent; |
53 |
|
54 |
import java.util.ArrayList; |
55 |
import java.util.Iterator; |
56 |
|
57 |
|
58 |
/**
|
59 |
* <p>The class <code>EventBuffer</code> represents a buffer of events that allows store listeners for the events, and
|
60 |
* configure its dispatching.</p>
|
61 |
*
|
62 |
* <p>The <i>dispatching mode:</i>
|
63 |
* <ul>
|
64 |
* <li>If <code>true</code> : dispatches each new event received.
|
65 |
* <li>If <code>false</code> : accumulates all new events received in a internal buffer, and only will dispatch them
|
66 |
* (according to the order they were received) when changes the mode.
|
67 |
* </ul>
|
68 |
* </p>
|
69 |
*
|
70 |
* @see LegendListener
|
71 |
* @see LayerCollectionListener
|
72 |
* @see SelectionListener
|
73 |
* @see ViewPortListener
|
74 |
* @see LegendListener
|
75 |
*
|
76 |
* @author Fernando Gonz?lez Cort?s
|
77 |
*/
|
78 |
public class EventBuffer implements LegendListener, LayerCollectionListener, |
79 |
SelectionListener, ViewPortListener, LayerListener { |
80 |
|
81 |
/**
|
82 |
* List with all events received and don't dispatched.
|
83 |
*
|
84 |
* @see #endAtomicEvent()
|
85 |
* @see #legendChanged(LegendChangedEvent)
|
86 |
* @see #layerAdded(LayerCollectionEvent)
|
87 |
* @see #layerMoved(LayerPositionEvent)
|
88 |
* @see #layerRemoved(LayerCollectionEvent)
|
89 |
* @see #layerAdding(LayerCollectionEvent)
|
90 |
* @see #layerMoving(LayerPositionEvent)
|
91 |
* @see #layerRemoving(LayerCollectionEvent)
|
92 |
* @see #visibilityChanged(LayerCollectionEvent)
|
93 |
* @see #visibilityChanged(LayerEvent)
|
94 |
* @see #selectionChanged(SelectionEvent)
|
95 |
* @see #extentChanged(ExtentEvent)
|
96 |
* @see #activationChanged(LayerEvent)
|
97 |
* @see #nameChanged(LayerEvent)
|
98 |
* @see #backColorChanged(ColorEvent)
|
99 |
* @see #editionChanged(LayerEvent)
|
100 |
* @see #projectionChanged(ProjectionEvent)
|
101 |
* @see #fireAtomicEventListener
|
102 |
*/
|
103 |
private ArrayList events = new ArrayList(); |
104 |
|
105 |
/**
|
106 |
* List with all listeners registered.
|
107 |
*
|
108 |
* @see #addAtomicEventListener(AtomicEventListener)
|
109 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
110 |
*/
|
111 |
private ArrayList listeners = new ArrayList(); |
112 |
|
113 |
/**
|
114 |
* Allows enable or disable the <i>dispatching mode</i>.
|
115 |
*
|
116 |
* @see #beginAtomicEvent()
|
117 |
* @see #endAtomicEvent()
|
118 |
*/
|
119 |
private boolean dispatching = true; |
120 |
|
121 |
/**
|
122 |
* Enables buffer in <i>accumulation event</i> mode. And then, all new events received, will be accumulated and
|
123 |
* don't notified to their respective listeners, until this buffer receive a call to {@link #endAtomicEvent() endAtomicEvent}.
|
124 |
*
|
125 |
* @see #endAtomicEvent()
|
126 |
*/
|
127 |
public void beginAtomicEvent() { |
128 |
dispatching = false;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Disables buffer in <i>accumulation event</i> mode. And then, all events accumulated will be notify to their respective
|
133 |
* listeners, in the same order as they arrived.
|
134 |
*
|
135 |
* @see #beginAtomicEvent()
|
136 |
*/
|
137 |
public void endAtomicEvent() { |
138 |
fireAtomicEventListener(); |
139 |
events.clear(); |
140 |
dispatching = true;
|
141 |
} |
142 |
|
143 |
/*
|
144 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
|
145 |
*/
|
146 |
public void legendChanged(LegendChangedEvent e) { |
147 |
events.add(e); |
148 |
|
149 |
if (dispatching) {
|
150 |
fireAtomicEventListener(); |
151 |
} |
152 |
} |
153 |
|
154 |
/*
|
155 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
156 |
*/
|
157 |
public void layerAdded(LayerCollectionEvent e) { |
158 |
events.add(e); |
159 |
|
160 |
if (dispatching) {
|
161 |
fireAtomicEventListener(); |
162 |
} |
163 |
} |
164 |
|
165 |
/*
|
166 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
167 |
*/
|
168 |
public void layerMoved(LayerPositionEvent e) { |
169 |
events.add(e); |
170 |
|
171 |
if (dispatching) {
|
172 |
fireAtomicEventListener(); |
173 |
} |
174 |
} |
175 |
|
176 |
/*
|
177 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
178 |
*/
|
179 |
public void layerRemoved(LayerCollectionEvent e) { |
180 |
events.add(e); |
181 |
|
182 |
if (dispatching) {
|
183 |
fireAtomicEventListener(); |
184 |
} |
185 |
} |
186 |
|
187 |
/*
|
188 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
189 |
*/
|
190 |
public void layerAdding(LayerCollectionEvent e) throws CancelationException { |
191 |
events.add(e); |
192 |
|
193 |
if (dispatching) {
|
194 |
fireAtomicEventListener(); |
195 |
} |
196 |
} |
197 |
|
198 |
/*
|
199 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
200 |
*/
|
201 |
public void layerMoving(LayerPositionEvent e) throws CancelationException { |
202 |
events.add(e); |
203 |
|
204 |
if (dispatching) {
|
205 |
fireAtomicEventListener(); |
206 |
} |
207 |
} |
208 |
|
209 |
/*
|
210 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
211 |
*/
|
212 |
public void layerRemoving(LayerCollectionEvent e) |
213 |
throws CancelationException {
|
214 |
events.add(e); |
215 |
|
216 |
if (dispatching) {
|
217 |
fireAtomicEventListener(); |
218 |
} |
219 |
} |
220 |
|
221 |
/*
|
222 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
223 |
*/
|
224 |
public void visibilityChanged(LayerCollectionEvent e) |
225 |
throws CancelationException {
|
226 |
events.add(e); |
227 |
|
228 |
if (dispatching) {
|
229 |
fireAtomicEventListener(); |
230 |
} |
231 |
} |
232 |
|
233 |
/*
|
234 |
* @see com.iver.cit.gvsig.fmap.layers.SelectionListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.SelectionEvent)
|
235 |
*/
|
236 |
public void selectionChanged(SelectionEvent e) { |
237 |
events.add(e); |
238 |
|
239 |
if (dispatching) {
|
240 |
fireAtomicEventListener(); |
241 |
} |
242 |
} |
243 |
|
244 |
/*
|
245 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
|
246 |
*/
|
247 |
public void extentChanged(ExtentEvent e) { |
248 |
events.add(e); |
249 |
|
250 |
if (dispatching) {
|
251 |
fireAtomicEventListener(); |
252 |
} |
253 |
} |
254 |
|
255 |
/**
|
256 |
* Appends, if it wasn't, the specified listener to the end of the internal list of atomic event listeners.
|
257 |
*
|
258 |
* @param listener an object that implements the atomic event listener
|
259 |
*
|
260 |
* @return <code>true</code> if has added the listener successfully
|
261 |
*
|
262 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
263 |
* @see #fireAtomicEventListener()
|
264 |
*/
|
265 |
public boolean addAtomicEventListener(AtomicEventListener listener) { |
266 |
boolean bFound = false; |
267 |
for (int i=0; i < listeners.size(); i++) |
268 |
if (listeners.get(i) == listener)
|
269 |
bFound = true;
|
270 |
if (!bFound)
|
271 |
listeners.add(listener); |
272 |
return true; |
273 |
} |
274 |
|
275 |
/**
|
276 |
* <p>Removes a single instance of the {@link AtomicEventListener AtomicEventListener} from the
|
277 |
* internal list, if it is present (optional operation). Returns <tt>true</tt>
|
278 |
* if the list contained the specified element (or equivalently, if the list changed as a
|
279 |
* result of the call).<p>
|
280 |
*
|
281 |
* @param o element to be removed from this list, if present
|
282 |
* @return <tt>true</tt> if the list contained the specified element
|
283 |
*
|
284 |
* @see #addAtomicEventListener(AtomicEventListener)
|
285 |
* @see #fireAtomicEventListener()
|
286 |
*/
|
287 |
public boolean removeAtomicEventListener(AtomicEventListener listener) { |
288 |
return listeners.remove(listener);
|
289 |
} |
290 |
|
291 |
/**
|
292 |
* Executes the {@linkplain AtomicEventListener#atomicEvent(AtomicEvent)} method of all listeners registered.
|
293 |
*
|
294 |
* @see #addAtomicEventListener(AtomicEventListener)
|
295 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
296 |
*/
|
297 |
private void fireAtomicEventListener() { |
298 |
if (events.size() == 0) |
299 |
return; // No hay eventos que lanzar. |
300 |
for (Iterator i = listeners.iterator(); i.hasNext();) { |
301 |
AtomicEventListener listener = (AtomicEventListener) i.next(); |
302 |
AtomicEvent e = new AtomicEvent(events);
|
303 |
listener.atomicEvent(e); |
304 |
} |
305 |
|
306 |
events.clear(); |
307 |
} |
308 |
|
309 |
/*
|
310 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
311 |
*/
|
312 |
public void visibilityChanged(LayerEvent e) { |
313 |
events.add(e); |
314 |
|
315 |
if (dispatching) {
|
316 |
fireAtomicEventListener(); |
317 |
} |
318 |
} |
319 |
|
320 |
/*
|
321 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
322 |
*/
|
323 |
public void activationChanged(LayerEvent e) { |
324 |
events.add(e); |
325 |
|
326 |
if (dispatching) {
|
327 |
fireAtomicEventListener(); |
328 |
} |
329 |
} |
330 |
|
331 |
/*
|
332 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#nameChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
333 |
*/
|
334 |
public void nameChanged(LayerEvent e) { |
335 |
events.add(e); |
336 |
|
337 |
if (dispatching) {
|
338 |
fireAtomicEventListener(); |
339 |
} |
340 |
} |
341 |
|
342 |
/*
|
343 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
|
344 |
*/
|
345 |
public void backColorChanged(ColorEvent e) { |
346 |
events.add(e); |
347 |
|
348 |
if (dispatching) {
|
349 |
fireAtomicEventListener(); |
350 |
} |
351 |
} |
352 |
|
353 |
/*
|
354 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#editionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
355 |
*/
|
356 |
public void editionChanged(LayerEvent e) { |
357 |
events.add(e); |
358 |
|
359 |
if (dispatching) {
|
360 |
fireAtomicEventListener(); |
361 |
} |
362 |
|
363 |
} |
364 |
|
365 |
/*
|
366 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
|
367 |
*/
|
368 |
public void projectionChanged(ProjectionEvent e) { |
369 |
events.add(e); |
370 |
|
371 |
if (dispatching) {
|
372 |
fireAtomicEventListener(); |
373 |
} |
374 |
} |
375 |
} |