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 java.awt.event.InvocationEvent;
023: import java.lang.reflect.InvocationTargetException;
024: import java.util.EmptyStackException;
025:
026: public class EventQueue {
027:
028: private final EventQueueCoreAtomicReference coreRef = new EventQueueCoreAtomicReference();
029:
030: private static final class EventQueueCoreAtomicReference {
031: private EventQueueCore core;
032:
033: /*synchronized*/EventQueueCore get() {
034: return core;
035: }
036:
037: /*synchronized*/void set(EventQueueCore newCore) {
038: core = newCore;
039: }
040: }
041:
042: public static boolean isDispatchThread() {
043: return Thread.currentThread() instanceof EventDispatchThread;
044: }
045:
046: public static void invokeLater(Runnable runnable) {
047: Toolkit toolkit = Toolkit.getDefaultToolkit();
048: InvocationEvent event = new InvocationEvent(toolkit, runnable);
049: toolkit.getSystemEventQueueImpl().postEvent(event);
050: }
051:
052: public static void invokeAndWait(Runnable runnable)
053: throws InterruptedException, InvocationTargetException {
054:
055: if (isDispatchThread()) {
056: throw new Error();
057: }
058:
059: final Toolkit toolkit = Toolkit.getDefaultToolkit();
060: final Object notifier = new Object(); //$NON-LOCK-1$
061: InvocationEvent event = new InvocationEvent(toolkit, runnable,
062: notifier, true);
063:
064: synchronized (notifier) {
065: toolkit.getSystemEventQueueImpl().postEvent(event);
066: notifier.wait();
067: }
068:
069: Exception exception = event.getException();
070:
071: if (exception != null) {
072: throw new InvocationTargetException(exception);
073: }
074: }
075:
076: private static EventQueue getSystemEventQueue() {
077: Thread th = Thread.currentThread();
078: if (th instanceof EventDispatchThread) {
079: return ((EventDispatchThread) th).toolkit
080: .getSystemEventQueueImpl();
081: }
082: return null;
083: }
084:
085: public static long getMostRecentEventTime() {
086: EventQueue eq = getSystemEventQueue();
087: return (eq != null) ? eq.getMostRecentEventTimeImpl() : System
088: .currentTimeMillis();
089: }
090:
091: private long getMostRecentEventTimeImpl() {
092: return getCore().getMostRecentEventTime();
093: }
094:
095: public static AWTEvent getCurrentEvent() {
096: EventQueue eq = getSystemEventQueue();
097: return (eq != null) ? eq.getCurrentEventImpl() : null;
098: }
099:
100: private AWTEvent getCurrentEventImpl() {
101: return getCore().getCurrentEvent();
102: }
103:
104: public EventQueue() {
105: setCore(new EventQueueCore(this ));
106: }
107:
108: EventQueue(Toolkit t) {
109: setCore(new EventQueueCore(this , t));
110: }
111:
112: public void postEvent(AWTEvent event) {
113: event.isPosted = true;
114: getCore().postEvent(event);
115: }
116:
117: public AWTEvent getNextEvent() throws InterruptedException {
118: return getCore().getNextEvent();
119: }
120:
121: AWTEvent getNextEventNoWait() {
122: return getCore().getNextEventNoWait();
123: }
124:
125: public AWTEvent peekEvent() {
126: return getCore().peekEvent();
127: }
128:
129: public AWTEvent peekEvent(int id) {
130: return getCore().peekEvent(id);
131: }
132:
133: public void push(EventQueue newEventQueue) {
134: getCore().push(newEventQueue);
135: }
136:
137: protected void pop() throws EmptyStackException {
138: getCore().pop();
139: }
140:
141: protected void dispatchEvent(AWTEvent event) {
142: getCore().dispatchEventImpl(event);
143: }
144:
145: boolean isEmpty() {
146: return getCore().isEmpty();
147: }
148:
149: EventQueueCore getCore() {
150: return coreRef.get();
151: }
152:
153: void setCore(EventQueueCore newCore) {
154: coreRef.set((newCore != null) ? newCore : new EventQueueCore(
155: this));
156: }
157: }
|