home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / AWTEvent.java < prev    next >
Text File  |  1997-05-20  |  12KB  |  337 lines

  1. /*
  2.  * @(#)AWTEvent.java    1.20 97/02/21 Carl Quinn
  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.util.EventObject;
  26. import java.awt.event.*;
  27.  
  28. /**
  29.  * The root event class for all AWT events.
  30.  * This class and its subclasses supercede the original
  31.  * java.awt.Event class.
  32.  * Subclasses of this root AWTEvent class defined outside of the
  33.  * java.awt.event package should define event ID values greater than
  34.  * the value defined by RESERVED_ID_MAX.
  35.  *
  36.  * The event masks defined in this class are needed ONLY by
  37.  * component subclasses which are using Component.enableEvents()
  38.  * to select for event types not selected by registered listeners.
  39.  * If a listener is registered on a component, the appropriate event
  40.  * mask is already set internally by the component.
  41.  * @see Component#enableEvents
  42.  *
  43.  * @see java.awt.event.ComponentEvent
  44.  * @see java.awt.event.FocusEvent
  45.  * @see java.awt.event.KeyEvent
  46.  * @see java.awt.event.MouseEvent
  47.  * @see java.awt.event.WindowEvent
  48.  * @see java.awt.event.ActionEvent
  49.  * @see java.awt.event.AdjustmentEvent
  50.  * @see java.awt.event.ItemEvent
  51.  * @see java.awt.event.TextEvent
  52.  *
  53.  * @version 1.20 02/21/97
  54.  * @author Carl Quinn
  55.  * @author Amy Fowler
  56.  */
  57. public abstract class AWTEvent extends EventObject {
  58.     private transient int data;
  59.  
  60.     protected int id;
  61.  
  62.     // This field controls whether or not the event is sent back
  63.     // down to the peer once the source has processed it -
  64.     // false means it's sent to the peer, true means it's not.
  65.     // Semantic events always have a 'true' value since they were
  66.     // generated by the peer in response to a low-level event.
  67.     protected boolean consumed = false;
  68.  
  69.     /**
  70.      * The event mask for selecting component events.
  71.      */
  72.     public final static long COMPONENT_EVENT_MASK = 0x01;
  73.  
  74.     /**
  75.      * The event mask for selecting container events.
  76.      */
  77.     public final static long CONTAINER_EVENT_MASK = 0x02;
  78.  
  79.     /**
  80.      * The event mask for selecting focus events.
  81.      */
  82.     public final static long FOCUS_EVENT_MASK = 0x04;
  83.  
  84.     /**
  85.      * The event mask for selecting key events.
  86.      */
  87.     public final static long KEY_EVENT_MASK = 0x08;
  88.  
  89.     /**
  90.      * The event mask for selecting mouse events.
  91.      */
  92.     public final static long MOUSE_EVENT_MASK = 0x10;
  93.  
  94.     /**
  95.      * The event mask for selecting mouse motion events.
  96.      */
  97.     public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
  98.  
  99.     /**
  100.      * The event mask for selecting window events.
  101.      */
  102.     public final static long WINDOW_EVENT_MASK = 0x40;
  103.  
  104.     /**
  105.      * The event mask for selecting action events.
  106.      */
  107.     public final static long ACTION_EVENT_MASK = 0x80;
  108.  
  109.     /**
  110.      * The event mask for selecting adjustment events.
  111.      */
  112.     public final static long ADJUSTMENT_EVENT_MASK = 0x100;
  113.  
  114.     /**
  115.      * The event mask for selecting item events.
  116.      */
  117.     public final static long ITEM_EVENT_MASK = 0x200;
  118.  
  119.     /**
  120.      * The event mask for selecting text events.
  121.      */
  122.     public final static long TEXT_EVENT_MASK = 0x400;
  123.  
  124.     /**
  125.      * The maximum value for reserved AWT event IDs. Programs defining
  126.      * their own event IDs should use IDs greater than this value.
  127.      */
  128.     public final static int RESERVED_ID_MAX = 1999;
  129.  
  130.     /*
  131.      * JDK 1.1 serialVersionUID 
  132.      */
  133.     private static final long serialVersionUID = -1825314779160409405L;
  134.  
  135.     /**
  136.      * Constructs an AWTEvent object from the parameters of a 1.0-style event.
  137.      * @param event the old-style event
  138.      */
  139.     public AWTEvent(Event event) {
  140.         this(event.target, event.id);
  141.     }
  142.  
  143.     /**
  144.      * Constructs an AWTEvent object with the specified source object and type.
  145.      * @param source the object where the event originated
  146.      * @id the event type
  147.      */
  148.     public AWTEvent(Object source, int id) {
  149.         super(source);
  150.     this.id = id;
  151.         switch(id) {
  152.           case ActionEvent.ACTION_PERFORMED:
  153.           case ItemEvent.ITEM_STATE_CHANGED:
  154.           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  155.           case TextEvent.TEXT_VALUE_CHANGED:
  156.             consumed = true;
  157.             break;
  158.           default:
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Returns the event type.
  164.      */
  165.     public int getID() {
  166.         return id;
  167.     }
  168.  
  169.     public String toString() {
  170.         String srcName = null;
  171.         if (source instanceof Component) {
  172.             srcName = ((Component)source).getName();
  173.         } else if (source instanceof MenuComponent) {
  174.             srcName = ((MenuComponent)source).getName();
  175.         }
  176.     return getClass().getName() + "[" + paramString() + "] on " +
  177.             (srcName != null? srcName : source);
  178.     }
  179.  
  180.     public String paramString() {
  181.         return "";
  182.     }
  183.  
  184.     protected void consume() {
  185.         switch(id) {
  186.           case KeyEvent.KEY_PRESSED:
  187.           case KeyEvent.KEY_RELEASED:
  188.           case MouseEvent.MOUSE_PRESSED:
  189.           case MouseEvent.MOUSE_RELEASED:
  190.           case MouseEvent.MOUSE_MOVED:
  191.           case MouseEvent.MOUSE_DRAGGED:
  192.           case MouseEvent.MOUSE_ENTERED:
  193.           case MouseEvent.MOUSE_EXITED:
  194.               consumed = true;
  195.               break;
  196.           default:
  197.               // event type cannot be consumed
  198.         }
  199.     }
  200.  
  201.     protected boolean isConsumed() {
  202.         return consumed;
  203.     }
  204.  
  205.     /* Converts a new event to an old one (used for compatibility).
  206.      * If the new event cannot be converted (because no old equivelent
  207.      * exists) then this returns null.
  208.      *
  209.      * Note: this method is here instead of in each individual new
  210.      * event class in java.awt.event because we don't want to make
  211.      * it public and it needs to be called from java.awt.
  212.      */
  213.     Event convertToOld() {
  214.         Object src = getSource();
  215.         int newid = id;
  216.  
  217.         switch(id) {
  218.           case KeyEvent.KEY_PRESSED:
  219.           case KeyEvent.KEY_RELEASED:
  220.               KeyEvent ke = (KeyEvent)this;
  221.               if (ke.isActionKey()) {
  222.                   newid = (id == KeyEvent.KEY_PRESSED? 
  223.                            Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
  224.               }
  225.               int keyCode = ke.getKeyCode();
  226.               if (keyCode == KeyEvent.VK_SHIFT || 
  227.                   keyCode == KeyEvent.VK_CONTROL || 
  228.                   keyCode == KeyEvent.VK_ALT) {
  229.                   return null;  // suppress modifier keys in old event model.
  230.               }
  231.               // no mask for button1 existed in old Event - strip it out
  232.               return new Event(src, ke.getWhen(), newid, 0, 0,
  233.                                Event.getOldEventKey(ke), 
  234.                                (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
  235.  
  236.           case MouseEvent.MOUSE_PRESSED:
  237.           case MouseEvent.MOUSE_RELEASED:
  238.           case MouseEvent.MOUSE_MOVED:
  239.           case MouseEvent.MOUSE_DRAGGED:
  240.           case MouseEvent.MOUSE_ENTERED:
  241.           case MouseEvent.MOUSE_EXITED:
  242.               MouseEvent me = (MouseEvent)this;
  243.               // no mask for button1 existed in old Event - strip it out
  244.               Event olde = new Event(src, me.getWhen(), newid, 
  245.                                me.getX(), me.getY(), 0, 
  246.                                (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
  247.               olde.clickCount = me.getClickCount();
  248.               return olde;
  249.  
  250.           case FocusEvent.FOCUS_GAINED:
  251.               return new Event(src, Event.GOT_FOCUS, null);
  252.  
  253.           case FocusEvent.FOCUS_LOST:
  254.               return new Event(src, Event.LOST_FOCUS, null);
  255.  
  256.           case WindowEvent.WINDOW_CLOSING:
  257.           case WindowEvent.WINDOW_ICONIFIED:
  258.           case WindowEvent.WINDOW_DEICONIFIED:
  259.               return new Event(src, newid, null);
  260.  
  261.           case ComponentEvent.COMPONENT_MOVED:
  262.               if (src instanceof Frame || src instanceof Dialog) {
  263.                   Point p = ((Component)src).getLocation();
  264.                   return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
  265.               }
  266.               break;
  267.  
  268.           case ActionEvent.ACTION_PERFORMED:
  269.               ActionEvent ae = (ActionEvent)this;
  270.               String cmd;
  271.               if (src instanceof Button) { 
  272.                   cmd = ((Button)src).getLabel();
  273.               } else if (src instanceof MenuItem) {
  274.                   cmd = ((MenuItem)src).getLabel();
  275.               } else {
  276.                   cmd = ae.getActionCommand();
  277.               } 
  278.               return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
  279.  
  280.           case ItemEvent.ITEM_STATE_CHANGED:
  281.               ItemEvent ie = (ItemEvent)this;
  282.               Object arg;
  283.               if (src instanceof List) {
  284.                   newid = (ie.getStateChange() == ItemEvent.SELECTED?
  285.                            Event.LIST_SELECT : Event.LIST_DESELECT);
  286.                   arg = ie.getItem();
  287.               } else {
  288.                   newid = Event.ACTION_EVENT;
  289.                   if (src instanceof Choice) {
  290.                       arg = ie.getItem();
  291.  
  292.                   } else { // Checkbox
  293.                       arg = new Boolean(ie.getStateChange() == ItemEvent.SELECTED);
  294.                   }
  295.               }             
  296.               return new Event(src, newid, arg);
  297.  
  298.           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  299.               AdjustmentEvent aje = (AdjustmentEvent)this;
  300.               switch(aje.getAdjustmentType()) {
  301.                 case AdjustmentEvent.UNIT_INCREMENT:
  302.                   newid = Event.SCROLL_LINE_DOWN;
  303.                   break;
  304.                 case AdjustmentEvent.UNIT_DECREMENT:
  305.                   newid = Event.SCROLL_LINE_UP;
  306.                   break;
  307.                 case AdjustmentEvent.BLOCK_INCREMENT:
  308.                   newid = Event.SCROLL_PAGE_DOWN;
  309.                   break;
  310.                 case AdjustmentEvent.BLOCK_DECREMENT:
  311.                   newid = Event.SCROLL_PAGE_UP;
  312.                   break;
  313.                 case AdjustmentEvent.TRACK:
  314.                   newid = Event.SCROLL_ABSOLUTE;
  315.                   break;
  316.                 default:
  317.                   return null;
  318.               }
  319.               return new Event(src, newid, new Integer(aje.getValue()));
  320.  
  321.           default:
  322.         }
  323.         return null;
  324.     }                          
  325.  
  326.     /* Package-private method to change a KeyEvent's source to the new
  327.      * focus owner.  This method needs to be here instead of in KeyEvent
  328.      * because it should only be called from by the EventQueue.
  329.      */
  330.     void setSource(Object newSource) {
  331.         if (!(this instanceof KeyEvent)) {
  332.             throw new ClassCastException();
  333.         }
  334.         source = newSource;
  335.     }
  336. }
  337.