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
30   131   12   3
2   75   0.4   10
10     1.2  
1    
 
  Context       Line # 36 30 0% 12 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Oct 14, 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.monitor;
16   
17    import static org.fest.swing.awt.AWT.ownerLessWindows;
18    import static org.fest.swing.edt.GuiActionRunner.execute;
19    import static org.fest.util.Collections.list;
20   
21    import java.awt.*;
22    import java.util.*;
23   
24    import net.jcip.annotations.GuardedBy;
25    import net.jcip.annotations.ThreadSafe;
26   
27    import org.fest.swing.annotation.RunsInEDT;
28    import org.fest.swing.edt.GuiQuery;
29   
30    /**
31    * Understands a monitor that maps event queues to GUI components and GUI components to event event queues.
32    *
33    * @author Alex Ruiz
34    */
35    @ThreadSafe
 
36    class Context {
37   
38    /** Maps unique event queues to the set of root windows found on each queue. */
39    @GuardedBy("lock") private final WindowEventQueueMapping windowEventQueueMapping;
40   
41    /** Maps components to their corresponding event queues. */
42    @GuardedBy("lock") private final EventQueueMapping eventQueueMapping;
43   
44    private final Object lock = new Object();
45   
 
46  621 toggle Context(Toolkit toolkit) {
47  621 this(toolkit, new WindowEventQueueMapping(), new EventQueueMapping());
48    }
49   
 
50  627 toggle Context(Toolkit toolkit, WindowEventQueueMapping windowEventQueueMapping, EventQueueMapping eventQueueMapping) {
51  627 this.windowEventQueueMapping = windowEventQueueMapping;
52  627 this.eventQueueMapping = eventQueueMapping;
53  627 this.windowEventQueueMapping.addQueueFor(toolkit);
54    }
55   
56    /**
57    * Return all available root windows. A root window is one that has a <code>null</code> parent. Nominally this means
58    * a list similar to that returned by <code>{@link Frame#getFrames() Frame.getFrames()}</code>, but in the case of
59    * an <code>{@link java.applet.Applet}</code> may return a few dialogs as well.
60    * @return all available root windows.
61    */
 
62  18068 toggle Collection<Window> rootWindows() {
63  18068 Set<Window> rootWindows = new HashSet<Window>();
64  18068 synchronized (lock) {
65  18068 rootWindows.addAll(windowEventQueueMapping.windows());
66    }
67  18068 rootWindows.addAll(list(Frame.getFrames()));
68  18068 rootWindows.addAll(list(ownerLessWindows()));
69  18068 return rootWindows;
70    }
71   
 
72  14412 toggle EventQueue storedQueueFor(Component c) {
73  14412 synchronized (lock) {
74  14412 return eventQueueMapping.storedQueueFor(c);
75    }
76    }
77   
 
78  4516 toggle void removeContextFor(Component component) {
79  4516 synchronized (lock) {
80  4516 windowEventQueueMapping.removeMappingFor(component);
81    }
82    }
83   
 
84  3533 toggle void addContextFor(Component component) {
85  3533 synchronized (lock) {
86  3533 windowEventQueueMapping.addQueueFor(component);
87  3533 eventQueueMapping.addQueueFor(component);
88    }
89    }
90   
91    /**
92    * Return the event queue corresponding to the given component. In most cases, this is the same as
93    * <code>{@link java.awt.Toolkit#getSystemEventQueue()}</code>, but in the case of applets will bypass the
94    * <code>AppContext</code> and provide the real event queue.
95    * @param c the given component.
96    * @return the event queue corresponding to the given component
97    */
 
98  7 toggle @RunsInEDT
99    EventQueue eventQueueFor(Component c) {
100  7 Component component = topParentOf(c);
101  7 synchronized (lock) {
102  7 return eventQueueMapping.queueFor(component);
103    }
104    }
105   
 
106  7 toggle @RunsInEDT
107    private static Component topParentOf(final Component c) {
108  7 return execute(new GuiQuery<Component>() {
 
109  7 toggle protected Component executeInEDT() {
110  7 Component parent = c;
111    // Components above the applet in the hierarchy may or may not share the same context with the applet itself.
112  8 while (!(parent instanceof java.applet.Applet) && parent.getParent() != null)
113  1 parent = parent.getParent();
114  7 return parent;
115    }
116    });
117    }
118   
119    /**
120    * Returns all known event queues.
121    * @return all known event queues.
122    */
 
123  1851 toggle Collection<EventQueue> allEventQueues() {
124  1851 Set<EventQueue> eventQueues = new HashSet<EventQueue>();
125  1851 synchronized (lock) {
126  1851 eventQueues.addAll(windowEventQueueMapping.eventQueues());
127  1851 eventQueues.addAll(eventQueueMapping.eventQueues());
128    }
129  1851 return eventQueues;
130    }
131    }