Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / EventBuffer.java @ 27619

History | View | Annotate | Download (10.6 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 10627 caballero
import java.util.ArrayList;
44
import java.util.Iterator;
45
46 679 fernando
import com.iver.cit.gvsig.fmap.layers.CancelationException;
47
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
48
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
49 727 fernando
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
50
import com.iver.cit.gvsig.fmap.layers.LayerListener;
51 679 fernando
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
52 18621 jdominguez
import com.iver.cit.gvsig.fmap.layers.LegendChangedEvent;
53 679 fernando
import com.iver.cit.gvsig.fmap.layers.SelectionEvent;
54
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
55 18621 jdominguez
import com.iver.cit.gvsig.fmap.rendering.LegendListener;
56 679 fernando
57
58
/**
59 20098 jmvivo
 * <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 14261 jvidal
 *
62 20098 jmvivo
 * <p>The <i>dispatching mode</i>:
63 14261 jvidal
 * <ul>
64 20098 jmvivo
 *  <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 14261 jvidal
 * </ul>
68
 * </p>
69 679 fernando
 *
70 14261 jvidal
 * @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 14261 jvidal
SelectionListener, ViewPortListener, LayerListener {
80 20098 jmvivo
81 14261 jvidal
        /**
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 20098 jmvivo
         */
103 679 fernando
        private ArrayList events = new ArrayList();
104 20098 jmvivo
105 14261 jvidal
        /**
106
         * List with all listeners registered.
107
         *
108
         * @see #addAtomicEventListener(AtomicEventListener)
109
         * @see #removeAtomicEventListener(AtomicEventListener)
110
         */
111 679 fernando
        private ArrayList listeners = new ArrayList();
112 20098 jmvivo
113 14261 jvidal
        /**
114
         * Allows enable or disable the <i>dispatching mode</i>.
115
         *
116
         * @see #beginAtomicEvent()
117
         * @see #endAtomicEvent()
118
         */
119 679 fernando
        private boolean dispatching = true;
120 20098 jmvivo
121 679 fernando
        /**
122 20098 jmvivo
         * <p>Enables buffer in <i>accumulation event</i> mode.<p>
123 14261 jvidal
         *
124 20098 jmvivo
         * <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 14261 jvidal
         * @see #endAtomicEvent()
128 679 fernando
         */
129
        public void beginAtomicEvent() {
130
                dispatching = false;
131
        }
132
133
        /**
134 20098 jmvivo
         * <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 14261 jvidal
         *
139
         * @see #beginAtomicEvent()
140 679 fernando
         */
141
        public void endAtomicEvent() {
142
                fireAtomicEventListener();
143
                events.clear();
144
                dispatching = true;
145
        }
146
147 14261 jvidal
        /*
148 679 fernando
         * @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 956 vcaballero
153
                if (dispatching) {
154
                        fireAtomicEventListener();
155
                }
156 679 fernando
        }
157
158 14261 jvidal
        /*
159 679 fernando
         * @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 956 vcaballero
164
                if (dispatching) {
165
                        fireAtomicEventListener();
166
                }
167 679 fernando
        }
168
169 14261 jvidal
        /*
170 679 fernando
         * @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 956 vcaballero
175
                if (dispatching) {
176
                        fireAtomicEventListener();
177
                }
178 679 fernando
        }
179
180 14261 jvidal
        /*
181 679 fernando
         * @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 956 vcaballero
186
                if (dispatching) {
187
                        fireAtomicEventListener();
188
                }
189 679 fernando
        }
190
191 14261 jvidal
        /*
192 679 fernando
         * @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 956 vcaballero
197
                if (dispatching) {
198
                        fireAtomicEventListener();
199
                }
200 679 fernando
        }
201
202 14261 jvidal
        /*
203 679 fernando
         * @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 956 vcaballero
208
                if (dispatching) {
209
                        fireAtomicEventListener();
210
                }
211 679 fernando
        }
212
213 14261 jvidal
        /*
214 679 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
215
         */
216
        public void layerRemoving(LayerCollectionEvent e)
217 14261 jvidal
        throws CancelationException {
218 679 fernando
                events.add(e);
219 956 vcaballero
220
                if (dispatching) {
221
                        fireAtomicEventListener();
222
                }
223 679 fernando
        }
224
225 14261 jvidal
        /*
226 679 fernando
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
227
         */
228
        public void visibilityChanged(LayerCollectionEvent e)
229 14261 jvidal
        throws CancelationException {
230 679 fernando
                events.add(e);
231 956 vcaballero
232
                if (dispatching) {
233
                        fireAtomicEventListener();
234
                }
235 679 fernando
        }
236
237 14261 jvidal
        /*
238 679 fernando
         * @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 956 vcaballero
243
                if (dispatching) {
244
                        fireAtomicEventListener();
245
                }
246 679 fernando
        }
247
248 14261 jvidal
        /*
249
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
250 679 fernando
         */
251
        public void extentChanged(ExtentEvent e) {
252
                events.add(e);
253 956 vcaballero
254
                if (dispatching) {
255
                        fireAtomicEventListener();
256
                }
257 679 fernando
        }
258
259
        /**
260 20098 jmvivo
         * Appends, if wasn't, the specified listener to the end of the internal list of atomic event listeners.
261 679 fernando
         *
262 14261 jvidal
         * @param listener an object that implements the atomic event listener
263 679 fernando
         *
264 20098 jmvivo
         * @return <code>true</code> if has added the listener successfully; otherwise <code>false</code>
265 14261 jvidal
         *
266
         * @see #removeAtomicEventListener(AtomicEventListener)
267
         * @see #fireAtomicEventListener()
268 679 fernando
         */
269
        public boolean addAtomicEventListener(AtomicEventListener listener) {
270 4147 fjp
                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 679 fernando
        }
278
279
        /**
280 20098 jmvivo
         * <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 679 fernando
         */
291
        public boolean removeAtomicEventListener(AtomicEventListener listener) {
292
                return listeners.remove(listener);
293
        }
294
295
        /**
296 20098 jmvivo
         * Executes the {@linkplain AtomicEventListener#atomicEvent(AtomicEvent)} method of all listeners registered.
297 14261 jvidal
         *
298
         * @see #addAtomicEventListener(AtomicEventListener)
299
         * @see #removeAtomicEventListener(AtomicEventListener)
300 679 fernando
         */
301
        private void fireAtomicEventListener() {
302 4147 fjp
                if (events.size() == 0)
303
                        return; // No hay eventos que lanzar.
304 679 fernando
                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 956 vcaballero
310 679 fernando
                events.clear();
311
        }
312 20098 jmvivo
313 14261 jvidal
        /*
314 727 fernando
         * @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 956 vcaballero
319
                if (dispatching) {
320
                        fireAtomicEventListener();
321
                }
322 727 fernando
        }
323
324 14261 jvidal
        /*
325 727 fernando
         * @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 956 vcaballero
330
                if (dispatching) {
331
                        fireAtomicEventListener();
332
                }
333 727 fernando
        }
334
335 14261 jvidal
        /*
336 727 fernando
         * @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 956 vcaballero
341
                if (dispatching) {
342
                        fireAtomicEventListener();
343
                }
344 727 fernando
        }
345 956 vcaballero
346 14261 jvidal
        /*
347 956 vcaballero
         * @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 979 fernando
                        fireAtomicEventListener();
354 956 vcaballero
                }
355
        }
356 20098 jmvivo
357 14261 jvidal
        /*
358
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#editionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
359
         */
360 4144 fjp
        public void editionChanged(LayerEvent e) {
361
                events.add(e);
362
363
                if (dispatching) {
364
                        fireAtomicEventListener();
365
                }
366 14261 jvidal
367 4144 fjp
        }
368 20098 jmvivo
369 14261 jvidal
        /*
370
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
371
         */
372 5939 jmvivo
        public void projectionChanged(ProjectionEvent e) {
373
                events.add(e);
374
375
                if (dispatching) {
376
                        fireAtomicEventListener();
377
                }
378
        }
379 24160 jmvivo
380
        public void drawValueChanged(LayerEvent e) {
381
                events.add(e);
382
383
                if (dispatching) {
384
                        fireAtomicEventListener();
385
                }
386
        }
387 679 fernando
}