01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package java.awt;
18:
19: final class HeadlessEventDispatchThread extends EventDispatchThread {
20:
21: HeadlessEventDispatchThread(Toolkit toolkit, Dispatcher dispatcher) {
22: super (toolkit, dispatcher);
23: }
24:
25: /**
26: * Initialise and run the main event loop
27: */
28: @Override
29: public void run() {
30: try {
31: runModalLoop(null);
32: } finally {
33: toolkit.shutdownWatchdog.forceShutdown();
34: }
35: }
36:
37: void runModalLoop(ModalContext context) {
38: long lastPaintTime = System.currentTimeMillis();
39: while (!shutdownPending
40: && (context == null || context.isModalLoopRunning())) {
41: try {
42: EventQueue eventQueue = toolkit
43: .getSystemEventQueueImpl();
44:
45: toolkit.shutdownWatchdog.setNativeQueueEmpty(true);
46: AWTEvent ae = eventQueue.getNextEventNoWait();
47: if (ae != null) {
48: eventQueue.dispatchEvent(ae);
49: long curTime = System.currentTimeMillis();
50: if (curTime - lastPaintTime > 10) {
51: toolkit.onQueueEmpty();
52: lastPaintTime = System.currentTimeMillis();
53: }
54: } else {
55: toolkit.shutdownWatchdog.setAwtQueueEmpty(true);
56: toolkit.onQueueEmpty();
57: lastPaintTime = System.currentTimeMillis();
58: waitForAnyEvent();
59: }
60: } catch (Throwable t) {
61: t.printStackTrace();
62: }
63: }
64: }
65:
66: private void waitForAnyEvent() {
67: EventQueue eventQueue = toolkit.getSystemEventQueueImpl();
68: if (!eventQueue.isEmpty()) {
69: return;
70: }
71:
72: Object eventMonitor = toolkit.getEventMonitor();
73: synchronized (eventMonitor) {
74: try {
75: eventMonitor.wait();
76: } catch (InterruptedException e) {
77: }
78: }
79: }
80:
81: }
|