1 /* 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 8 * 9 * 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 /** 18 * @author Mikhail Danilov 19 * @version $Revision$ 20 */ 21 package org.apache.harmony.awt.wtk; 22 23 import java.util.LinkedList; 24 25 26 /** 27 * Describes the cross-platform native event queue interface 28 * 29 * <p/> The implementation constructor should remember thread it was 30 * created. All other methods would be called obly from this thread, 31 * except awake(). 32 */ 33 public abstract class NativeEventQueue { 34 35 private ShutdownWatchdog shutdownWatchdog; 36 private class EventMonitor {} 37 private final Object eventMonitor = new EventMonitor(); 38 private final LinkedList<NativeEvent> eventQueue = new LinkedList<NativeEvent>(); 39 40 public static abstract class Task { 41 public volatile Object returnValue; 42 43 public abstract void perform(); 44 } 45 46 /** 47 * Blocks current thread until native event queue is not empty 48 * or awaken from other thread by awake(). 49 * 50 * <p/>Should be called only on tread which 51 * will process native events. 52 * 53 * @return if event loop should be stopped 54 */ 55 public abstract boolean waitEvent(); 56 57 /** 58 * Determines whether or not the native event queue is empty. 59 * An queue is empty if it contains no messages waiting. 60 * 61 * @return true if the queue is empty; false otherwise 62 */ 63 public boolean isEmpty() { 64 synchronized(eventQueue) { 65 return eventQueue.isEmpty(); 66 } 67 } 68 69 public NativeEvent getNextEvent() { 70 synchronized (eventQueue) { 71 if (eventQueue.isEmpty()) { 72 shutdownWatchdog.setNativeQueueEmpty(true); 73 return null; 74 } 75 return eventQueue.remove(0); 76 } 77 } 78 79 protected void addEvent(NativeEvent event) { 80 synchronized (eventQueue) { 81 eventQueue.add(event); 82 shutdownWatchdog.setNativeQueueEmpty(false); 83 } 84 synchronized (eventMonitor) { 85 eventMonitor.notify(); 86 } 87 } 88 89 public final Object getEventMonitor() { 90 return eventMonitor; 91 } 92 93 public abstract void awake(); 94 95 /** 96 * Gets AWT system window ID. 97 * 98 * @return AWT system window ID 99 */ 100 public abstract long getJavaWindow(); 101 102 /** 103 * Add NativeEvent to the queue 104 */ 105 public abstract void dispatchEvent(); 106 107 public abstract void performTask(Task task); 108 109 public abstract void performLater(Task task); 110 111 public final void setShutdownWatchdog(ShutdownWatchdog watchdog) { 112 synchronized (eventQueue) { 113 shutdownWatchdog = watchdog; 114 } 115 } 116 117 } 118