home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / EventQueue.java < prev    next >
Text File  |  1997-10-01  |  7KB  |  212 lines

  1. /*
  2.  * @(#)EventQueue.java    1.17 97/06/23
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt;
  24.  
  25. import java.awt.event.FocusEvent;
  26. import java.awt.event.KeyEvent;
  27. import java.awt.event.MouseEvent;
  28. import java.awt.event.PaintEvent;
  29.  
  30. /**
  31.  * EventQueue is a platform-independent class that queues events, both
  32.  * from the underlying peer classes and from trusted application classes.
  33.  * There is only one EventQueue for the system.
  34.  *
  35.  * @version 1.17 06/23/97
  36.  * @author Thomas Ball
  37.  */
  38. public class EventQueue {
  39.  
  40.     // From Thread.java
  41.     private static int threadInitNumber;
  42.     private static synchronized int nextThreadNum() {
  43.     return threadInitNumber++;
  44.     }
  45.  
  46.     private EventQueueItem queue;
  47.  
  48.     public EventQueue() {
  49.         queue = null;
  50.         String name = "AWT-EventQueue-" + nextThreadNum();
  51.         new EventDispatchThread(name, this).start();
  52.     }
  53.  
  54.     /**
  55.      * Post a 1.1-style event to the EventQueue.
  56.      *
  57.      * @param theEvent an instance of java.awt.AWTEvent, or a
  58.      * subclass of it.
  59.      */
  60.     public synchronized void postEvent(AWTEvent theEvent) {
  61.         EventQueueItem eqi = new EventQueueItem(theEvent);
  62.         if (queue == null) {
  63.             queue = eqi;
  64.             notifyAll();
  65.         } else {
  66.             EventQueueItem q = queue;
  67.             for (;;) {
  68.                 if (q.id == eqi.id) {
  69.                     switch (q.id) {
  70.                       case Event.MOUSE_MOVE:
  71.                       case Event.MOUSE_DRAG:
  72.                           // New-style event id's never collide with 
  73.                           // old-style id's, so if the id's are equal, 
  74.                           // we can safely cast the queued event to be 
  75.                           // an old-style one.
  76.                           MouseEvent e = (MouseEvent)q.event;
  77.                           if (e.getSource() == ((MouseEvent)theEvent).getSource() &&
  78.                               e.getModifiers() == ((MouseEvent)theEvent).getModifiers()) {
  79.                               q.event = eqi.event;// just replace old event
  80.                               return;
  81.                           }
  82.                           break;
  83.  
  84.                       case PaintEvent.PAINT:
  85.                       case PaintEvent.UPDATE:
  86.                           PaintEvent pe = (PaintEvent)q.event;
  87.                           if (pe.getSource() == theEvent.getSource()) {
  88.                               Rectangle rect = pe.getUpdateRect();
  89.                               Rectangle newRect = 
  90.                                   ((PaintEvent)theEvent).getUpdateRect();
  91.                               if (!rect.equals(newRect)) {
  92.                                   pe.setUpdateRect(rect.union(newRect));
  93.                               }
  94.                               return;
  95.                           }
  96.                           break;
  97.  
  98.                     }
  99.                 }
  100.                 if (q.next != null) {
  101.                     q = q.next;
  102.                 } else {
  103.                     break;
  104.                 }
  105.             }
  106.             q.next = eqi;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Remove an event from the queue and return it.  This method will
  112.      * block until an event has been posted by another thread.
  113.      * @return the next AWTEvent
  114.      * @exception InterruptedException 
  115.      *            if another thread has interrupted this thread.
  116.      */
  117.     public synchronized AWTEvent getNextEvent() throws InterruptedException {
  118.         while (queue == null) {
  119.             wait();
  120.         }
  121.         EventQueueItem eqi = queue;
  122.         queue = queue.next;
  123.         return eqi.event;
  124.     }
  125.  
  126.     /**
  127.      * Return the first event without removing it.
  128.      * @return the first event, which is either an instance of java.awt.Event
  129.      * or java.awt.AWTEvent.
  130.      */
  131.     public synchronized AWTEvent peekEvent() {
  132.         return (queue != null) ? queue.event : null;
  133.     }
  134.  
  135.     /*
  136.      * Return the first event of the specified type, if any.
  137.      * @param id the id of the type of event desired.
  138.      * @return the first event of the requested type, 
  139.      * which is either an instance of java.awt.Event
  140.      * or java.awt.AWTEvent.
  141.      */
  142.     public synchronized AWTEvent peekEvent(int id) {
  143.         EventQueueItem q = queue;
  144.         for (; q != null; q = q.next) {
  145.             if (q.id == id) {
  146.                 return q.event;
  147.             }
  148.         }
  149.         return null;
  150.     }
  151.  
  152.     /* comment out until 1.2...
  153.     /**
  154.      * Dispatch an event to its source.
  155.      *
  156.      * @param theEvent an instance of java.awt.AWTEvent, or a
  157.      * subclass of it.
  158.      *
  159.      *    protected void dispatchEvent(AWTEvent event) {
  160.      *        Object src = event.getSource();
  161.      *        if (src instanceof Component) {
  162.      *            ((Component)src).dispatchEvent(event);
  163.      *        } else if (src instanceof MenuComponent) {
  164.      *            ((MenuComponent)src).dispatchEvent(event);
  165.      *        }
  166.      *    }
  167.      */
  168.  
  169.     /*
  170.      * Change the target of any pending KeyEvents because of a focus change.
  171.      */
  172.     synchronized void changeKeyEventFocus(Object newSource) {
  173.         EventQueueItem q = queue;
  174.         for (; q != null; q = q.next) {
  175.             if (q.event instanceof KeyEvent) {
  176.                 q.event.setSource(newSource);
  177.             }
  178.         }
  179.     }
  180.  
  181.     /*
  182.      * Remove any pending events for the specified source object.
  183.      * This method is normally called by the source's removeNotify method.
  184.      */
  185.     synchronized void removeSourceEvents(Object source) {
  186.         EventQueueItem entry = queue;
  187.         EventQueueItem prev = null;
  188.         while (entry != null) {
  189.             if (entry.event.getSource().equals(source)) {
  190.                 if (prev == null) {
  191.                     queue = entry.next;
  192.                 } else {
  193.                     prev.next = entry.next;
  194.                 }
  195.             }
  196.             prev = entry;
  197.             entry = entry.next;
  198.         }
  199.     }
  200. }
  201.  
  202. class EventQueueItem {
  203.     AWTEvent event;
  204.     int      id;
  205.     EventQueueItem next;
  206.  
  207.     EventQueueItem(AWTEvent evt) {
  208.         event = evt;
  209.         id = evt.getID();
  210.     }
  211. }
  212.