eventqueue.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 552 行 · 第 1/2 页

JAVA
552
字号
/* EventQueue.java --   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005  Free Software FoundationThis file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import gnu.java.awt.ClasspathToolkit;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.InvocationEvent;import java.awt.event.WindowEvent;import java.lang.reflect.InvocationTargetException;import java.util.EmptyStackException;/* Written using on-line Java 2 Platform Standard Edition v1.3 API  * Specification, as well as "The Java Class Libraries", 2nd edition  * (Addison-Wesley, 1998). * Status:  Believed complete, but untested. *//** * This class manages a queue of <code>AWTEvent</code> objects that * are posted to it.  The AWT system uses only one event queue for all * events. * * @author Bryce McKinlay * @author Aaron M. Renn (arenn@urbanophile.com) */public class EventQueue{  private static final int INITIAL_QUEUE_DEPTH = 8;  private AWTEvent[] queue = new AWTEvent[INITIAL_QUEUE_DEPTH];  private int next_in = 0; // Index where next event will be added to queue  private int next_out = 0; // Index of next event to be removed from queue  private EventQueue next;  private EventQueue prev;  private AWTEvent currentEvent;  private long lastWhen = System.currentTimeMillis();  private EventDispatchThread dispatchThread = new EventDispatchThread(this);  private boolean shutdown = false;  private long lastNativeQueueAccess = 0;  private long humanLatencyThreshold = 100;  synchronized void setShutdown (boolean b)   {    shutdown = b;  }  synchronized boolean isShutdown ()  {    if (shutdown)      return true;    // This is the exact self-shutdown condition specified in J2SE:    // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/doc-files/AWTThreadIssues.html        if (peekEvent() == null        && ((ClasspathToolkit) Toolkit.getDefaultToolkit()).nativeQueueEmpty())      {        Frame[] frames = Frame.getFrames();        for (int i = 0; i < frames.length; ++i)          if (frames[i].isDisplayable())            return false;        return true;      }    return false;  }  /**   * Initializes a new instance of <code>EventQueue</code>.   */  public EventQueue()  {  }  /**   * Returns the next event in the queue.  This method will block until   * an event is available or until the thread is interrupted.   *   * @return The next event in the queue.   *   * @exception InterruptedException If this thread is interrupted while   * waiting for an event to be posted to the queue.   */  public synchronized AWTEvent getNextEvent()    throws InterruptedException  {    if (next != null)      return next.getNextEvent();        ClasspathToolkit tk = ((ClasspathToolkit) Toolkit.getDefaultToolkit());    long curr = System.currentTimeMillis();    if (! tk.nativeQueueEmpty() &&        (curr - lastNativeQueueAccess > humanLatencyThreshold))      {        tk.iterateNativeQueue(this, false);        lastNativeQueueAccess = curr;      }    while (next_in == next_out)      {        // Only the EventDispatchThread associated with the top of the stack is        // allowed to get events from the native source; everyone else just        // waits on the head of the queue.        if (isDispatchThread())          {            // We are not allowed to return null from this method, yet it            // is possible that we actually have run out of native events            // in the enclosing while() loop, and none of the native events            // happened to cause AWT events. We therefore ought to check            // the isShutdown() condition here, before risking a "native            // wait". If we check it before entering this function we may            // wait forever for events after the shutdown condition has            // arisen.            if (isShutdown())              throw new InterruptedException();            tk.iterateNativeQueue(this, true);            lastNativeQueueAccess = System.currentTimeMillis();          }        else          {            try              {                wait();              }            catch (InterruptedException ie)              {              }          }      }    AWTEvent res = queue[next_out];    if (++next_out == queue.length)      next_out = 0;    return res;  }  /**   * Returns the next event in the queue without removing it from the queue.   * This method will block until an event is available or until the thread   * is interrupted.   *   * @return The next event in the queue.   * @specnote Does not block. Returns null if there are no events on the    *            queue.    */   public synchronized AWTEvent peekEvent()  {    if (next != null)      return next.peekEvent();    if (next_in != next_out)      return queue[next_out];    else      return null;  }  /**   * Returns the next event in the queue that has the specified id   * without removing it from the queue.   * This method will block until an event is available or until the thread   * is interrupted.   *   * @param id The event id to return.   *   * @return The next event in the queue.   *   * @specnote Does not block. Returns null if there are no matching events    *            on the queue.    */   public synchronized AWTEvent peekEvent(int id)  {    if (next != null)      return next.peekEvent(id);    int i = next_out;    while (i != next_in)      {        AWTEvent qevt = queue[i];        if (qevt.id == id)          return qevt;      }    return null;  }  /**   * Posts a new event to the queue.   *   * @param evt The event to post to the queue.   *   * @exception NullPointerException If event is null.   */  public synchronized void postEvent(AWTEvent evt)  {    if (evt == null)      throw new NullPointerException();    if (next != null)      {        next.postEvent(evt);        return;      }    /* Check for any events already on the queue with the same source        and ID. */	    int i = next_out;    while (i != next_in)      {        AWTEvent qevt = queue[i];        Object src;        if (qevt.id == evt.id            && (src = qevt.getSource()) == evt.getSource()            && src instanceof Component)          {            /* If there are, call coalesceEvents on the source component                to see if they can be combined. */            Component srccmp = (Component) src;            AWTEvent coalesced_evt = srccmp.coalesceEvents(qevt, evt);            if (coalesced_evt != null)              {                /* Yes. Replace the existing event with the combined event. */                queue[i] = coalesced_evt;                return;              }            break;          }        if (++i == queue.length)          i = 0;      }    queue[next_in] = evt;        if (++next_in == queue.length)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?