home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / AWTEvent.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  13.1 KB  |  414 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AWTEvent.java    1.33 98/10/05
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.util.EventObject;
  18. import java.awt.event.*;
  19.  
  20. /**
  21.  * The root event class for all AWT events.
  22.  * This class and its subclasses supercede the original
  23.  * java.awt.Event class.
  24.  * Subclasses of this root AWTEvent class defined outside of the
  25.  * java.awt.event package should define event ID values greater than
  26.  * the value defined by RESERVED_ID_MAX.
  27.  *
  28.  * The event masks defined in this class are needed ONLY by
  29.  * component subclasses which are using Component.enableEvents()
  30.  * to select for event types not selected by registered listeners.
  31.  * If a listener is registered on a component, the appropriate event
  32.  * mask is already set internally by the component.
  33.  * @see Component#enableEvents
  34.  *
  35.  * @see java.awt.event.ComponentEvent
  36.  * @see java.awt.event.FocusEvent
  37.  * @see java.awt.event.KeyEvent
  38.  * @see java.awt.event.MouseEvent
  39.  * @see java.awt.event.WindowEvent
  40.  * @see java.awt.event.ActionEvent
  41.  * @see java.awt.event.AdjustmentEvent
  42.  * @see java.awt.event.ItemEvent
  43.  * @see java.awt.event.TextEvent
  44.  *
  45.  * @version 1.33 10/05/98
  46.  * @author Carl Quinn
  47.  * @author Amy Fowler
  48.  */
  49. public abstract class AWTEvent extends EventObject {
  50.     private transient long data;
  51.  
  52.     /*
  53.     * This field contains the event's event id
  54.     * @serial
  55.     * @see getID()
  56.     * @see AWTEvent())
  57.     */
  58.     protected int id;
  59.  
  60.     // This field controls whether or not the event is sent back
  61.     // down to the peer once the source has processed it -
  62.     // false means it's sent to the peer, true means it's not.
  63.     // Semantic events always have a 'true' value since they were
  64.     // generated by the peer in response to a low-level event.
  65.     /*
  66.     * @serial
  67.     * @see consume()
  68.     * @see isConsumed()
  69.     */
  70.     protected boolean consumed = false;
  71.  
  72.     /**
  73.      * The event mask for selecting component events.
  74.      */
  75.     public final static long COMPONENT_EVENT_MASK = 0x01;
  76.  
  77.     /**
  78.      * The event mask for selecting container events.
  79.      */
  80.     public final static long CONTAINER_EVENT_MASK = 0x02;
  81.  
  82.     /**
  83.      * The event mask for selecting focus events.
  84.      */
  85.     public final static long FOCUS_EVENT_MASK = 0x04;
  86.  
  87.     /**
  88.      * The event mask for selecting key events.
  89.      */
  90.     public final static long KEY_EVENT_MASK = 0x08;
  91.  
  92.     /**
  93.      * The event mask for selecting mouse events.
  94.      */
  95.     public final static long MOUSE_EVENT_MASK = 0x10;
  96.  
  97.     /**
  98.      * The event mask for selecting mouse motion events.
  99.      */
  100.     public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
  101.  
  102.     /**
  103.      * The event mask for selecting window events.
  104.      */
  105.     public final static long WINDOW_EVENT_MASK = 0x40;
  106.  
  107.     /**
  108.      * The event mask for selecting action events.
  109.      */
  110.     public final static long ACTION_EVENT_MASK = 0x80;
  111.  
  112.     /**
  113.      * The event mask for selecting adjustment events.
  114.      */
  115.     public final static long ADJUSTMENT_EVENT_MASK = 0x100;
  116.  
  117.     /**
  118.      * The event mask for selecting item events.
  119.      */
  120.     public final static long ITEM_EVENT_MASK = 0x200;
  121.  
  122.     /**
  123.      * The event mask for selecting text events.
  124.      */
  125.     public final static long TEXT_EVENT_MASK = 0x400;
  126.  
  127.     /**
  128.      * The event mask for selecting input method events.
  129.      */
  130.     public final static long INPUT_METHOD_EVENT_MASK = 0x800;
  131.  
  132.     /**
  133.      * The pseudo event mask for enabling input methods.
  134.      * We're using one bit in the eventMask so we don't need
  135.      * a separate field inputMethodsEnabled.
  136.      */
  137.     final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
  138.  
  139.     /**
  140.      * The maximum value for reserved AWT event IDs. Programs defining
  141.      * their own event IDs should use IDs greater than this value.
  142.      */
  143.     public final static int RESERVED_ID_MAX = 1999;
  144.  
  145.     /*
  146.      * JDK 1.1 serialVersionUID
  147.      */
  148.     private static final long serialVersionUID = -1825314779160409405L;
  149.  
  150.     static {
  151.         /* ensure that the necessary native libraries are loaded */
  152.     Toolkit.loadLibraries();
  153.     initIDs();
  154.     }
  155.  
  156.     /**
  157.      * Initialize JNI field and method IDs for fields that may be
  158.        accessed from C.
  159.      */
  160.     private static native void initIDs();
  161.  
  162.     /**
  163.      * Constructs an AWTEvent object from the parameters of a 1.0-style event.
  164.      * @param event the old-style event
  165.      */
  166.     public AWTEvent(Event event) {
  167.         this(event.target, event.id);
  168.     }
  169.  
  170.     /**
  171.      * Constructs an AWTEvent object with the specified source object and type.
  172.      * @param source the object where the event originated
  173.      * @id the event type
  174.      */
  175.     public AWTEvent(Object source, int id) {
  176.         super(source);
  177.     this.id = id;
  178.         switch(id) {
  179.           case ActionEvent.ACTION_PERFORMED:
  180.           case ItemEvent.ITEM_STATE_CHANGED:
  181.           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  182.           case TextEvent.TEXT_VALUE_CHANGED:
  183.             consumed = true;
  184.             break;
  185.           default:
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Returns the event type.
  191.      */
  192.     public int getID() {
  193.         return id;
  194.     }
  195.  
  196.     public String toString() {
  197.         String srcName = null;
  198.         if (source instanceof Component) {
  199.             srcName = ((Component)source).getName();
  200.         } else if (source instanceof MenuComponent) {
  201.             srcName = ((MenuComponent)source).getName();
  202.         }
  203.     return getClass().getName() + "[" + paramString() + "] on " +
  204.             (srcName != null? srcName : source);
  205.     }
  206.  
  207.     /**
  208.      * Returns a string representing the state of this event. This 
  209.      * method is intended to be used only for debugging purposes, and the 
  210.      * content and format of the returned string may vary between 
  211.      * implementations. The returned string may be empty but may not be 
  212.      * <code>null</code>.
  213.      * 
  214.      * @return  a string representation of this event.
  215.      */
  216.     public String paramString() {
  217.         return "";
  218.     }
  219.  
  220.     protected void consume() {
  221.         switch(id) {
  222.           case KeyEvent.KEY_PRESSED:
  223.           case KeyEvent.KEY_RELEASED:
  224.           case MouseEvent.MOUSE_PRESSED:
  225.           case MouseEvent.MOUSE_RELEASED:
  226.           case MouseEvent.MOUSE_MOVED:
  227.           case MouseEvent.MOUSE_DRAGGED:
  228.           case MouseEvent.MOUSE_ENTERED:
  229.           case MouseEvent.MOUSE_EXITED:
  230.           case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  231.           case InputMethodEvent.CARET_POSITION_CHANGED:
  232.               consumed = true;
  233.               break;
  234.           default:
  235.               // event type cannot be consumed
  236.         }
  237.     }
  238.  
  239.     protected boolean isConsumed() {
  240.         return consumed;
  241.     }
  242.  
  243.     /* Converts a new event to an old one (used for compatibility).
  244.      * If the new event cannot be converted (because no old equivelent
  245.      * exists) then this returns null.
  246.      *
  247.      * Note: this method is here instead of in each individual new
  248.      * event class in java.awt.event because we don't want to make
  249.      * it public and it needs to be called from java.awt.
  250.      */
  251.     Event convertToOld() {
  252.         Object src = getSource();
  253.         int newid = id;
  254.  
  255.         switch(id) {
  256.           case KeyEvent.KEY_PRESSED:
  257.           case KeyEvent.KEY_RELEASED:
  258.               KeyEvent ke = (KeyEvent)this;
  259.               if (ke.isActionKey()) {
  260.                   newid = (id == KeyEvent.KEY_PRESSED?
  261.                            Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
  262.               }
  263.               int keyCode = ke.getKeyCode();
  264.               if (keyCode == KeyEvent.VK_SHIFT ||
  265.                   keyCode == KeyEvent.VK_CONTROL ||
  266.                   keyCode == KeyEvent.VK_ALT) {
  267.                   return null;  // suppress modifier keys in old event model.
  268.               }
  269.               // no mask for button1 existed in old Event - strip it out
  270.               return new Event(src, ke.getWhen(), newid, 0, 0,
  271.                                Event.getOldEventKey(ke),
  272.                                (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
  273.  
  274.           case MouseEvent.MOUSE_PRESSED:
  275.           case MouseEvent.MOUSE_RELEASED:
  276.           case MouseEvent.MOUSE_MOVED:
  277.           case MouseEvent.MOUSE_DRAGGED:
  278.           case MouseEvent.MOUSE_ENTERED:
  279.           case MouseEvent.MOUSE_EXITED:
  280.               MouseEvent me = (MouseEvent)this;
  281.               // no mask for button1 existed in old Event - strip it out
  282.               Event olde = new Event(src, me.getWhen(), newid,
  283.                                me.getX(), me.getY(), 0,
  284.                                (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
  285.               olde.clickCount = me.getClickCount();
  286.               return olde;
  287.  
  288.           case FocusEvent.FOCUS_GAINED:
  289.               return new Event(src, Event.GOT_FOCUS, null);
  290.  
  291.           case FocusEvent.FOCUS_LOST:
  292.               return new Event(src, Event.LOST_FOCUS, null);
  293.  
  294.           case WindowEvent.WINDOW_CLOSING:
  295.           case WindowEvent.WINDOW_ICONIFIED:
  296.           case WindowEvent.WINDOW_DEICONIFIED:
  297.               return new Event(src, newid, null);
  298.  
  299.           case ComponentEvent.COMPONENT_MOVED:
  300.               if (src instanceof Frame || src instanceof Dialog) {
  301.                   Point p = ((Component)src).getLocation();
  302.                   return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
  303.               }
  304.               break;
  305.  
  306.           case ActionEvent.ACTION_PERFORMED:
  307.               ActionEvent ae = (ActionEvent)this;
  308.               String cmd;
  309.               if (src instanceof Button) {
  310.                   cmd = ((Button)src).getLabel();
  311.               } else if (src instanceof MenuItem) {
  312.                   cmd = ((MenuItem)src).getLabel();
  313.               } else {
  314.                   cmd = ae.getActionCommand();
  315.               }
  316.               return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
  317.  
  318.           case ItemEvent.ITEM_STATE_CHANGED:
  319.               ItemEvent ie = (ItemEvent)this;
  320.               Object arg;
  321.               if (src instanceof List) {
  322.                   newid = (ie.getStateChange() == ItemEvent.SELECTED?
  323.                            Event.LIST_SELECT : Event.LIST_DESELECT);
  324.                   arg = ie.getItem();
  325.               } else {
  326.                   newid = Event.ACTION_EVENT;
  327.                   if (src instanceof Choice) {
  328.                       arg = ie.getItem();
  329.  
  330.                   } else { // Checkbox
  331.                       arg = new Boolean(ie.getStateChange() == ItemEvent.SELECTED);
  332.                   }
  333.               }
  334.               return new Event(src, newid, arg);
  335.  
  336.           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  337.               AdjustmentEvent aje = (AdjustmentEvent)this;
  338.               switch(aje.getAdjustmentType()) {
  339.                 case AdjustmentEvent.UNIT_INCREMENT:
  340.                   newid = Event.SCROLL_LINE_DOWN;
  341.                   break;
  342.                 case AdjustmentEvent.UNIT_DECREMENT:
  343.                   newid = Event.SCROLL_LINE_UP;
  344.                   break;
  345.                 case AdjustmentEvent.BLOCK_INCREMENT:
  346.                   newid = Event.SCROLL_PAGE_DOWN;
  347.                   break;
  348.                 case AdjustmentEvent.BLOCK_DECREMENT:
  349.                   newid = Event.SCROLL_PAGE_UP;
  350.                   break;
  351.                 case AdjustmentEvent.TRACK:
  352.                   newid = Event.SCROLL_ABSOLUTE;
  353.                   break;
  354.                 default:
  355.                   return null;
  356.               }
  357.               return new Event(src, newid, new Integer(aje.getValue()));
  358.  
  359.           default:
  360.         }
  361.         return null;
  362.     }
  363.  
  364.     /* Package-private method to change a KeyEvent's source to the new
  365.      * focus owner.  This method needs to be here instead of in KeyEvent
  366.      * because it should only be called from by the EventQueue.
  367.      */
  368.     void setSource(Object newSource) {
  369.         if (!(this instanceof KeyEvent)) {
  370.             throw new ClassCastException();
  371.         }
  372.         source = newSource;
  373.     }
  374.  
  375.     /**
  376.      * "Moves" any private data into "other." The data is copied, and
  377.      * any references data structures in this object are set to null.
  378.      */
  379.     void movePrivateDataInto(AWTEvent other) {
  380.     other.data = this.data;
  381.     data = 0;
  382.     }
  383.  
  384.     /**
  385.      * Copies all private data from this event into that. 
  386.      * Space is allocated for the copied data that will be
  387.      * freed when the that is finalized. Upon completion,
  388.      * this event is not changed.
  389.      */
  390.     void copyPrivateDataInto(AWTEvent that) {
  391.     // currently, the only private data is native
  392.     copyDataFieldInto(that);
  393.     }
  394.  
  395.     /**
  396.      * Copies the "data" instance variable from this event
  397.      * into that. Space is allocated for the data in that
  398.      * and must be freed at some point (normally in finalize()).
  399.      */
  400.     private native void copyDataFieldInto(AWTEvent that);
  401.  
  402.     /**
  403.      * frees any native data held by this object. Normally called
  404.      * when the object is finalized.
  405.      */
  406.     private native void freeNativeData();
  407.      
  408.     protected void finalize() throws Throwable {
  409.         freeNativeData();
  410.         super.finalize();
  411.     }
  412.  
  413. } // class AWTEvent
  414.