2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Mikhail Danilov
21 package org.apache.harmony.awt.wtk;
23 import java.util.LinkedList;
27 * Describes the cross-platform native event queue interface
29 * <p/> The implementation constructor should remember thread it was
30 * created. All other methods would be called obly from this thread,
33 public abstract class NativeEventQueue {
35 private ShutdownWatchdog shutdownWatchdog;
36 private class EventMonitor {}
37 private final Object eventMonitor = new EventMonitor();
38 private final LinkedList<NativeEvent> eventQueue = new LinkedList<NativeEvent>();
40 public static abstract class Task {
41 public volatile Object returnValue;
43 public abstract void perform();
47 * Blocks current thread until native event queue is not empty
48 * or awaken from other thread by awake().
50 * <p/>Should be called only on tread which
51 * will process native events.
53 * @return if event loop should be stopped
55 public abstract boolean waitEvent();
58 * Determines whether or not the native event queue is empty.
59 * An queue is empty if it contains no messages waiting.
61 * @return true if the queue is empty; false otherwise
63 public boolean isEmpty() {
64 synchronized(eventQueue) {
65 return eventQueue.isEmpty();
69 public NativeEvent getNextEvent() {
70 synchronized (eventQueue) {
71 if (eventQueue.isEmpty()) {
72 shutdownWatchdog.setNativeQueueEmpty(true);
75 return eventQueue.remove(0);
79 protected void addEvent(NativeEvent event) {
80 synchronized (eventQueue) {
81 eventQueue.add(event);
82 shutdownWatchdog.setNativeQueueEmpty(false);
84 synchronized (eventMonitor) {
85 eventMonitor.notify();
89 public final Object getEventMonitor() {
93 public abstract void awake();
96 * Gets AWT system window ID.
98 * @return AWT system window ID
100 public abstract long getJavaWindow();
103 * Add NativeEvent to the queue
105 public abstract void dispatchEvent();
107 public abstract void performTask(Task task);
109 public abstract void performLater(Task task);
111 public final void setShutdownWatchdog(ShutdownWatchdog watchdog) {
112 synchronized (eventQueue) {
113 shutdownWatchdog = watchdog;