Statistics
| Revision:

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

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 java.util.ArrayList;
44
import java.util.Iterator;
45

    
46
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
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
50
import com.iver.cit.gvsig.fmap.layers.LayerListener;
51
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
52
import com.iver.cit.gvsig.fmap.layers.LegendChangedEvent;
53
import com.iver.cit.gvsig.fmap.layers.SelectionEvent;
54
import com.iver.cit.gvsig.fmap.layers.SelectionListener;
55
import com.iver.cit.gvsig.fmap.rendering.LegendListener;
56

    
57

    
58
/**
59
 * <p>The class <code>EventBuffer</code> represents a buffer of events that allows store 
60
 * listeners for the events, and 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,
66
 *   and only will dispatch them (according to the order they were received) when 
67
 *   changes the mode.
68
 * </ul>
69
 * </p>
70
 *
71
 * @see LegendListener
72
 * @see LayerCollectionListener
73
 * @see SelectionListener
74
 * @see ViewPortListener
75
 * @see LegendListener
76
 *
77
 * @author Fernando Gonz?lez Cort?s
78
 */
79
public class EventBuffer implements LegendListener, LayerCollectionListener,
80
SelectionListener, ViewPortListener, LayerListener {
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
         * List with all listeners registered.
106
         * 
107
         * @see #addAtomicEventListener(AtomicEventListener)
108
         * @see #removeAtomicEventListener(AtomicEventListener)
109
         */ 
110
        private ArrayList listeners = new ArrayList();
111
        /**
112
         * Allows enable or disable the <i>dispatching mode</i>.
113
         * 
114
         * @see #beginAtomicEvent()
115
         * @see #endAtomicEvent()
116
         */
117
        private boolean dispatching = true;
118
        /**
119
         * Enables buffer in <i>accumulation event</i> mode. And then, all new events 
120
         * received, will be accumulated and don't notified to their respective listeners,
121
         * until this buffer receive a call to {@link #endAtomicEvent() endAtomicEvent}.
122
         * 
123
         * @see #endAtomicEvent()
124
         */
125

    
126
        public void beginAtomicEvent() {
127
                dispatching = false;
128
        }
129

    
130
        /**
131
         * Disables buffer in <i>accumulation event</i> mode. And then, all events 
132
         * accumulated will be notify to their respective listeners, in the same order as they arrived.
133
         *  
134
         * @see #beginAtomicEvent()
135
         */
136
        public void endAtomicEvent() {
137
                fireAtomicEventListener();
138
                events.clear();
139
                dispatching = true;
140
        }
141

    
142
        /*
143
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
144
         */
145
        public void legendChanged(LegendChangedEvent e) {
146
                events.add(e);
147

    
148
                if (dispatching) {
149
                        fireAtomicEventListener();
150
                }
151
        }
152

    
153
        /*
154
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
155
         */
156
        public void layerAdded(LayerCollectionEvent e) {
157
                events.add(e);
158

    
159
                if (dispatching) {
160
                        fireAtomicEventListener();
161
                }
162
        }
163

    
164
        /*
165
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
166
         */
167
        public void layerMoved(LayerPositionEvent e) {
168
                events.add(e);
169

    
170
                if (dispatching) {
171
                        fireAtomicEventListener();
172
                }
173
        }
174

    
175
        /*
176
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
177
         */
178
        public void layerRemoved(LayerCollectionEvent e) {
179
                events.add(e);
180

    
181
                if (dispatching) {
182
                        fireAtomicEventListener();
183
                }
184
        }
185

    
186
        /*
187
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
188
         */
189
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
190
                events.add(e);
191

    
192
                if (dispatching) {
193
                        fireAtomicEventListener();
194
                }
195
        }
196

    
197
        /*
198
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
199
         */
200
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
201
                events.add(e);
202

    
203
                if (dispatching) {
204
                        fireAtomicEventListener();
205
                }
206
        }
207

    
208
        /*
209
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
210
         */
211
        public void layerRemoving(LayerCollectionEvent e)
212
        throws CancelationException {
213
                events.add(e);
214

    
215
                if (dispatching) {
216
                        fireAtomicEventListener();
217
                }
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
257
         *  of atomic event listeners.
258
         *
259
         * @param listener an object that implements the atomic event listener
260
         *
261
         * @return <code>true</code> if has added the listener successfully
262
         * 
263
         * @see #removeAtomicEventListener(AtomicEventListener)
264
         * @see #fireAtomicEventListener()
265
         */
266
        public boolean addAtomicEventListener(AtomicEventListener listener) {
267
                boolean bFound = false;
268
                for (int i=0; i < listeners.size(); i++)
269
                        if (listeners.get(i) == listener)
270
                                bFound = true;
271
                if (!bFound)
272
                        listeners.add(listener);
273
                return true; 
274
        }
275

    
276
        /**
277
         * <p>Removes a single instance of the {@link AtomicEventListener AtomicEventListener}
278
         * from the internal list, if it is present (optional operation). Returns <tt>true</tt>
279
         * if the list contained the specified element (or equivalently, if the list changed
280
         * as a result of the call).<p>
281
         *
282
         * @param o element to be removed from this list, if present
283
         * @return <tt>true</tt> if the list contained the specified element
284
         * 
285
         * @see #addAtomicEventListener(AtomicEventListener)
286
         * @see #fireAtomicEventListener()
287
         */
288
        public boolean removeAtomicEventListener(AtomicEventListener listener) {
289
                return listeners.remove(listener);
290
        }
291

    
292
        /**
293
         * Executes the {@linkplain AtomicEventListener#atomicEvent(AtomicEvent)} method 
294
         * of all listeners registered.
295
         * 
296
         * @see #addAtomicEventListener(AtomicEventListener)
297
         * @see #removeAtomicEventListener(AtomicEventListener)
298
         */
299
        private void fireAtomicEventListener() {
300
                if (events.size() == 0)
301
                        return; // No hay eventos que lanzar.
302
                for (Iterator i = listeners.iterator(); i.hasNext();) {
303
                        AtomicEventListener listener = (AtomicEventListener) i.next();
304
                        AtomicEvent e = new AtomicEvent(events);
305
                        listener.atomicEvent(e);
306
                }
307

    
308
                events.clear();
309
        }
310
        /*
311
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
312
         */
313
        public void visibilityChanged(LayerEvent e) {
314
                events.add(e);
315

    
316
                if (dispatching) {
317
                        fireAtomicEventListener();
318
                }
319
        }
320

    
321
        /*
322
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
323
         */
324
        public void activationChanged(LayerEvent e) {
325
                events.add(e);
326

    
327
                if (dispatching) {
328
                        fireAtomicEventListener();
329
                }
330
        }
331

    
332
        /*
333
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#nameChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
334
         */
335
        public void nameChanged(LayerEvent e) {
336
                events.add(e);
337

    
338
                if (dispatching) {
339
                        fireAtomicEventListener();
340
                }
341
        }
342

    
343
        /*
344
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
345
         */
346
        public void backColorChanged(ColorEvent e) {
347
                events.add(e);
348

    
349
                if (dispatching) {
350
                        fireAtomicEventListener();
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
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#projectionChanged(com.iver.cit.gvsig.fmap.ProjectionEvent)
366
         */
367
        public void projectionChanged(ProjectionEvent e) {
368
                events.add(e);
369

    
370
                if (dispatching) {
371
                        fireAtomicEventListener();
372
                }
373
        }
374
}