예제 #1
0
  /** Is called when the applet wants to be resized. */
  @Override
  public void appletResize(int width, int height) {
    currentAppletSize.width = width;
    currentAppletSize.height = height;
    final Dimension currentSize = new Dimension(currentAppletSize.width, currentAppletSize.height);

    if (loader != null) {
      AppContext appCtxt = loader.getAppContext();
      if (appCtxt != null) appEvtQ = (java.awt.EventQueue) appCtxt.get(AppContext.EVENT_QUEUE_KEY);
    }

    final AppletPanel ap = this;
    if (appEvtQ != null) {
      appEvtQ.postEvent(
          new InvocationEvent(
              Toolkit.getDefaultToolkit(),
              new Runnable() {
                @Override
                public void run() {
                  if (ap != null) {
                    ap.dispatchAppletEvent(APPLET_RESIZE, currentSize);
                  }
                }
              }));
    }
  }
예제 #2
0
파일: EventQueue.java 프로젝트: Blueash/gcc
  /**
   * This arranges for runnable to have its run method called in the dispatch thread of the
   * EventQueue. This will happen after all pending events are processed.
   *
   * @since 1.2
   */
  public static void invokeLater(Runnable runnable) {
    EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();

    InvocationEvent ie = new InvocationEvent(eq, runnable, null, false);

    eq.postEvent(ie);
  }
 private void wakeupEDT() {
   if (log.isLoggable(PlatformLogger.Level.FINEST)) {
     log.finest("wakeupEDT(): EDT == " + dispatchThread);
   }
   EventQueue eq = dispatchThread.getEventQueue();
   eq.postEvent(new PeerEvent(this, wakingRunnable, PeerEvent.PRIORITY_EVENT));
 }
 // Post the given event to the corresponding event queue for the given component.
 void postEvent(@Nullable Component c, @Nonnull AWTEvent event) {
   // Force an update of the input state, so that we're in synch internally. Otherwise we might
   // post more events before
   // this one gets processed and end up using stale values for those events.
   inputState.update(event);
   EventQueue eventQueue = eventQueueFor(c);
   if (eventQueue != null) {
     eventQueue.postEvent(event);
   }
   pause(settings.delayBetweenEvents());
 }
예제 #5
0
 /**
  * Pass an event onto the AWT component.
  *
  * @see java.awt.Component#processEvent(java.awt.AWTEvent)
  */
 protected final void processEvent(AWTEvent event) {
   AppContext ac = SunToolkit.targetToAppContext(target);
   if (ac == null) {
     target.dispatchEvent(SwingToolkit.convertEvent(event, target));
   } else {
     EventQueue eq = (EventQueue) ac.get(AppContext.EVENT_QUEUE_KEY);
     if (eq == null) {
       target.dispatchEvent(SwingToolkit.convertEvent(event, target));
     } else {
       eq.postEvent(SwingToolkit.convertEvent(event, target));
     }
   }
 }
예제 #6
0
파일: EventQueue.java 프로젝트: Blueash/gcc
  /**
   * Causes runnable to have its run method called in the dispatch thread of the EventQueue. This
   * will happen after all pending events are processed. The call blocks until this has happened.
   * This method will throw an Error if called from the event dispatcher thread.
   *
   * @exception InterruptedException If another thread has interrupted this thread.
   * @exception InvocationTargetException If an exception is thrown when running runnable.
   * @since 1.2
   */
  public static void invokeAndWait(Runnable runnable)
      throws InterruptedException, InvocationTargetException {
    if (isDispatchThread()) throw new Error("Can't call invokeAndWait from event dispatch thread");

    EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
    Object notifyObject = new Object();

    InvocationEvent ie = new InvocationEvent(eq, runnable, notifyObject, true);

    synchronized (notifyObject) {
      eq.postEvent(ie);
      notifyObject.wait();
    }

    Exception exception;

    if ((exception = ie.getException()) != null) throw new InvocationTargetException(exception);
  }
예제 #7
0
 public void run() {
   final EventQueue eq = (EventQueue) appContext.get(EVENT_QUEUE_KEY);
   if (eq != null) {
     eq.postEvent(AWTAutoShutdown.getShutdownEvent());
   }
 }
