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
36   101   22   6
24   60   0.61   6
6     3.67  
1    
 
  ContextMonitor       Line # 37 36 0% 22 4 93.9% 0.93939394
 
No Tests
 
1    /*
2    * Created on Oct 13, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with 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
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.monitor;
17   
18    import static java.awt.AWTEvent.COMPONENT_EVENT_MASK;
19    import static java.awt.AWTEvent.WINDOW_EVENT_MASK;
20    import static java.awt.event.ComponentEvent.COMPONENT_SHOWN;
21    import static java.awt.event.WindowEvent.*;
22    import static org.fest.swing.listener.WeakEventListener.attachAsWeakEventListener;
23    import static org.fest.swing.query.ComponentParentQuery.parentOf;
24   
25    import java.applet.Applet;
26    import java.awt.*;
27    import java.awt.event.AWTEventListener;
28    import java.awt.event.ComponentEvent;
29   
30    import org.fest.swing.annotation.RunsInEDT;
31   
32    /**
33    * Understands a monitor for components and event queues.
34    *
35    * @author Alex Ruiz
36    */
 
37    final class ContextMonitor implements AWTEventListener {
38   
39    private static final long EVENT_MASK = WINDOW_EVENT_MASK | COMPONENT_EVENT_MASK;
40   
41    private final Context context;
42    private final Windows windows;
43   
 
44  704 toggle ContextMonitor(Context context, Windows windows) {
45  704 this.context = context;
46  704 this.windows = windows;
47    }
48   
 
49  634 toggle void attachTo(Toolkit toolkit) {
50  634 attachAsWeakEventListener(toolkit, this, EVENT_MASK);
51    }
52   
53    /** {@inheritDoc} */
 
54  37455 toggle @RunsInEDT
55    public void eventDispatched(AWTEvent e) {
56  37455 if (!(e instanceof ComponentEvent)) return;
57  37455 ComponentEvent event = (ComponentEvent) e;
58  37455 Component component = event.getComponent();
59    // This is our sole means of accessing other AppContexts (if running within an applet). We look for window events
60    // beyond OPENED in order to catch windows that have already opened by the time we start listening but which are not
61    // in the Frame.getFrames list (i.e. they are on a different context). Specifically watch for COMPONENT_SHOWN on
62    // applets, since we may not get frame events for them.
63  22981 if (!(component instanceof Applet) && !(component instanceof Window)) return;
64  14474 processEvent(event);
65    // The context for root-level windows may change between WINDOW_OPENED and subsequent events.
66  14474 if (!component.getToolkit().getSystemEventQueue().equals(context.storedQueueFor(component)))
67  2225 context.addContextFor(component);
68    }
69   
 
70  14474 toggle private void processEvent(ComponentEvent event) {
71  14474 Component component = event.getComponent();
72  14474 int id = event.getID();
73  14474 if (id == WINDOW_OPENED) {
74  908 recognizeAsOpenWindow(component);
75  908 return;
76    }
77  13566 if (id == WINDOW_CLOSED) {
78  5086 recognizeAsClosedWindow(component);
79  5086 return;
80    }
81  20 if (id == WINDOW_CLOSING) return;
82  8460 if ((id >= WINDOW_FIRST && id <= WINDOW_LAST) || id == COMPONENT_SHOWN)
83  412 if ((!context.rootWindows().contains(component)) || windows.isClosed(component)) recognizeAsOpenWindow(component);
84    }
85   
 
86  1320 toggle private void recognizeAsOpenWindow(Component component) {
87  1320 context.addContextFor(component);
88    // Attempt to ensure the window is ready for input before recognizing it as "open".
89    // There is no Java API for this, so we institute an empirically tested delay.
90  1320 if (!(component instanceof Window)) return;
91  1320 windows.attachNewWindowVisibilityMonitor((Window)component);
92  1320 windows.markAsShowing((Window) component);
93    // Native components don't receive events anyway...
94  7 if (component instanceof FileDialog) windows.markAsReady((Window) component);
95    }
96   
 
97  5086 toggle private void recognizeAsClosedWindow(Component component) {
98  4522 if (parentOf(component) == null) context.removeContextFor(component);
99  5079 if (component instanceof Window) windows.markAsClosed((Window)component);
100    }
101    }