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

    1   /*
    2    * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package java.awt;
   27   
   28   import java.util.EventObject;
   29   import java.awt.event;
   30   import java.awt.peer.ComponentPeer;
   31   import java.awt.peer.LightweightPeer;
   32   import java.lang.reflect.Field;
   33   import sun.awt.AWTAccessor;
   34   import sun.util.logging.PlatformLogger;
   35   
   36   import java.security.AccessControlContext;
   37   import java.security.AccessController;
   38   import java.io.ObjectInputStream;
   39   import java.io.IOException;
   40   
   41   /**
   42    * The root event class for all AWT events.
   43    * This class and its subclasses supercede the original
   44    * java.awt.Event class.
   45    * Subclasses of this root AWTEvent class defined outside of the
   46    * java.awt.event package should define event ID values greater than
   47    * the value defined by RESERVED_ID_MAX.
   48    * <p>
   49    * The event masks defined in this class are needed by Component subclasses
   50    * which are using Component.enableEvents() to select for event types not
   51    * selected by registered listeners. If a listener is registered on a
   52    * component, the appropriate event mask is already set internally by the
   53    * component.
   54    * <p>
   55    * The masks are also used to specify to which types of events an
   56    * AWTEventListener should listen. The masks are bitwise-ORed together
   57    * and passed to Toolkit.addAWTEventListener.
   58    *
   59    * @see Component#enableEvents
   60    * @see Toolkit#addAWTEventListener
   61    *
   62    * @see java.awt.event.ActionEvent
   63    * @see java.awt.event.AdjustmentEvent
   64    * @see java.awt.event.ComponentEvent
   65    * @see java.awt.event.ContainerEvent
   66    * @see java.awt.event.FocusEvent
   67    * @see java.awt.event.InputMethodEvent
   68    * @see java.awt.event.InvocationEvent
   69    * @see java.awt.event.ItemEvent
   70    * @see java.awt.event.HierarchyEvent
   71    * @see java.awt.event.KeyEvent
   72    * @see java.awt.event.MouseEvent
   73    * @see java.awt.event.MouseWheelEvent
   74    * @see java.awt.event.PaintEvent
   75    * @see java.awt.event.TextEvent
   76    * @see java.awt.event.WindowEvent
   77    *
   78    * @author Carl Quinn
   79    * @author Amy Fowler
   80    * @since 1.1
   81    */
   82   public abstract class AWTEvent extends EventObject {
   83       private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.AWTEvent");
   84       private byte bdata[];
   85   
   86       /**
   87        * The event's id.
   88        * @serial
   89        * @see #getID()
   90        * @see #AWTEvent
   91        */
   92       protected int id;
   93   
   94       /**
   95        * Controls whether or not the event is sent back down to the peer once the
   96        * source has processed it - false means it's sent to the peer; true means
   97        * it's not. Semantic events always have a 'true' value since they were
   98        * generated by the peer in response to a low-level event.
   99        * @serial
  100        * @see #consume
  101        * @see #isConsumed
  102        */
  103       protected boolean consumed = false;
  104   
  105      /*
  106       * The event's AccessControlContext.
  107       */
  108       private transient volatile AccessControlContext acc =
  109           AccessController.getContext();
  110   
  111      /*
  112       * Returns the acc this event was constructed with.
  113       */
  114       final AccessControlContext getAccessControlContext() {
  115           if (acc == null) {
  116               throw new SecurityException("AWTEvent is missing AccessControlContext");
  117           }
  118           return acc;
  119       }
  120   
  121       transient boolean focusManagerIsDispatching = false;
  122       transient boolean isPosted;
  123   
  124       /**
  125        * Indicates whether this AWTEvent was generated by the system as
  126        * opposed to by user code.
  127        */
  128       private transient boolean isSystemGenerated;
  129   
  130       /**
  131        * The event mask for selecting component events.
  132        */
  133       public final static long COMPONENT_EVENT_MASK = 0x01;
  134   
  135       /**
  136        * The event mask for selecting container events.
  137        */
  138       public final static long CONTAINER_EVENT_MASK = 0x02;
  139   
  140       /**
  141        * The event mask for selecting focus events.
  142        */
  143       public final static long FOCUS_EVENT_MASK = 0x04;
  144   
  145       /**
  146        * The event mask for selecting key events.
  147        */
  148       public final static long KEY_EVENT_MASK = 0x08;
  149   
  150       /**
  151        * The event mask for selecting mouse events.
  152        */
  153       public final static long MOUSE_EVENT_MASK = 0x10;
  154   
  155       /**
  156        * The event mask for selecting mouse motion events.
  157        */
  158       public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
  159   
  160       /**
  161        * The event mask for selecting window events.
  162        */
  163       public final static long WINDOW_EVENT_MASK = 0x40;
  164   
  165       /**
  166        * The event mask for selecting action events.
  167        */
  168       public final static long ACTION_EVENT_MASK = 0x80;
  169   
  170       /**
  171        * The event mask for selecting adjustment events.
  172        */
  173       public final static long ADJUSTMENT_EVENT_MASK = 0x100;
  174   
  175       /**
  176        * The event mask for selecting item events.
  177        */
  178       public final static long ITEM_EVENT_MASK = 0x200;
  179   
  180       /**
  181        * The event mask for selecting text events.
  182        */
  183       public final static long TEXT_EVENT_MASK = 0x400;
  184   
  185       /**
  186        * The event mask for selecting input method events.
  187        */
  188       public final static long INPUT_METHOD_EVENT_MASK = 0x800;
  189   
  190       /**
  191        * The pseudo event mask for enabling input methods.
  192        * We're using one bit in the eventMask so we don't need
  193        * a separate field inputMethodsEnabled.
  194        */
  195       final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
  196   
  197       /**
  198        * The event mask for selecting paint events.
  199        */
  200       public final static long PAINT_EVENT_MASK = 0x2000;
  201   
  202       /**
  203        * The event mask for selecting invocation events.
  204        */
  205       public final static long INVOCATION_EVENT_MASK = 0x4000;
  206   
  207       /**
  208        * The event mask for selecting hierarchy events.
  209        */
  210       public final static long HIERARCHY_EVENT_MASK = 0x8000;
  211   
  212       /**
  213        * The event mask for selecting hierarchy bounds events.
  214        */
  215       public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
  216   
  217       /**
  218        * The event mask for selecting mouse wheel events.
  219        * @since 1.4
  220        */
  221       public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;
  222   
  223       /**
  224        * The event mask for selecting window state events.
  225        * @since 1.4
  226        */
  227       public final static long WINDOW_STATE_EVENT_MASK = 0x40000;
  228   
  229       /**
  230        * The event mask for selecting window focus events.
  231        * @since 1.4
  232        */
  233       public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;
  234   
  235       /**
  236        * WARNING: there are more mask defined privately.  See
  237        * SunToolkit.GRAB_EVENT_MASK.
  238        */
  239   
  240       /**
  241        * The maximum value for reserved AWT event IDs. Programs defining
  242        * their own event IDs should use IDs greater than this value.
  243        */
  244       public final static int RESERVED_ID_MAX = 1999;
  245   
  246       // security stuff
  247       private static Field inputEvent_CanAccessSystemClipboard_Field = null;
  248   
  249       /*
  250        * JDK 1.1 serialVersionUID
  251        */
  252       private static final long serialVersionUID = -1825314779160409405L;
  253   
  254       static {
  255           /* ensure that the necessary native libraries are loaded */
  256           Toolkit.loadLibraries();
  257           if (!GraphicsEnvironment.isHeadless()) {
  258               initIDs();
  259           }
  260           AWTAccessor.setAWTEventAccessor(
  261               new AWTAccessor.AWTEventAccessor() {
  262                   public void setPosted(AWTEvent ev) {
  263                       ev.isPosted = true;
  264                   }
  265                   public void setSystemGenerated(AWTEvent ev) {
  266                       ev.isSystemGenerated = true;
  267                   }
  268                   public boolean isSystemGenerated(AWTEvent ev) {
  269                       return ev.isSystemGenerated;
  270                   }
  271   
  272                   public AccessControlContext getAccessControlContext(AWTEvent ev) {
  273                       return ev.getAccessControlContext();
  274                   }
  275               });
  276       }
  277   
  278       private static synchronized Field get_InputEvent_CanAccessSystemClipboard() {
  279           if (inputEvent_CanAccessSystemClipboard_Field == null) {
  280               inputEvent_CanAccessSystemClipboard_Field =
  281                   (Field)java.security.AccessController.doPrivileged(
  282                       new java.security.PrivilegedAction() {
  283                               public Object run() {
  284                                   Field field = null;
  285                                   try {
  286                                       field = InputEvent.class.
  287                                           getDeclaredField("canAccessSystemClipboard");
  288                                       field.setAccessible(true);
  289                                       return field;
  290                                   } catch (SecurityException e) {
  291                                       if (log.isLoggable(PlatformLogger.FINE)) {
  292                                           log.fine("AWTEvent.get_InputEvent_CanAccessSystemClipboard() got SecurityException ", e);
  293                                       }
  294                                   } catch (NoSuchFieldException e) {
  295                                       if (log.isLoggable(PlatformLogger.FINE)) {
  296                                           log.fine("AWTEvent.get_InputEvent_CanAccessSystemClipboard() got NoSuchFieldException ", e);
  297                                       }
  298                                   }
  299                                   return null;
  300                               }
  301                           });
  302           }
  303   
  304           return inputEvent_CanAccessSystemClipboard_Field;
  305       }
  306   
  307       /**
  308        * Initialize JNI field and method IDs for fields that may be
  309        * accessed from C.
  310        */
  311       private static native void initIDs();
  312   
  313       /**
  314        * Constructs an AWTEvent object from the parameters of a 1.0-style event.
  315        * @param event the old-style event
  316        */
  317       public AWTEvent(Event event) {
  318           this(event.target, event.id);
  319       }
  320   
  321       /**
  322        * Constructs an AWTEvent object with the specified source object and type.
  323        *
  324        * @param source the object where the event originated
  325        * @param id the event type
  326        */
  327       public AWTEvent(Object source, int id) {
  328           super(source);
  329           this.id = id;
  330           switch(id) {
  331             case ActionEvent.ACTION_PERFORMED:
  332             case ItemEvent.ITEM_STATE_CHANGED:
  333             case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  334             case TextEvent.TEXT_VALUE_CHANGED:
  335               consumed = true;
  336               break;
  337             default:
  338           }
  339       }
  340   
  341       /**
  342        * Retargets an event to a new source. This method is typically used to
  343        * retarget an event to a lightweight child Component of the original
  344        * heavyweight source.
  345        * <p>
  346        * This method is intended to be used only by event targeting subsystems,
  347        * such as client-defined KeyboardFocusManagers. It is not for general
  348        * client use.
  349        *
  350        * @param newSource the new Object to which the event should be dispatched
  351        * @since 1.4
  352        */
  353       public void setSource(Object newSource) {
  354           if (source == newSource) {
  355               return;
  356           }
  357   
  358           Component comp = null;
  359           if (newSource instanceof Component) {
  360               comp = (Component)newSource;
  361               while (comp != null && comp.peer != null &&
  362                      (comp.peer instanceof LightweightPeer)) {
  363                   comp = comp.parent;
  364               }
  365           }
  366   
  367           synchronized (this) {
  368               source = newSource;
  369               if (comp != null) {
  370                   ComponentPeer peer = comp.peer;
  371                   if (peer != null) {
  372                       nativeSetSource(peer);
  373                   }
  374               }
  375           }
  376       }
  377   
  378       private native void nativeSetSource(ComponentPeer peer);
  379   
  380       /**
  381        * Returns the event type.
  382        */
  383       public int getID() {
  384           return id;
  385       }
  386   
  387       /**
  388        * Returns a String representation of this object.
  389        */
  390       public String toString() {
  391           String srcName = null;
  392           if (source instanceof Component) {
  393               srcName = ((Component)source).getName();
  394           } else if (source instanceof MenuComponent) {
  395               srcName = ((MenuComponent)source).getName();
  396           }
  397           return getClass().getName() + "[" + paramString() + "] on " +
  398               (srcName != null? srcName : source);
  399       }
  400   
  401       /**
  402        * Returns a string representing the state of this <code>Event</code>.
  403        * This method is intended to be used only for debugging purposes, and the
  404        * content and format of the returned string may vary between
  405        * implementations. The returned string may be empty but may not be
  406        * <code>null</code>.
  407        *
  408        * @return  a string representation of this event
  409        */
  410       public String paramString() {
  411           return "";
  412       }
  413   
  414       /**
  415        * Consumes this event, if this event can be consumed. Only low-level,
  416        * system events can be consumed
  417        */
  418       protected void consume() {
  419           switch(id) {
  420             case KeyEvent.KEY_PRESSED:
  421             case KeyEvent.KEY_RELEASED:
  422             case MouseEvent.MOUSE_PRESSED:
  423             case MouseEvent.MOUSE_RELEASED:
  424             case MouseEvent.MOUSE_MOVED:
  425             case MouseEvent.MOUSE_DRAGGED:
  426             case MouseEvent.MOUSE_ENTERED:
  427             case MouseEvent.MOUSE_EXITED:
  428             case MouseEvent.MOUSE_WHEEL:
  429             case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  430             case InputMethodEvent.CARET_POSITION_CHANGED:
  431                 consumed = true;
  432                 break;
  433             default:
  434                 // event type cannot be consumed
  435           }
  436       }
  437   
  438       /**
  439        * Returns whether this event has been consumed.
  440        */
  441       protected boolean isConsumed() {
  442           return consumed;
  443       }
  444   
  445       /**
  446        * Converts a new event to an old one (used for compatibility).
  447        * If the new event cannot be converted (because no old equivalent
  448        * exists) then this returns null.
  449        *
  450        * Note: this method is here instead of in each individual new
  451        * event class in java.awt.event because we don't want to make
  452        * it public and it needs to be called from java.awt.
  453        */
  454       Event convertToOld() {
  455           Object src = getSource();
  456           int newid = id;
  457   
  458           switch(id) {
  459             case KeyEvent.KEY_PRESSED:
  460             case KeyEvent.KEY_RELEASED:
  461                 KeyEvent ke = (KeyEvent)this;
  462                 if (ke.isActionKey()) {
  463                     newid = (id == KeyEvent.KEY_PRESSED?
  464                              Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
  465                 }
  466                 int keyCode = ke.getKeyCode();
  467                 if (keyCode == KeyEvent.VK_SHIFT ||
  468                     keyCode == KeyEvent.VK_CONTROL ||
  469                     keyCode == KeyEvent.VK_ALT) {
  470                     return null;  // suppress modifier keys in old event model.
  471                 }
  472                 // no mask for button1 existed in old Event - strip it out
  473                 return new Event(src, ke.getWhen(), newid, 0, 0,
  474                                  Event.getOldEventKey(ke),
  475                                  (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
  476   
  477             case MouseEvent.MOUSE_PRESSED:
  478             case MouseEvent.MOUSE_RELEASED:
  479             case MouseEvent.MOUSE_MOVED:
  480             case MouseEvent.MOUSE_DRAGGED:
  481             case MouseEvent.MOUSE_ENTERED:
  482             case MouseEvent.MOUSE_EXITED:
  483                 MouseEvent me = (MouseEvent)this;
  484                 // no mask for button1 existed in old Event - strip it out
  485                 Event olde = new Event(src, me.getWhen(), newid,
  486                                  me.getX(), me.getY(), 0,
  487                                  (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
  488                 olde.clickCount = me.getClickCount();
  489                 return olde;
  490   
  491             case FocusEvent.FOCUS_GAINED:
  492                 return new Event(src, Event.GOT_FOCUS, null);
  493   
  494             case FocusEvent.FOCUS_LOST:
  495                 return new Event(src, Event.LOST_FOCUS, null);
  496   
  497             case WindowEvent.WINDOW_CLOSING:
  498             case WindowEvent.WINDOW_ICONIFIED:
  499             case WindowEvent.WINDOW_DEICONIFIED:
  500                 return new Event(src, newid, null);
  501   
  502             case ComponentEvent.COMPONENT_MOVED:
  503                 if (src instanceof Frame || src instanceof Dialog) {
  504                     Point p = ((Component)src).getLocation();
  505                     return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
  506                 }
  507                 break;
  508   
  509             case ActionEvent.ACTION_PERFORMED:
  510                 ActionEvent ae = (ActionEvent)this;
  511                 String cmd;
  512                 if (src instanceof Button) {
  513                     cmd = ((Button)src).getLabel();
  514                 } else if (src instanceof MenuItem) {
  515                     cmd = ((MenuItem)src).getLabel();
  516                 } else {
  517                     cmd = ae.getActionCommand();
  518                 }
  519                 return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
  520   
  521             case ItemEvent.ITEM_STATE_CHANGED:
  522                 ItemEvent ie = (ItemEvent)this;
  523                 Object arg;
  524                 if (src instanceof List) {
  525                     newid = (ie.getStateChange() == ItemEvent.SELECTED?
  526                              Event.LIST_SELECT : Event.LIST_DESELECT);
  527                     arg = ie.getItem();
  528                 } else {
  529                     newid = Event.ACTION_EVENT;
  530                     if (src instanceof Choice) {
  531                         arg = ie.getItem();
  532   
  533                     } else { // Checkbox
  534                         arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED);
  535                     }
  536                 }
  537                 return new Event(src, newid, arg);
  538   
  539             case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  540                 AdjustmentEvent aje = (AdjustmentEvent)this;
  541                 switch(aje.getAdjustmentType()) {
  542                   case AdjustmentEvent.UNIT_INCREMENT:
  543                     newid = Event.SCROLL_LINE_DOWN;
  544                     break;
  545                   case AdjustmentEvent.UNIT_DECREMENT:
  546                     newid = Event.SCROLL_LINE_UP;
  547                     break;
  548                   case AdjustmentEvent.BLOCK_INCREMENT:
  549                     newid = Event.SCROLL_PAGE_DOWN;
  550                     break;
  551                   case AdjustmentEvent.BLOCK_DECREMENT:
  552                     newid = Event.SCROLL_PAGE_UP;
  553                     break;
  554                   case AdjustmentEvent.TRACK:
  555                     if (aje.getValueIsAdjusting()) {
  556                         newid = Event.SCROLL_ABSOLUTE;
  557                     }
  558                     else {
  559                         newid = Event.SCROLL_END;
  560                     }
  561                     break;
  562                   default:
  563                     return null;
  564                 }
  565                 return new Event(src, newid, Integer.valueOf(aje.getValue()));
  566   
  567             default:
  568           }
  569           return null;
  570       }
  571   
  572       /**
  573        * Copies all private data from this event into that.
  574        * Space is allocated for the copied data that will be
  575        * freed when the that is finalized. Upon completion,
  576        * this event is not changed.
  577        */
  578       void copyPrivateDataInto(AWTEvent that) {
  579           that.bdata = this.bdata;
  580           // Copy canAccessSystemClipboard value from this into that.
  581           if (this instanceof InputEvent && that instanceof InputEvent) {
  582               Field field = get_InputEvent_CanAccessSystemClipboard();
  583               if (field != null) {
  584                   try {
  585                       boolean b = field.getBoolean(this);
  586                       field.setBoolean(that, b);
  587                   } catch(IllegalAccessException e) {
  588                       if (log.isLoggable(PlatformLogger.FINE)) {
  589                           log.fine("AWTEvent.copyPrivateDataInto() got IllegalAccessException ", e);
  590                       }
  591                   }
  592               }
  593           }
  594           that.isSystemGenerated = this.isSystemGenerated;
  595       }
  596   
  597       void dispatched() {
  598           if (this instanceof InputEvent) {
  599               Field field = get_InputEvent_CanAccessSystemClipboard();
  600               if (field != null) {
  601                   try {
  602                       field.setBoolean(this, false);
  603                   } catch(IllegalAccessException e) {
  604                       if (log.isLoggable(PlatformLogger.FINE)) {
  605                           log.fine("AWTEvent.dispatched() got IllegalAccessException ", e);
  606                       }
  607                   }
  608               }
  609           }
  610       }
  611   } // class AWTEvent

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