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