17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt; 27 28 import java.awt.event.InputEvent; 29 import java.awt.event.MouseEvent; 30 import java.awt.event.ActionEvent; 31 import java.awt.event.WindowEvent; 32 import java.lang.reflect.Method; 33 import java.security.AccessController; 34 import sun.security.action.GetPropertyAction; 35 import sun.awt.AWTAutoShutdown; 36 import sun.awt.SunToolkit; 37 38 import java.util.Vector; 39 import sun.util.logging.PlatformLogger; 40 41 import sun.awt.dnd.SunDragSourceContextPeer; 42 import sun.awt.EventQueueDelegate; 43 44 /** 45 * EventDispatchThread is a package-private AWT class which takes 46 * events off the EventQueue and dispatches them to the appropriate 47 * AWT components. 48 * 49 * The Thread starts a "permanent" event pump with a call to 50 * pumpEvents(Conditional) in its run() method. Event handlers can choose to 51 * block this event pump at any time, but should start a new pump (<b>not</b> 52 * a new EventDispatchThread) by again calling pumpEvents(Conditional). This 53 * secondary event pump will exit automatically as soon as the Condtional 54 * evaluate()s to false and an additional Event is pumped and dispatched. 55 * 56 * @author Tom Ball 57 * @author Amy Fowler 58 * @author Fred Ecks 59 * @author David Mendenhall 60 * 61 * @since 1.1 62 */ 63 class EventDispatchThread extends Thread { 64 65 private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread"); 66 67 private EventQueue theQueue; 68 private boolean doDispatch = true; 69 private boolean threadDeathCaught = false; 70 71 private static final int ANY_EVENT = -1; 72 73 private Vector<EventFilter> eventFilters = new Vector<EventFilter>(); 74 75 EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { 76 super(group, name); 77 setEventQueue(queue); 78 } 79 80 /* 81 * Must be called on EDT only, that's why no synchronization 82 */ 83 public void stopDispatching() { 84 doDispatch = false; 85 } 86 87 public void run() { 88 while (true) { 89 try { 90 pumpEvents(new Conditional() { 91 public boolean evaluate() { 92 return true; 93 } 94 }); 95 } finally { 96 EventQueue eq = getEventQueue(); 97 if (eq.detachDispatchThread(this) || threadDeathCaught) { 98 break; 99 } 100 } 101 } 102 } 103 104 void pumpEvents(Conditional cond) { 105 pumpEvents(ANY_EVENT, cond); 106 } 107 108 void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { 109 pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); 110 } 111 112 void pumpEvents(int id, Conditional cond) { 113 pumpEventsForHierarchy(id, cond, null); 114 } 115 116 void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) { 117 pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); 118 } 119 120 void pumpEventsForFilter(Conditional cond, EventFilter filter) { 121 pumpEventsForFilter(ANY_EVENT, cond, filter); 122 } 123 124 void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { 125 addEventFilter(filter); 126 doDispatch = true; 127 while (doDispatch && cond.evaluate()) { 128 if (isInterrupted() || !pumpOneEventForFilters(id)) { 129 doDispatch = false; 130 } 131 } 132 removeEventFilter(filter); 133 } 134 135 void addEventFilter(EventFilter filter) { 136 eventLog.finest("adding the event filter: " + filter); 137 synchronized (eventFilters) { 138 if (!eventFilters.contains(filter)) { 139 if (filter instanceof ModalEventFilter) { 140 ModalEventFilter newFilter = (ModalEventFilter)filter; 141 int k = 0; 142 for (k = 0; k < eventFilters.size(); k++) { 143 EventFilter f = eventFilters.get(k); 144 if (f instanceof ModalEventFilter) { 145 ModalEventFilter cf = (ModalEventFilter)f; 146 if (cf.compareTo(newFilter) > 0) { 147 break; 148 } 199 } 200 } 201 while (eventOK == false); 202 203 if (eventLog.isLoggable(PlatformLogger.FINEST)) { 204 eventLog.finest("Dispatching: " + event); 205 } 206 207 Object handle = null; 208 if (delegate != null) { 209 handle = delegate.beforeDispatch(event); 210 } 211 eq.dispatchEvent(event); 212 if (delegate != null) { 213 delegate.afterDispatch(event, handle); 214 } 215 216 return true; 217 } 218 catch (ThreadDeath death) { 219 threadDeathCaught = true; 220 return false; 221 222 } 223 catch (InterruptedException interruptedException) { 224 return false; // AppContext.dispose() interrupts all 225 // Threads in the AppContext 226 227 } 228 catch (Throwable e) { 229 processException(e); 230 } 231 232 return true; 233 } 234 235 private void processException(Throwable e) { 236 if (eventLog.isLoggable(PlatformLogger.FINE)) { 237 eventLog.fine("Processing exception: " + e); 238 } 239 getUncaughtExceptionHandler().uncaughtException(this, e); 240 } 241 242 public synchronized EventQueue getEventQueue() { 243 return theQueue; | 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt; 27 28 import java.awt.event.InputEvent; 29 import java.awt.event.MouseEvent; 30 import java.awt.event.ActionEvent; 31 import java.awt.event.WindowEvent; 32 import java.lang.reflect.Method; 33 import java.security.AccessController; 34 import sun.security.action.GetPropertyAction; 35 import sun.awt.AWTAutoShutdown; 36 import sun.awt.SunToolkit; 37 import sun.awt.AppContext; 38 39 import java.util.ArrayList; 40 import java.util.List; 41 import sun.util.logging.PlatformLogger; 42 43 import sun.awt.dnd.SunDragSourceContextPeer; 44 import sun.awt.EventQueueDelegate; 45 46 /** 47 * EventDispatchThread is a package-private AWT class which takes 48 * events off the EventQueue and dispatches them to the appropriate 49 * AWT components. 50 * 51 * The Thread starts a "permanent" event pump with a call to 52 * pumpEvents(Conditional) in its run() method. Event handlers can choose to 53 * block this event pump at any time, but should start a new pump (<b>not</b> 54 * a new EventDispatchThread) by again calling pumpEvents(Conditional). This 55 * secondary event pump will exit automatically as soon as the Condtional 56 * evaluate()s to false and an additional Event is pumped and dispatched. 57 * 58 * @author Tom Ball 59 * @author Amy Fowler 60 * @author Fred Ecks 61 * @author David Mendenhall 62 * 63 * @since 1.1 64 */ 65 class EventDispatchThread extends Thread { 66 67 private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread"); 68 69 private EventQueue theQueue; 70 private boolean doDispatch = true; 71 private volatile boolean shutdown = false; 72 73 private static final int ANY_EVENT = -1; 74 75 private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>(); 76 77 EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { 78 super(group, name); 79 setEventQueue(queue); 80 } 81 82 /* 83 * Must be called on EDT only, that's why no synchronization 84 */ 85 public void stopDispatching() { 86 doDispatch = false; 87 } 88 89 public void interrupt() { 90 shutdown = true; 91 super.interrupt(); 92 } 93 94 public void run() { 95 while (true) { 96 try { 97 pumpEvents(new Conditional() { 98 public boolean evaluate() { 99 return true; 100 } 101 }); 102 } finally { 103 if(getEventQueue().detachDispatchThread(this, shutdown)) { 104 break; 105 } 106 } 107 } 108 } 109 110 void pumpEvents(Conditional cond) { 111 pumpEvents(ANY_EVENT, cond); 112 } 113 114 void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { 115 pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); 116 } 117 118 void pumpEvents(int id, Conditional cond) { 119 pumpEventsForHierarchy(id, cond, null); 120 } 121 122 void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) { 123 pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); 124 } 125 126 void pumpEventsForFilter(Conditional cond, EventFilter filter) { 127 pumpEventsForFilter(ANY_EVENT, cond, filter); 128 } 129 130 void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { 131 addEventFilter(filter); 132 doDispatch = !shutdown; 133 while (doDispatch && cond.evaluate()) { 134 if (shutdown || !pumpOneEventForFilters(id)) { 135 doDispatch = false; 136 } 137 } 138 removeEventFilter(filter); 139 } 140 141 void addEventFilter(EventFilter filter) { 142 eventLog.finest("adding the event filter: " + filter); 143 synchronized (eventFilters) { 144 if (!eventFilters.contains(filter)) { 145 if (filter instanceof ModalEventFilter) { 146 ModalEventFilter newFilter = (ModalEventFilter)filter; 147 int k = 0; 148 for (k = 0; k < eventFilters.size(); k++) { 149 EventFilter f = eventFilters.get(k); 150 if (f instanceof ModalEventFilter) { 151 ModalEventFilter cf = (ModalEventFilter)f; 152 if (cf.compareTo(newFilter) > 0) { 153 break; 154 } 205 } 206 } 207 while (eventOK == false); 208 209 if (eventLog.isLoggable(PlatformLogger.FINEST)) { 210 eventLog.finest("Dispatching: " + event); 211 } 212 213 Object handle = null; 214 if (delegate != null) { 215 handle = delegate.beforeDispatch(event); 216 } 217 eq.dispatchEvent(event); 218 if (delegate != null) { 219 delegate.afterDispatch(event, handle); 220 } 221 222 return true; 223 } 224 catch (ThreadDeath death) { 225 shutdown = true; 226 throw death; 227 } 228 catch (InterruptedException interruptedException) { 229 shutdown = true; 230 return false; // AppContext.dispose() interrupts all 231 // Threads in the AppContext 232 233 } 234 catch (Throwable e) { 235 processException(e); 236 } 237 238 return true; 239 } 240 241 private void processException(Throwable e) { 242 if (eventLog.isLoggable(PlatformLogger.FINE)) { 243 eventLog.fine("Processing exception: " + e); 244 } 245 getUncaughtExceptionHandler().uncaughtException(this, e); 246 } 247 248 public synchronized EventQueue getEventQueue() { 249 return theQueue; |