Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/AWTEvent.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.util.EventObject;28import java.awt.event.*;29import java.awt.peer.ComponentPeer;30import java.awt.peer.LightweightPeer;31import java.lang.reflect.Field;32import sun.awt.AWTAccessor;33import sun.util.logging.PlatformLogger;3435import java.security.AccessControlContext;36import java.security.AccessController;3738/**39* The root event class for all AWT events.40* This class and its subclasses supercede the original41* java.awt.Event class.42* Subclasses of this root AWTEvent class defined outside of the43* java.awt.event package should define event ID values greater than44* the value defined by RESERVED_ID_MAX.45* <p>46* The event masks defined in this class are needed by Component subclasses47* which are using Component.enableEvents() to select for event types not48* selected by registered listeners. If a listener is registered on a49* component, the appropriate event mask is already set internally by the50* component.51* <p>52* The masks are also used to specify to which types of events an53* AWTEventListener should listen. The masks are bitwise-ORed together54* and passed to Toolkit.addAWTEventListener.55*56* @see Component#enableEvents57* @see Toolkit#addAWTEventListener58*59* @see java.awt.event.ActionEvent60* @see java.awt.event.AdjustmentEvent61* @see java.awt.event.ComponentEvent62* @see java.awt.event.ContainerEvent63* @see java.awt.event.FocusEvent64* @see java.awt.event.InputMethodEvent65* @see java.awt.event.InvocationEvent66* @see java.awt.event.ItemEvent67* @see java.awt.event.HierarchyEvent68* @see java.awt.event.KeyEvent69* @see java.awt.event.MouseEvent70* @see java.awt.event.MouseWheelEvent71* @see java.awt.event.PaintEvent72* @see java.awt.event.TextEvent73* @see java.awt.event.WindowEvent74*75* @author Carl Quinn76* @author Amy Fowler77* @since 1.178*/79public abstract class AWTEvent extends EventObject {80private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.AWTEvent");81private byte bdata[];8283/**84* The event's id.85* @serial86* @see #getID()87* @see #AWTEvent88*/89protected int id;9091/**92* Controls whether or not the event is sent back down to the peer once the93* source has processed it - false means it's sent to the peer; true means94* it's not. Semantic events always have a 'true' value since they were95* generated by the peer in response to a low-level event.96* @serial97* @see #consume98* @see #isConsumed99*/100protected boolean consumed = false;101102/*103* The event's AccessControlContext.104*/105private transient volatile AccessControlContext acc =106AccessController.getContext();107108/*109* Returns the acc this event was constructed with.110*/111final AccessControlContext getAccessControlContext() {112if (acc == null) {113throw new SecurityException("AWTEvent is missing AccessControlContext");114}115return acc;116}117118transient boolean focusManagerIsDispatching = false;119transient boolean isPosted;120121/**122* Indicates whether this AWTEvent was generated by the system as123* opposed to by user code.124*/125private transient boolean isSystemGenerated;126127/**128* The event mask for selecting component events.129*/130public final static long COMPONENT_EVENT_MASK = 0x01;131132/**133* The event mask for selecting container events.134*/135public final static long CONTAINER_EVENT_MASK = 0x02;136137/**138* The event mask for selecting focus events.139*/140public final static long FOCUS_EVENT_MASK = 0x04;141142/**143* The event mask for selecting key events.144*/145public final static long KEY_EVENT_MASK = 0x08;146147/**148* The event mask for selecting mouse events.149*/150public final static long MOUSE_EVENT_MASK = 0x10;151152/**153* The event mask for selecting mouse motion events.154*/155public final static long MOUSE_MOTION_EVENT_MASK = 0x20;156157/**158* The event mask for selecting window events.159*/160public final static long WINDOW_EVENT_MASK = 0x40;161162/**163* The event mask for selecting action events.164*/165public final static long ACTION_EVENT_MASK = 0x80;166167/**168* The event mask for selecting adjustment events.169*/170public final static long ADJUSTMENT_EVENT_MASK = 0x100;171172/**173* The event mask for selecting item events.174*/175public final static long ITEM_EVENT_MASK = 0x200;176177/**178* The event mask for selecting text events.179*/180public final static long TEXT_EVENT_MASK = 0x400;181182/**183* The event mask for selecting input method events.184*/185public final static long INPUT_METHOD_EVENT_MASK = 0x800;186187/**188* The pseudo event mask for enabling input methods.189* We're using one bit in the eventMask so we don't need190* a separate field inputMethodsEnabled.191*/192final static long INPUT_METHODS_ENABLED_MASK = 0x1000;193194/**195* The event mask for selecting paint events.196*/197public final static long PAINT_EVENT_MASK = 0x2000;198199/**200* The event mask for selecting invocation events.201*/202public final static long INVOCATION_EVENT_MASK = 0x4000;203204/**205* The event mask for selecting hierarchy events.206*/207public final static long HIERARCHY_EVENT_MASK = 0x8000;208209/**210* The event mask for selecting hierarchy bounds events.211*/212public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;213214/**215* The event mask for selecting mouse wheel events.216* @since 1.4217*/218public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;219220/**221* The event mask for selecting window state events.222* @since 1.4223*/224public final static long WINDOW_STATE_EVENT_MASK = 0x40000;225226/**227* The event mask for selecting window focus events.228* @since 1.4229*/230public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;231232/**233* WARNING: there are more mask defined privately. See234* SunToolkit.GRAB_EVENT_MASK.235*/236237/**238* The maximum value for reserved AWT event IDs. Programs defining239* their own event IDs should use IDs greater than this value.240*/241public final static int RESERVED_ID_MAX = 1999;242243// security stuff244private static Field inputEvent_CanAccessSystemClipboard_Field = null;245246/*247* JDK 1.1 serialVersionUID248*/249private static final long serialVersionUID = -1825314779160409405L;250251static {252/* ensure that the necessary native libraries are loaded */253Toolkit.loadLibraries();254if (!GraphicsEnvironment.isHeadless()) {255initIDs();256}257AWTAccessor.setAWTEventAccessor(258new AWTAccessor.AWTEventAccessor() {259public void setPosted(AWTEvent ev) {260ev.isPosted = true;261}262263public void setSystemGenerated(AWTEvent ev) {264ev.isSystemGenerated = true;265}266267public boolean isSystemGenerated(AWTEvent ev) {268return ev.isSystemGenerated;269}270271public AccessControlContext getAccessControlContext(AWTEvent ev) {272return ev.getAccessControlContext();273}274275public byte[] getBData(AWTEvent ev) {276return ev.bdata;277}278279public void setBData(AWTEvent ev, byte[] bdata) {280ev.bdata = bdata;281}282283});284}285286private static synchronized Field get_InputEvent_CanAccessSystemClipboard() {287if (inputEvent_CanAccessSystemClipboard_Field == null) {288inputEvent_CanAccessSystemClipboard_Field =289java.security.AccessController.doPrivileged(290new java.security.PrivilegedAction<Field>() {291public Field run() {292Field field = null;293try {294field = InputEvent.class.295getDeclaredField("canAccessSystemClipboard");296field.setAccessible(true);297return field;298} catch (SecurityException e) {299if (log.isLoggable(PlatformLogger.Level.FINE)) {300log.fine("AWTEvent.get_InputEvent_CanAccessSystemClipboard() got SecurityException ", e);301}302} catch (NoSuchFieldException e) {303if (log.isLoggable(PlatformLogger.Level.FINE)) {304log.fine("AWTEvent.get_InputEvent_CanAccessSystemClipboard() got NoSuchFieldException ", e);305}306}307return null;308}309});310}311312return inputEvent_CanAccessSystemClipboard_Field;313}314315/**316* Initialize JNI field and method IDs for fields that may be317* accessed from C.318*/319private static native void initIDs();320321/**322* Constructs an AWTEvent object from the parameters of a 1.0-style event.323* @param event the old-style event324*/325public AWTEvent(Event event) {326this(event.target, event.id);327}328329/**330* Constructs an AWTEvent object with the specified source object and type.331*332* @param source the object where the event originated333* @param id the event type334*/335public AWTEvent(Object source, int id) {336super(source);337this.id = id;338switch(id) {339case ActionEvent.ACTION_PERFORMED:340case ItemEvent.ITEM_STATE_CHANGED:341case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:342case TextEvent.TEXT_VALUE_CHANGED:343consumed = true;344break;345default:346}347}348349/**350* Retargets an event to a new source. This method is typically used to351* retarget an event to a lightweight child Component of the original352* heavyweight source.353* <p>354* This method is intended to be used only by event targeting subsystems,355* such as client-defined KeyboardFocusManagers. It is not for general356* client use.357*358* @param newSource the new Object to which the event should be dispatched359* @since 1.4360*/361public void setSource(Object newSource) {362if (source == newSource) {363return;364}365366Component comp = null;367if (newSource instanceof Component) {368comp = (Component)newSource;369while (comp != null && comp.peer != null &&370(comp.peer instanceof LightweightPeer)) {371comp = comp.parent;372}373}374375synchronized (this) {376source = newSource;377if (comp != null) {378ComponentPeer peer = comp.peer;379if (peer != null) {380nativeSetSource(peer);381}382}383}384}385386private native void nativeSetSource(ComponentPeer peer);387388/**389* Returns the event type.390*/391public int getID() {392return id;393}394395/**396* Returns a String representation of this object.397*/398public String toString() {399String srcName = null;400if (source instanceof Component) {401srcName = ((Component)source).getName();402} else if (source instanceof MenuComponent) {403srcName = ((MenuComponent)source).getName();404}405return getClass().getName() + "[" + paramString() + "] on " +406(srcName != null? srcName : source);407}408409/**410* Returns a string representing the state of this <code>Event</code>.411* This method is intended to be used only for debugging purposes, and the412* content and format of the returned string may vary between413* implementations. The returned string may be empty but may not be414* <code>null</code>.415*416* @return a string representation of this event417*/418public String paramString() {419return "";420}421422/**423* Consumes this event, if this event can be consumed. Only low-level,424* system events can be consumed425*/426protected void consume() {427switch(id) {428case KeyEvent.KEY_PRESSED:429case KeyEvent.KEY_RELEASED:430case MouseEvent.MOUSE_PRESSED:431case MouseEvent.MOUSE_RELEASED:432case MouseEvent.MOUSE_MOVED:433case MouseEvent.MOUSE_DRAGGED:434case MouseEvent.MOUSE_ENTERED:435case MouseEvent.MOUSE_EXITED:436case MouseEvent.MOUSE_WHEEL:437case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:438case InputMethodEvent.CARET_POSITION_CHANGED:439consumed = true;440break;441default:442// event type cannot be consumed443}444}445446/**447* Returns whether this event has been consumed.448*/449protected boolean isConsumed() {450return consumed;451}452453/**454* Converts a new event to an old one (used for compatibility).455* If the new event cannot be converted (because no old equivalent456* exists) then this returns null.457*458* Note: this method is here instead of in each individual new459* event class in java.awt.event because we don't want to make460* it public and it needs to be called from java.awt.461*/462Event convertToOld() {463Object src = getSource();464int newid = id;465466switch(id) {467case KeyEvent.KEY_PRESSED:468case KeyEvent.KEY_RELEASED:469KeyEvent ke = (KeyEvent)this;470if (ke.isActionKey()) {471newid = (id == KeyEvent.KEY_PRESSED?472Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);473}474int keyCode = ke.getKeyCode();475if (keyCode == KeyEvent.VK_SHIFT ||476keyCode == KeyEvent.VK_CONTROL ||477keyCode == KeyEvent.VK_ALT) {478return null; // suppress modifier keys in old event model.479}480// no mask for button1 existed in old Event - strip it out481return new Event(src, ke.getWhen(), newid, 0, 0,482Event.getOldEventKey(ke),483(ke.getModifiers() & ~InputEvent.BUTTON1_MASK));484485case MouseEvent.MOUSE_PRESSED:486case MouseEvent.MOUSE_RELEASED:487case MouseEvent.MOUSE_MOVED:488case MouseEvent.MOUSE_DRAGGED:489case MouseEvent.MOUSE_ENTERED:490case MouseEvent.MOUSE_EXITED:491MouseEvent me = (MouseEvent)this;492// no mask for button1 existed in old Event - strip it out493Event olde = new Event(src, me.getWhen(), newid,494me.getX(), me.getY(), 0,495(me.getModifiers() & ~InputEvent.BUTTON1_MASK));496olde.clickCount = me.getClickCount();497return olde;498499case FocusEvent.FOCUS_GAINED:500return new Event(src, Event.GOT_FOCUS, null);501502case FocusEvent.FOCUS_LOST:503return new Event(src, Event.LOST_FOCUS, null);504505case WindowEvent.WINDOW_CLOSING:506case WindowEvent.WINDOW_ICONIFIED:507case WindowEvent.WINDOW_DEICONIFIED:508return new Event(src, newid, null);509510case ComponentEvent.COMPONENT_MOVED:511if (src instanceof Frame || src instanceof Dialog) {512Point p = ((Component)src).getLocation();513return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);514}515break;516517case ActionEvent.ACTION_PERFORMED:518ActionEvent ae = (ActionEvent)this;519String cmd;520if (src instanceof Button) {521cmd = ((Button)src).getLabel();522} else if (src instanceof MenuItem) {523cmd = ((MenuItem)src).getLabel();524} else {525cmd = ae.getActionCommand();526}527return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);528529case ItemEvent.ITEM_STATE_CHANGED:530ItemEvent ie = (ItemEvent)this;531Object arg;532if (src instanceof List) {533newid = (ie.getStateChange() == ItemEvent.SELECTED?534Event.LIST_SELECT : Event.LIST_DESELECT);535arg = ie.getItem();536} else {537newid = Event.ACTION_EVENT;538if (src instanceof Choice) {539arg = ie.getItem();540541} else { // Checkbox542arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED);543}544}545return new Event(src, newid, arg);546547case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:548AdjustmentEvent aje = (AdjustmentEvent)this;549switch(aje.getAdjustmentType()) {550case AdjustmentEvent.UNIT_INCREMENT:551newid = Event.SCROLL_LINE_DOWN;552break;553case AdjustmentEvent.UNIT_DECREMENT:554newid = Event.SCROLL_LINE_UP;555break;556case AdjustmentEvent.BLOCK_INCREMENT:557newid = Event.SCROLL_PAGE_DOWN;558break;559case AdjustmentEvent.BLOCK_DECREMENT:560newid = Event.SCROLL_PAGE_UP;561break;562case AdjustmentEvent.TRACK:563if (aje.getValueIsAdjusting()) {564newid = Event.SCROLL_ABSOLUTE;565}566else {567newid = Event.SCROLL_END;568}569break;570default:571return null;572}573return new Event(src, newid, Integer.valueOf(aje.getValue()));574575default:576}577return null;578}579580/**581* Copies all private data from this event into that.582* Space is allocated for the copied data that will be583* freed when the that is finalized. Upon completion,584* this event is not changed.585*/586void copyPrivateDataInto(AWTEvent that) {587that.bdata = this.bdata;588// Copy canAccessSystemClipboard value from this into that.589if (this instanceof InputEvent && that instanceof InputEvent) {590Field field = get_InputEvent_CanAccessSystemClipboard();591if (field != null) {592try {593boolean b = field.getBoolean(this);594field.setBoolean(that, b);595} catch(IllegalAccessException e) {596if (log.isLoggable(PlatformLogger.Level.FINE)) {597log.fine("AWTEvent.copyPrivateDataInto() got IllegalAccessException ", e);598}599}600}601}602that.isSystemGenerated = this.isSystemGenerated;603}604605void dispatched() {606if (this instanceof InputEvent) {607Field field = get_InputEvent_CanAccessSystemClipboard();608if (field != null) {609try {610field.setBoolean(this, false);611} catch(IllegalAccessException e) {612if (log.isLoggable(PlatformLogger.Level.FINE)) {613log.fine("AWTEvent.dispatched() got IllegalAccessException ", e);614}615}616}617}618}619} // class AWTEvent620621622