001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov, Pavel Dolgov
019: * @version $Revision$
020: */package java.awt;
021:
022: import org.apache.harmony.awt.wtk.NativeEvent;
023: import org.apache.harmony.awt.wtk.NativeEventQueue;
024:
025: class EventDispatchThread extends Thread {
026:
027: private static final class MarkerEvent extends AWTEvent {
028: MarkerEvent(Object source, int id) {
029: super (source, id);
030: }
031: }
032:
033: final Dispatcher dispatcher;
034: final Toolkit toolkit;
035: private NativeEventQueue nativeQueue;
036:
037: protected volatile boolean shutdownPending = false;
038:
039: /**
040: * Initialise and run the main event loop
041: */
042: @Override
043: public void run() {
044: nativeQueue = toolkit.getNativeEventQueue();
045:
046: try {
047: runModalLoop(null);
048: } finally {
049: toolkit.shutdownWatchdog.forceShutdown();
050: }
051: }
052:
053: void runModalLoop(ModalContext context) {
054: long lastPaintTime = System.currentTimeMillis();
055: while (!shutdownPending
056: && (context == null || context.isModalLoopRunning())) {
057: try {
058: EventQueue eventQueue = toolkit
059: .getSystemEventQueueImpl();
060:
061: NativeEvent ne = nativeQueue.getNextEvent();
062: if (ne != null) {
063: dispatcher.onEvent(ne);
064: MarkerEvent marker = new MarkerEvent(this , 0);
065: eventQueue.postEvent(marker);
066: for (AWTEvent ae = eventQueue.getNextEventNoWait(); (ae != null)
067: && (ae != marker); ae = eventQueue
068: .getNextEventNoWait()) {
069: eventQueue.dispatchEvent(ae);
070: }
071: } else {
072: toolkit.shutdownWatchdog.setNativeQueueEmpty(true);
073: AWTEvent ae = eventQueue.getNextEventNoWait();
074: if (ae != null) {
075: eventQueue.dispatchEvent(ae);
076: long curTime = System.currentTimeMillis();
077: if (curTime - lastPaintTime > 10) {
078: toolkit.onQueueEmpty();
079: lastPaintTime = System.currentTimeMillis();
080: }
081: } else {
082: toolkit.shutdownWatchdog.setAwtQueueEmpty(true);
083: toolkit.onQueueEmpty();
084: lastPaintTime = System.currentTimeMillis();
085: waitForAnyEvent();
086: }
087: }
088: } catch (Throwable t) {
089: // TODO: Exception handler should be implemented
090: t.printStackTrace();
091: }
092: }
093: }
094:
095: private void waitForAnyEvent() {
096: EventQueue eventQueue = toolkit.getSystemEventQueueImpl();
097: if (!eventQueue.isEmpty() || !nativeQueue.isEmpty()) {
098: return;
099: }
100: Object eventMonitor = toolkit.getEventMonitor();
101: synchronized (eventMonitor) {
102: try {
103: eventMonitor.wait();
104: } catch (InterruptedException e) {
105: }
106: }
107: }
108:
109: void shutdown() {
110: shutdownPending = true;
111: }
112:
113: EventDispatchThread(Toolkit toolkit, Dispatcher dispatcher) {
114: this .toolkit = toolkit;
115: this .dispatcher = dispatcher;
116: setName("AWT-EventDispatchThread"); //$NON-NLS-1$
117: setDaemon(true);
118: }
119:
120: }
|