예제 #8
0
파일: EventQueue.java 프로젝트: Blueash/gcc
  /**
   * Actually performs the event posting. This is needed because the RI doesn't use the public
   * postEvent() method when transferring events between event queues in push() and pop().
   *
   * @param evt the event to post
   * @param priority the priority of the event
   */
  private final void postEventImpl(AWTEvent evt, int priority) {
    if (evt == null) throw new NullPointerException();

    if (next != null) {
      next.postEvent(evt);
      return;
    }

    Object source = evt.getSource();

    Queue q = queues[priority];
    if (source instanceof Component) {
      // For PaintEvents, ask the ComponentPeer to coalesce the event
      // when the component is heavyweight.
      Component comp = (Component) source;
      ComponentPeer peer = comp.peer;
      if (peer != null && evt instanceof PaintEvent && !(peer instanceof LightweightPeer))
        peer.coalescePaintEvent((PaintEvent) evt);

      // Check for any events already on the queue with the same source
      // and ID.
      AWTEvent previous = null;
      for (AWTEvent qevt = q.queueHead; qevt != null; qevt = qevt.queueNext) {
        Object src = qevt.getSource();
        if (qevt.id == evt.id && src == comp) {
          // If there are, call coalesceEvents on the source component
          // to see if they can be combined.
          Component srccmp = (Component) src;
          AWTEvent coalescedEvt = srccmp.coalesceEvents(qevt, evt);
          if (coalescedEvt != null) {
            // Yes. Replace the existing event with the combined event.
            if (qevt != coalescedEvt) {
              if (previous != null) {
                assert previous.queueNext == qevt;
                previous.queueNext = coalescedEvt;
              } else {
                assert q.queueHead == qevt;
                q.queueHead = coalescedEvt;
              }
              coalescedEvt.queueNext = qevt.queueNext;
              if (q.queueTail == qevt) q.queueTail = coalescedEvt;
              qevt.queueNext = null;
            }
            return;
          }
        }
        previous = qevt;
      }
    }

    if (q.queueHead == null) {
      // We have an empty queue. Set this event both as head and as tail.
      q.queueHead = evt;
      q.queueTail = evt;
    } else {
      // Note: queueTail should not be null here.
      q.queueTail.queueNext = evt;
      q.queueTail = evt;
    }

    if (dispatchThread == null || !dispatchThread.isAlive()) {
      dispatchThread = new EventDispatchThread(this);
      dispatchThread.start();
    }

    notify();
  }
 private void expectEventQueueToPostEvent() {
   eventQueue.postEvent(event);
   expectLastCall().once();
 }
  /** @inheritDoc */
  @Override
  public boolean enter() {
    if (log.isLoggable(PlatformLogger.Level.FINE)) {
      log.fine(
          "enter(): blockingEDT=" + keepBlockingEDT.get() + ", blockingCT=" + keepBlockingCT.get());
    }

    if (!keepBlockingEDT.compareAndSet(false, true)) {
      log.fine("The secondary loop is already running, aborting");
      return false;
    }

    final Runnable run =
        new Runnable() {
          public void run() {
            log.fine("Starting a new event pump");
            if (filter == null) {
              dispatchThread.pumpEvents(condition);
            } else {
              dispatchThread.pumpEventsForFilter(condition, filter);
            }
          }
        };

    // We have two mechanisms for blocking: if we're on the
    // dispatch thread, start a new event pump; if we're
    // on any other thread, call wait() on the treelock

    Thread currentThread = Thread.currentThread();
    if (currentThread == dispatchThread) {
      if (log.isLoggable(PlatformLogger.Level.FINEST)) {
        log.finest("On dispatch thread: " + dispatchThread);
      }
      if (interval != 0) {
        if (log.isLoggable(PlatformLogger.Level.FINEST)) {
          log.finest("scheduling the timer for " + interval + " ms");
        }
        timer.schedule(
            timerTask =
                new TimerTask() {
                  @Override
                  public void run() {
                    if (keepBlockingEDT.compareAndSet(true, false)) {
                      wakeupEDT();
                    }
                  }
                },
            interval);
      }
      // Dispose SequencedEvent we are dispatching on the the current
      // AppContext, to prevent us from hang - see 4531693 for details
      SequencedEvent currentSE =
          KeyboardFocusManager.getCurrentKeyboardFocusManager().getCurrentSequencedEvent();
      if (currentSE != null) {
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
          log.fine("Dispose current SequencedEvent: " + currentSE);
        }
        currentSE.dispose();
      }
      // In case the exit() method is called before starting
      // new event pump it will post the waking event to EDT.
      // The event will be handled after the the new event pump
      // starts. Thus, the enter() method will not hang.
      //
      // Event pump should be privileged. See 6300270.
      AccessController.doPrivileged(
          new PrivilegedAction() {
            public Object run() {
              run.run();
              return null;
            }
          });
    } else {
      if (log.isLoggable(PlatformLogger.Level.FINEST)) {
        log.finest("On non-dispatch thread: " + currentThread);
      }
      synchronized (getTreeLock()) {
        if (filter != null) {
          dispatchThread.addEventFilter(filter);
        }
        try {
          EventQueue eq = dispatchThread.getEventQueue();
          eq.postEvent(new PeerEvent(this, run, PeerEvent.PRIORITY_EVENT));
          keepBlockingCT.set(true);
          if (interval > 0) {
            long currTime = System.currentTimeMillis();
            while (keepBlockingCT.get()
                && ((extCondition != null) ? extCondition.evaluate() : true)
                && (currTime + interval > System.currentTimeMillis())) {
              getTreeLock().wait(interval);
            }
          } else {
            while (keepBlockingCT.get()
                && ((extCondition != null) ? extCondition.evaluate() : true)) {
              getTreeLock().wait();
            }
          }
          if (log.isLoggable(PlatformLogger.Level.FINE)) {
            log.fine("waitDone " + keepBlockingEDT.get() + " " + keepBlockingCT.get());
          }
        } catch (InterruptedException e) {
          if (log.isLoggable(PlatformLogger.Level.FINE)) {
            log.fine("Exception caught while waiting: " + e);
          }
        } finally {
          if (filter != null) {
            dispatchThread.removeEventFilter(filter);
          }
        }
        // If the waiting process has been stopped because of the
        // time interval passed or an exception occurred, the state
        // should be changed
        keepBlockingEDT.set(false);
        keepBlockingCT.set(false);
      }
    }

    return true;
  }