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
13   98   7   2.17
2   37   0.54   6
6     1.17  
1    
 
  WeakEventListener       Line # 35 13 0% 7 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Oct 8, 2007
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 @2007-2010 the original author or authors.
14    */
15    package org.fest.swing.listener;
16   
17    import java.awt.AWTEvent;
18    import java.awt.Toolkit;
19    import java.awt.event.AWTEventListener;
20    import java.lang.ref.WeakReference;
21   
22    import org.fest.util.VisibleForTesting;
23   
24    /**
25    * Understands an event listener that wraps a given <code>{@link AWTEventListener}</code> and:
26    * <ul>
27    * <li>attaches itself to the default toolkit</li>
28    * <li>dispatches any given event to the wrapped listener</li>
29    * <li>removes itself from the default toolkit when the wrapped listener gets garbage-collected</li>
30    * </ul>
31    *
32    * @author Alex Ruiz
33    * @author Yvonne Wang
34    */
 
35    public final class WeakEventListener implements AWTEventListener {
36   
37    private final WeakReference<AWTEventListener> listenerReference;
38    private final Toolkit toolkit;
39   
40    /**
41    * Creates a new <code>{@link WeakEventListener}</code> and adds it to the given <code>{@link Toolkit}</code> using
42    * the given event mask. The created <code>{@link WeakEventListener}</code> simply "decorates" the given
43    * <code>{@link AWTEventListener}</code>. All events dispatched to the <code>{@link WeakEventListener}</code> are
44    * re-routed to the underlying <code>{@link AWTEventListener}</code>. When the underlying
45    * <code>{@link AWTEventListener}</code> is garbage-collected, the <code>{@link WeakEventListener}</code> will remove
46    * itself from the toolkit.
47    * @param toolkit the given AWT <code>Toolkit</code>.
48    * @param listener the underlying listener to wrap.
49    * @param eventMask the event mask to use to attach the <code>WeakEventListener</code> to the toolkit.
50    * @return the created <code>WeakEventListener</code>.
51    */
 
52  3458 toggle public static WeakEventListener attachAsWeakEventListener(Toolkit toolkit, AWTEventListener listener, long eventMask) {
53  3458 WeakEventListener l = new WeakEventListener(toolkit, listener);
54  3458 toolkit.addAWTEventListener(l, eventMask);
55  3458 return l;
56    }
57   
 
58  3458 toggle private WeakEventListener(Toolkit toolkit, AWTEventListener listener) {
59  3458 listenerReference = new WeakReference<AWTEventListener>(listener);
60  3458 this.toolkit = toolkit;
61    }
62   
63    /**
64    * Returns the underlying listener.
65    * @return the underlying listener.
66    */
 
67  15 toggle public AWTEventListener underlyingListener() {
68  15 return listenerReference.get();
69    }
70   
71    /**
72    * Dispatches the given event to the wrapped event listener. If the wrapped listener is <code>null</code>
73    * (garbage-collected,) this listener will remove itself from the default toolkit.
74    * @param e the event dispatched in the AWT.
75    */
 
76  169622 toggle public void eventDispatched(AWTEvent e) {
77  169622 AWTEventListener listener = listenerReference.get();
78  169622 if (listener == null) {
79  165 dispose();
80  165 return;
81    }
82  169457 listener.eventDispatched(e);
83    }
84   
85    /** Removes itself from the <code>{@link Toolkit}</code> this listener is attached to. */
 
86  168 toggle public void dispose() {
87  168 toolkit.removeAWTEventListener(this);
88    }
89   
90    /**
91    * Removes the wrapped listener from the <code>{@link WeakReference}</code> (to simulate garbage collection). This
92    * method should be used only for <strong>testing only</strong>.
93    */
 
94  1 toggle @VisibleForTesting
95    void simulateUnderlyingListenerIsGarbageCollected() {
96  1 listenerReference.clear();
97    }
98    }