Home » openjdk-7 » java » awt » [javadoc | source]

    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 Pavel Dolgov
   19    */
   20   package java.awt;
   21   
   22   import java.awt.event.ActionEvent;
   23   import java.awt.event.InputEvent;
   24   import java.awt.event.InputMethodEvent;
   25   import java.awt.event.InvocationEvent;
   26   import java.awt.event.MouseEvent;
   27   import java.util.LinkedList;
   28   
   29   import org.apache.harmony.awt.internal.nls.Messages;
   30   
   31   /**
   32    * The events storage for EventQueue
   33    */
   34   final class EventQueueCore {
   35       
   36       private final LinkedList<EventQueue> queueStack = new LinkedList<EventQueue>();
   37       private final LinkedList<AWTEvent> events = new LinkedList<AWTEvent>();
   38       
   39       private Toolkit toolkit;
   40       private EventQueue activeQueue;
   41       private Thread dispatchThread;
   42       
   43       AWTEvent currentEvent;
   44       long mostRecentEventTime = System.currentTimeMillis();
   45       
   46       EventQueueCore(EventQueue eq) {
   47           synchronized (this) {
   48               queueStack.addLast(eq);
   49               activeQueue = eq;
   50           }
   51       }
   52   
   53       EventQueueCore(EventQueue eq, Toolkit t) {
   54           synchronized (this) {
   55               queueStack.addLast(eq);
   56               activeQueue = eq;
   57               setToolkit(t);
   58           }
   59       }
   60   
   61       synchronized long getMostRecentEventTime() {
   62           return mostRecentEventTime;
   63       }
   64       
   65       synchronized AWTEvent getCurrentEvent() {
   66           return currentEvent;
   67       }
   68       
   69       synchronized boolean isSystemEventQueue() {
   70           return toolkit != null;
   71       }
   72       
   73       private void setToolkit(Toolkit t) {
   74           toolkit = t;
   75           if (toolkit != null) {
   76               toolkit.setSystemEventQueueCore(this);
   77               dispatchThread = toolkit.dispatchThread;
   78           }
   79       }
   80   
   81       synchronized void postEvent(AWTEvent event) {
   82           events.addLast(event);
   83           if ((toolkit == null) && (dispatchThread == null)) {
   84               dispatchThread = new EventQueueThread(this);
   85               dispatchThread.start();
   86           }
   87           // TODO: add event coalescing
   88           if (toolkit != null) {
   89               toolkit.shutdownWatchdog.setAwtQueueEmpty(false);
   90               notifyEventMonitor(toolkit);
   91           }
   92           notifyAll();
   93       }
   94       
   95       void notifyEventMonitor(Toolkit t) {
   96           Object em = t.getEventMonitor();
   97           synchronized (em) {
   98               em.notifyAll();
   99           }
  100       }
  101       
  102       synchronized AWTEvent getNextEvent() throws InterruptedException {
  103           while (events.isEmpty()) {
  104               wait();
  105           }
  106           AWTEvent event = events.removeFirst();
  107           // TODO: add event coalescing
  108           return event;
  109       }    
  110       
  111       synchronized AWTEvent peekEvent() {
  112           return events.isEmpty() ? null : events.getFirst();
  113       }
  114       
  115       synchronized AWTEvent peekEvent(int id) {
  116           for (AWTEvent event : events) {
  117               if (event.getID() == id) {
  118                   return event;
  119               }
  120           }
  121           return null;
  122       }
  123       
  124       synchronized void dispatchEvent(AWTEvent event) {
  125           updateCurrentEventAndTime(event);
  126           try {
  127               activeQueue.dispatchEvent(event);
  128           } finally {
  129               currentEvent = null;
  130           }
  131       }
  132       
  133       void dispatchEventImpl(AWTEvent event) {
  134           if (event instanceof ActiveEvent) {
  135               updateCurrentEventAndTime(event);
  136               try {
  137                   ((ActiveEvent) event).dispatch();
  138               } finally {
  139                   currentEvent = null;
  140               }
  141               return;
  142           }
  143   
  144           Object src = event.getSource();
  145   
  146           if (src instanceof Component) {
  147               if (preprocessComponentEvent(event)) {
  148                   ((Component) src).dispatchEvent(event);
  149               }
  150           } else {
  151               if (toolkit != null) {
  152                   toolkit.dispatchAWTEvent(event);
  153               }
  154               if (src instanceof MenuComponent) {
  155                   ((MenuComponent) src).dispatchEvent(event);
  156               }
  157           }
  158       }
  159   
  160       private final boolean preprocessComponentEvent(AWTEvent event) {
  161         if (event instanceof MouseEvent) {
  162             return preprocessMouseEvent((MouseEvent)event);
  163         }
  164         return true;
  165       }
  166   
  167       private final boolean preprocessMouseEvent(MouseEvent event) {
  168         if (toolkit != null && toolkit.mouseEventPreprocessor != null) {
  169             toolkit.lockAWT();
  170             try {
  171                 return toolkit.mouseEventPreprocessor.preprocess(event);
  172             } finally {
  173                 toolkit.unlockAWT();
  174             }
  175         }
  176         return true;
  177     }
  178       
  179       private void updateCurrentEventAndTime(AWTEvent event) {
  180           currentEvent = event;
  181           long when = 0;
  182           if (event instanceof ActionEvent) {
  183               when = ((ActionEvent) event).getWhen();
  184           } else if (event instanceof InputEvent) {
  185               when = ((InputEvent) event).getWhen();
  186           } else if (event instanceof InputMethodEvent) {
  187               when = ((InputMethodEvent) event).getWhen();
  188           } else if (event instanceof InvocationEvent) {
  189               when = ((InvocationEvent) event).getWhen();
  190           }
  191           if (when != 0) {
  192               mostRecentEventTime = when;
  193           }
  194       }
  195       
  196       synchronized void push(EventQueue newEventQueue) {
  197           // TODO: handle incorrect situations
  198           if (queueStack.isEmpty()) {
  199               // awt.6B=Queue stack is empty
  200               throw new IllegalStateException(Messages.getString("awt.6B")); //$NON-NLS-1$
  201           }
  202           
  203           queueStack.addLast(newEventQueue);
  204           activeQueue = newEventQueue;
  205           activeQueue.setCore(this);
  206       }
  207       
  208       synchronized void pop() {
  209           EventQueue removed = queueStack.removeLast();
  210           if (removed != activeQueue) {
  211               // awt.6C=Event queue stack is broken
  212               throw new IllegalStateException(Messages.getString("awt.6C")); //$NON-NLS-1$
  213           }
  214           activeQueue = queueStack.getLast();
  215           removed.setCore(null);
  216       }
  217   
  218       synchronized AWTEvent getNextEventNoWait() {
  219           try {
  220               return events.isEmpty() ? null : activeQueue.getNextEvent();
  221           } catch (InterruptedException e) {
  222               return null;
  223           }
  224       }
  225   
  226       synchronized boolean isEmpty() {
  227           return (currentEvent == null) && events.isEmpty();
  228       }
  229       
  230       synchronized boolean isEmpty(long timeout) {
  231           if (!isEmpty()) {
  232               return false;
  233           }
  234           try {
  235               wait(timeout);
  236           } catch (InterruptedException e) {}
  237           return isEmpty();
  238       }
  239       
  240       synchronized EventQueue getActiveEventQueue() {
  241           return activeQueue;
  242       }
  243   }

Home » openjdk-7 » java » awt » [javadoc | source]