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