Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
16   99   5   5.33
4   43   0.31   3
3     1.67  
1    
 
  EventDispatchThreadedEventListener       Line # 41 16 0% 5 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Mar 27, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11    * specific language governing permissions and limitations under the License.
12    *
13    * Copyright @2008-2010 the original author or authors.
14    */
15    package org.fest.swing.listener;
16   
17    import static javax.swing.SwingUtilities.invokeLater;
18    import static javax.swing.SwingUtilities.isEventDispatchThread;
19   
20    import java.awt.AWTEvent;
21    import java.awt.event.AWTEventListener;
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import net.jcip.annotations.GuardedBy;
26    import net.jcip.annotations.ThreadSafe;
27   
28    /**
29    * Understands a <code>{@link AWTEventListener}</code> that ensures all events are handled on the event dispatch
30    * thread.
31    * <p>
32    * NOTE from Abbot: Applet runners may run several simultaneous event dispatch threads when displaying multiple applets
33    * simultaneously. If this listener is installed in the parent context of those dispatch threads, it will be invoked on
34    * each of those threads, possibly simultaneously.
35    * </p>
36    *
37    * @author Yvonne Wang
38    * @author Alex Ruiz
39    */
40    @ThreadSafe
 
41    public abstract class EventDispatchThreadedEventListener implements AWTEventListener {
42   
43    @GuardedBy("lock") private final List<AWTEvent> deferredEvents = new ArrayList<AWTEvent>();
44    private final Object lock = new Object();
45   
46    private final Runnable processDeferredEventsTask = new Runnable() {
 
47  1 toggle public void run() {
48  1 processDeferredEvents();
49    }
50    };
51   
52    /**
53    * If this method is called in the event dispatch thread, it processes the given event and the queued ones. Otherwise
54    * it will add the given event to the queue and process all the events in the queue in the event dispatch thread.
55    * @param event the event to process.
56    */
 
57  5207 toggle public void eventDispatched(AWTEvent event) {
58  5207 if (!isEventDispatchThread()) {
59    // Often the application under test will invoke Window.show, which spawns hierarchy events. We want to ensure we
60    // respond to those events on the dispatch thread to avoid deadlock.
61  1 synchronized (lock) {
62  1 deferredEvents.add(event);
63    }
64    // Ensure that in the absence of any subsequent event thread events deferred events still get processed.
65    // If regular events are received before this action is run, the deferred events will be processed prior to those
66    // events and the action will do nothing.
67  1 invokeLater(processDeferredEventsTask);
68  1 return;
69    }
70    // Ensure any deferred events are processed prior to subsequently posted events.
71  5206 processDeferredEvents();
72  5206 processEvent(event);
73    }
74   
75    /** Processes any events that were generated off the event queue but not immediately handled. */
 
76  5207 toggle protected void processDeferredEvents() {
77    // Make a copy of the deferred events and empty the queue
78  5207 List<AWTEvent> queue = new ArrayList<AWTEvent>();
79  5207 synchronized (lock) {
80    // In the rare case where there are multiple simultaneous dispatch threads, it's possible for deferred events to
81    // get posted while another event is being processed. At most this will mean a few events get processed out of
82    // order, but they will likely be from different event dispatch contexts, so it shouldn't matter.
83  5207 queue.addAll(deferredEvents);
84  5207 deferredEvents.clear();
85    }
86  5208 while (queue.size() > 0) {
87  1 AWTEvent event = queue.get(0);
88  1 queue.remove(0);
89  1 processEvent(event);
90    }
91    }
92   
93    /**
94    * This method is not protected by any synchronization locks (nor should it be); in the presence of multiple
95    * simultaneous event dispatch threads, the listener must be thread-safe.
96    * @param event the event to process.
97    */
98    protected abstract void processEvent(AWTEvent event);
99    }