Example #1
0
  /**
   * Allows a custom EventQueue implementation to replace this one. All pending events are
   * transferred to the new queue. Calls to postEvent, getNextEvent, and peekEvent and others are
   * forwarded to the pushed queue until it is removed with a pop().
   *
   * @exception NullPointerException if newEventQueue is null.
   */
  public synchronized void push(EventQueue newEventQueue) {
    if (newEventQueue == null) throw new NullPointerException();

    /* Make sure we are at the top of the stack because callers can
    only get a reference to the one at the bottom using
    Toolkit.getDefaultToolkit().getSystemEventQueue() */
    if (next != null) {
      next.push(newEventQueue);
      return;
    }

    /* Make sure we have a live dispatch thread to drive the queue */
    if (dispatchThread == null) dispatchThread = new EventDispatchThread(this);

    synchronized (newEventQueue) {
      // The RI transfers the events without calling the new eventqueue's
      // push(), but using getNextEvent().
      while (peekEvent() != null) {
        try {
          newEventQueue.postEventImpl(getNextEvent());
        } catch (InterruptedException ex) {
          // What should we do with this?
          ex.printStackTrace();
        }
      }
      newEventQueue.prev = this;
    }

    next = newEventQueue;
  }
Example #2
0
 /**
  * Sorts events to their priority and calls {@link #postEventImpl(AWTEvent, int)}.
  *
  * @param evt the event to post
  */
 private final synchronized void postEventImpl(AWTEvent evt) {
   int priority = NORM_PRIORITY;
   if (evt instanceof PaintEvent || evt instanceof LowPriorityEvent) priority = LOW_PRIORITY;
   // TODO: Maybe let Swing RepaintManager events also be processed with
   // low priority.
   if (evt instanceof NativeEventLoopRunningEvent) {
     nativeLoopRunning = ((NativeEventLoopRunningEvent) evt).isRunning();
     notify();
     return;
   }
   postEventImpl(evt, priority);
 }
Example #3
0
  /**
   * Transfer any pending events from this queue back to the parent queue that was previously
   * push()ed. Event dispatch from this queue is suspended.
   *
   * @exception EmptyStackException If no previous push was made on this EventQueue.
   */
  protected void pop() throws EmptyStackException {
    /* The order is important here, we must get the prev lock first,
    or deadlock could occur as callers usually get here following
    prev's next pointer, and thus obtain prev's lock before trying
    to get this lock. */
    EventQueue previous = prev;
    if (previous == null) throw new EmptyStackException();
    synchronized (previous) {
      synchronized (this) {
        EventQueue nextQueue = next;
        if (nextQueue != null) {
          nextQueue.pop();
        } else {
          previous.next = null;

          // The RI transfers the events without calling the new eventqueue's
          // push(), so this should be OK and most effective.
          while (peekEvent() != null) {
            try {
              previous.postEventImpl(getNextEvent());
            } catch (InterruptedException ex) {
              // What should we do with this?
              ex.printStackTrace();
            }
          }
          prev = null;
          // Tell our EventDispatchThread that it can end
          // execution.
          if (dispatchThread != null) {
            dispatchThread.interrupt();
            dispatchThread = null;
          }
        }
      }
    }
  }
Example #4
0
 /**
  * Posts a new event to the queue.
  *
  * @param evt The event to post to the queue.
  * @exception NullPointerException If event is null.
  */
 public void postEvent(AWTEvent evt) {
   postEventImpl(evt);
 }