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 | 1100 | fjp | /* 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 | 679 | fernando | 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 | 727 | fernando | import com.iver.cit.gvsig.fmap.layers.LayerEvent; |
47 | import com.iver.cit.gvsig.fmap.layers.LayerListener; |
||
48 | 679 | fernando | 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 | 13316 | ppiqueras | * <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 | 13508 | ppiqueras | * (according to the order they were received) when changes the mode.
|
67 | 13316 | ppiqueras | * </ul>
|
68 | * </p>
|
||
69 | 679 | fernando | *
|
70 | 13369 | ppiqueras | * @see LegendListener
|
71 | * @see LayerCollectionListener
|
||
72 | * @see SelectionListener
|
||
73 | * @see ViewPortListener
|
||
74 | * @see LegendListener
|
||
75 | *
|
||
76 | 679 | fernando | * @author Fernando Gonz?lez Cort?s
|
77 | */
|
||
78 | public class EventBuffer implements LegendListener, LayerCollectionListener, |
||
79 | 956 | vcaballero | SelectionListener, ViewPortListener, LayerListener { |
80 | 13316 | ppiqueras | |
81 | /**
|
||
82 | * List with all events received and don't dispatched.
|
||
83 | 13369 | ppiqueras | *
|
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 | 13316 | ppiqueras | */
|
103 | 679 | fernando | private ArrayList events = new ArrayList(); |
104 | 13316 | ppiqueras | |
105 | /**
|
||
106 | * List with all listeners registered.
|
||
107 | 13369 | ppiqueras | *
|
108 | * @see #addAtomicEventListener(AtomicEventListener)
|
||
109 | * @see #removeAtomicEventListener(AtomicEventListener)
|
||
110 | 13316 | ppiqueras | */
|
111 | 679 | fernando | private ArrayList listeners = new ArrayList(); |
112 | 13316 | ppiqueras | |
113 | /**
|
||
114 | * Allows enable or disable the <i>dispatching mode</i>.
|
||
115 | 13369 | ppiqueras | *
|
116 | * @see #beginAtomicEvent()
|
||
117 | * @see #endAtomicEvent()
|
||
118 | 13316 | ppiqueras | */
|
119 | 679 | fernando | private boolean dispatching = true; |
120 | |||
121 | /**
|
||
122 | 13316 | ppiqueras | * Enables buffer in <i>accumulation event</i> mode. And then, all new events received, will be accumulated and
|
123 | 13369 | ppiqueras | * don't notified to their respective listeners, until this buffer receive a call to {@link #endAtomicEvent() endAtomicEvent}.
|
124 | 13483 | ppiqueras | *
|
125 | * @see #endAtomicEvent()
|
||
126 | 679 | fernando | */
|
127 | public void beginAtomicEvent() { |
||
128 | dispatching = false;
|
||
129 | } |
||
130 | |||
131 | /**
|
||
132 | 13316 | ppiqueras | * 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 | 13483 | ppiqueras | *
|
135 | * @see #beginAtomicEvent()
|
||
136 | 679 | fernando | */
|
137 | public void endAtomicEvent() { |
||
138 | fireAtomicEventListener(); |
||
139 | events.clear(); |
||
140 | dispatching = true;
|
||
141 | } |
||
142 | |||
143 | 13316 | ppiqueras | /*
|
144 | 679 | fernando | * @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 | 956 | vcaballero | |
149 | if (dispatching) {
|
||
150 | fireAtomicEventListener(); |
||
151 | } |
||
152 | 679 | fernando | } |
153 | |||
154 | 13316 | ppiqueras | /*
|
155 | 679 | fernando | * @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 | 956 | vcaballero | |
160 | if (dispatching) {
|
||
161 | fireAtomicEventListener(); |
||
162 | } |
||
163 | 679 | fernando | } |
164 | |||
165 | 13316 | ppiqueras | /*
|
166 | 679 | fernando | * @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 | 956 | vcaballero | |
171 | if (dispatching) {
|
||
172 | fireAtomicEventListener(); |
||
173 | } |
||
174 | 679 | fernando | } |
175 | |||
176 | 13316 | ppiqueras | /*
|
177 | 679 | fernando | * @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 | 956 | vcaballero | |
182 | if (dispatching) {
|
||
183 | fireAtomicEventListener(); |
||
184 | } |
||
185 | 679 | fernando | } |
186 | |||
187 | 13316 | ppiqueras | /*
|
188 | 679 | fernando | * @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 | 956 | vcaballero | |
193 | if (dispatching) {
|
||
194 | fireAtomicEventListener(); |
||
195 | } |
||
196 | 679 | fernando | } |
197 | |||
198 | 13316 | ppiqueras | /*
|
199 | 679 | fernando | * @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 | 956 | vcaballero | |
204 | if (dispatching) {
|
||
205 | fireAtomicEventListener(); |
||
206 | } |
||
207 | 679 | fernando | } |
208 | |||
209 | 13316 | ppiqueras | /*
|
210 | 679 | fernando | * @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 | 956 | vcaballero | |
216 | if (dispatching) {
|
||
217 | fireAtomicEventListener(); |
||
218 | } |
||
219 | 679 | fernando | } |
220 | |||
221 | 13316 | ppiqueras | /*
|
222 | 679 | fernando | * @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 | 956 | vcaballero | |
228 | if (dispatching) {
|
||
229 | fireAtomicEventListener(); |
||
230 | } |
||
231 | 679 | fernando | } |
232 | |||
233 | 13316 | ppiqueras | /*
|
234 | 679 | fernando | * @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 | 956 | vcaballero | |
239 | if (dispatching) {
|
||
240 | fireAtomicEventListener(); |
||
241 | } |
||
242 | 679 | fernando | } |
243 | |||
244 | 13316 | ppiqueras | /*
|
245 | * @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
|
||
246 | 679 | fernando | */
|
247 | public void extentChanged(ExtentEvent e) { |
||
248 | events.add(e); |
||
249 | 956 | vcaballero | |
250 | if (dispatching) {
|
||
251 | fireAtomicEventListener(); |
||
252 | } |
||
253 | 679 | fernando | } |
254 | |||
255 | /**
|
||
256 | 13316 | ppiqueras | * Appends, if it wasn't, the specified listener to the end of the internal list of atomic event listeners.
|
257 | 679 | fernando | *
|
258 | 13316 | ppiqueras | * @param listener an object that implements the atomic event listener
|
259 | 679 | fernando | *
|
260 | 13483 | ppiqueras | * @return <code>true</code> if has added the listener successfully
|
261 | *
|
||
262 | * @see #removeAtomicEventListener(AtomicEventListener)
|
||
263 | * @see #fireAtomicEventListener()
|
||
264 | 679 | fernando | */
|
265 | public boolean addAtomicEventListener(AtomicEventListener listener) { |
||
266 | 4147 | fjp | 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 | 679 | fernando | } |
274 | |||
275 | /**
|
||
276 | 13369 | ppiqueras | * <p>Removes a single instance of the {@link AtomicEventListener AtomicEventListener} from the
|
277 | 13316 | ppiqueras | * 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 | 13483 | ppiqueras | * @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 | 679 | fernando | */
|
287 | public boolean removeAtomicEventListener(AtomicEventListener listener) { |
||
288 | return listeners.remove(listener);
|
||
289 | } |
||
290 | |||
291 | /**
|
||
292 | 13369 | ppiqueras | * Executes the {@linkplain AtomicEventListener#atomicEvent(AtomicEvent)} method of all listeners registered.
|
293 | 13483 | ppiqueras | *
|
294 | * @see #addAtomicEventListener(AtomicEventListener)
|
||
295 | * @see #removeAtomicEventListener(AtomicEventListener)
|
||
296 | 679 | fernando | */
|
297 | private void fireAtomicEventListener() { |
||
298 | 4147 | fjp | if (events.size() == 0) |
299 | return; // No hay eventos que lanzar. |
||
300 | 679 | fernando | 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 | 956 | vcaballero | |
306 | 679 | fernando | events.clear(); |
307 | } |
||
308 | 727 | fernando | |
309 | 13316 | ppiqueras | /*
|
310 | 727 | fernando | * @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 | 956 | vcaballero | |
315 | if (dispatching) {
|
||
316 | fireAtomicEventListener(); |
||
317 | } |
||
318 | 727 | fernando | } |
319 | |||
320 | 13316 | ppiqueras | /*
|
321 | 727 | fernando | * @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 | 956 | vcaballero | |
326 | if (dispatching) {
|
||
327 | fireAtomicEventListener(); |
||
328 | } |
||
329 | 727 | fernando | } |
330 | |||
331 | 13316 | ppiqueras | /*
|
332 | 727 | fernando | * @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 | 956 | vcaballero | |
337 | if (dispatching) {
|
||
338 | fireAtomicEventListener(); |
||
339 | } |
||
340 | 727 | fernando | } |
341 | 956 | vcaballero | |
342 | 13316 | ppiqueras | /*
|
343 | 956 | vcaballero | * @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 | 979 | fernando | fireAtomicEventListener(); |
350 | 956 | vcaballero | } |
351 | } |
||
352 | 4144 | fjp | |
353 | 13316 | ppiqueras | /*
|
354 | * @see com.iver.cit.gvsig.fmap.layers.LayerListener#editionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
||
355 | */
|
||
356 | 4144 | fjp | public void editionChanged(LayerEvent e) { |
357 | events.add(e); |
||
358 | |||
359 | if (dispatching) {
|
||
360 | fireAtomicEventListener(); |
||
361 | } |
||
362 | |||
363 | } |
||
364 | 5939 | jmvivo | |
365 | 13316 | ppiqueras | /*
|
366 | * @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
|
||
367 | */
|
||
368 | 5939 | jmvivo | public void projectionChanged(ProjectionEvent e) { |
369 | events.add(e); |
||
370 | |||
371 | if (dispatching) {
|
||
372 | fireAtomicEventListener(); |
||
373 | } |
||
374 | } |
||
375 | 679 | fernando | } |