svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / EventBuffer.java @ 20100
History | View | Annotate | Download (10.5 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><code>EventBuffer</code> represents a buffer of events that allows store listeners of events produced in layers
|
60 |
* of a <code>MapContext</code> instance, and configure its dispatching mode.</p>
|
61 |
*
|
62 |
* <p>The <i>dispatching mode</i>:
|
63 |
* <ul>
|
64 |
* <li><code>true</code> : dispatches each new event received.</li>
|
65 |
* <li><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.</li>
|
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 |
* <p>Enables buffer in <i>accumulation event</i> mode.<p>
|
123 |
*
|
124 |
* <p>All new events received, will be accumulated and won't notified to their respective listeners,
|
125 |
* until this buffer would received a call to {@link #endAtomicEvent() endAtomicEvent}.</p>
|
126 |
*
|
127 |
* @see #endAtomicEvent()
|
128 |
*/
|
129 |
public void beginAtomicEvent() { |
130 |
dispatching = false;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* <p>Disables buffer in <i>accumulation event</i> mode.</p>
|
135 |
*
|
136 |
* <p>All events accumulated will be notify to their respective
|
137 |
* listeners, in the same order as they arrived.</p>
|
138 |
*
|
139 |
* @see #beginAtomicEvent()
|
140 |
*/
|
141 |
public void endAtomicEvent() { |
142 |
fireAtomicEventListener(); |
143 |
events.clear(); |
144 |
dispatching = true;
|
145 |
} |
146 |
|
147 |
/*
|
148 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
|
149 |
*/
|
150 |
public void legendChanged(LegendChangedEvent e) { |
151 |
events.add(e); |
152 |
|
153 |
if (dispatching) {
|
154 |
fireAtomicEventListener(); |
155 |
} |
156 |
} |
157 |
|
158 |
/*
|
159 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
160 |
*/
|
161 |
public void layerAdded(LayerCollectionEvent e) { |
162 |
events.add(e); |
163 |
|
164 |
if (dispatching) {
|
165 |
fireAtomicEventListener(); |
166 |
} |
167 |
} |
168 |
|
169 |
/*
|
170 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
171 |
*/
|
172 |
public void layerMoved(LayerPositionEvent e) { |
173 |
events.add(e); |
174 |
|
175 |
if (dispatching) {
|
176 |
fireAtomicEventListener(); |
177 |
} |
178 |
} |
179 |
|
180 |
/*
|
181 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
182 |
*/
|
183 |
public void layerRemoved(LayerCollectionEvent e) { |
184 |
events.add(e); |
185 |
|
186 |
if (dispatching) {
|
187 |
fireAtomicEventListener(); |
188 |
} |
189 |
} |
190 |
|
191 |
/*
|
192 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
193 |
*/
|
194 |
public void layerAdding(LayerCollectionEvent e) throws CancelationException { |
195 |
events.add(e); |
196 |
|
197 |
if (dispatching) {
|
198 |
fireAtomicEventListener(); |
199 |
} |
200 |
} |
201 |
|
202 |
/*
|
203 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
|
204 |
*/
|
205 |
public void layerMoving(LayerPositionEvent e) throws CancelationException { |
206 |
events.add(e); |
207 |
|
208 |
if (dispatching) {
|
209 |
fireAtomicEventListener(); |
210 |
} |
211 |
} |
212 |
|
213 |
/*
|
214 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
215 |
*/
|
216 |
public void layerRemoving(LayerCollectionEvent e) |
217 |
throws CancelationException {
|
218 |
events.add(e); |
219 |
|
220 |
if (dispatching) {
|
221 |
fireAtomicEventListener(); |
222 |
} |
223 |
} |
224 |
|
225 |
/*
|
226 |
* @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
|
227 |
*/
|
228 |
public void visibilityChanged(LayerCollectionEvent e) |
229 |
throws CancelationException {
|
230 |
events.add(e); |
231 |
|
232 |
if (dispatching) {
|
233 |
fireAtomicEventListener(); |
234 |
} |
235 |
} |
236 |
|
237 |
/*
|
238 |
* @see com.iver.cit.gvsig.fmap.layers.SelectionListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.SelectionEvent)
|
239 |
*/
|
240 |
public void selectionChanged(SelectionEvent e) { |
241 |
events.add(e); |
242 |
|
243 |
if (dispatching) {
|
244 |
fireAtomicEventListener(); |
245 |
} |
246 |
} |
247 |
|
248 |
/*
|
249 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
|
250 |
*/
|
251 |
public void extentChanged(ExtentEvent e) { |
252 |
events.add(e); |
253 |
|
254 |
if (dispatching) {
|
255 |
fireAtomicEventListener(); |
256 |
} |
257 |
} |
258 |
|
259 |
/**
|
260 |
* Appends, if wasn't, the specified listener to the end of the internal list of atomic event listeners.
|
261 |
*
|
262 |
* @param listener an object that implements the atomic event listener
|
263 |
*
|
264 |
* @return <code>true</code> if has added the listener successfully; otherwise <code>false</code>
|
265 |
*
|
266 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
267 |
* @see #fireAtomicEventListener()
|
268 |
*/
|
269 |
public boolean addAtomicEventListener(AtomicEventListener listener) { |
270 |
boolean bFound = false; |
271 |
for (int i=0; i < listeners.size(); i++) |
272 |
if (listeners.get(i) == listener)
|
273 |
bFound = true;
|
274 |
if (!bFound)
|
275 |
listeners.add(listener); |
276 |
return true; |
277 |
} |
278 |
|
279 |
/**
|
280 |
* <p>Removes a single instance of the {@link AtomicEventListener AtomicEventListener} from the
|
281 |
* internal list, if it is present (optional operation). Returns <tt>true</tt>
|
282 |
* if the list contained the specified element (or equivalently, if the list changed as a
|
283 |
* result of the call).<p>
|
284 |
*
|
285 |
* @param o element to be removed from this list, if present
|
286 |
* @return <tt>true</tt> if the list contained the specified element
|
287 |
*
|
288 |
* @see #addAtomicEventListener(AtomicEventListener)
|
289 |
* @see #fireAtomicEventListener()
|
290 |
*/
|
291 |
public boolean removeAtomicEventListener(AtomicEventListener listener) { |
292 |
return listeners.remove(listener);
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* Executes the {@linkplain AtomicEventListener#atomicEvent(AtomicEvent)} method of all listeners registered.
|
297 |
*
|
298 |
* @see #addAtomicEventListener(AtomicEventListener)
|
299 |
* @see #removeAtomicEventListener(AtomicEventListener)
|
300 |
*/
|
301 |
private void fireAtomicEventListener() { |
302 |
if (events.size() == 0) |
303 |
return; // No hay eventos que lanzar. |
304 |
for (Iterator i = listeners.iterator(); i.hasNext();) { |
305 |
AtomicEventListener listener = (AtomicEventListener) i.next(); |
306 |
AtomicEvent e = new AtomicEvent(events);
|
307 |
listener.atomicEvent(e); |
308 |
} |
309 |
|
310 |
events.clear(); |
311 |
} |
312 |
|
313 |
/*
|
314 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
315 |
*/
|
316 |
public void visibilityChanged(LayerEvent e) { |
317 |
events.add(e); |
318 |
|
319 |
if (dispatching) {
|
320 |
fireAtomicEventListener(); |
321 |
} |
322 |
} |
323 |
|
324 |
/*
|
325 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
326 |
*/
|
327 |
public void activationChanged(LayerEvent e) { |
328 |
events.add(e); |
329 |
|
330 |
if (dispatching) {
|
331 |
fireAtomicEventListener(); |
332 |
} |
333 |
} |
334 |
|
335 |
/*
|
336 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#nameChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
337 |
*/
|
338 |
public void nameChanged(LayerEvent e) { |
339 |
events.add(e); |
340 |
|
341 |
if (dispatching) {
|
342 |
fireAtomicEventListener(); |
343 |
} |
344 |
} |
345 |
|
346 |
/*
|
347 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
|
348 |
*/
|
349 |
public void backColorChanged(ColorEvent e) { |
350 |
events.add(e); |
351 |
|
352 |
if (dispatching) {
|
353 |
fireAtomicEventListener(); |
354 |
} |
355 |
} |
356 |
|
357 |
/*
|
358 |
* @see com.iver.cit.gvsig.fmap.layers.LayerListener#editionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
359 |
*/
|
360 |
public void editionChanged(LayerEvent e) { |
361 |
events.add(e); |
362 |
|
363 |
if (dispatching) {
|
364 |
fireAtomicEventListener(); |
365 |
} |
366 |
|
367 |
} |
368 |
|
369 |
/*
|
370 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
|
371 |
*/
|
372 |
public void projectionChanged(ProjectionEvent e) { |
373 |
events.add(e); |
374 |
|
375 |
if (dispatching) {
|
376 |
fireAtomicEventListener(); |
377 |
} |
378 |
} |
379 |
} |