Path: blob/master/src/java.desktop/share/classes/java/awt/AWTEvent.java
41152 views
/*1* Copyright (c) 1996, 2021, 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.awt.event.ActionEvent;28import java.awt.event.AdjustmentEvent;29import java.awt.event.ComponentEvent;30import java.awt.event.FocusEvent;31import java.awt.event.InputEvent;32import java.awt.event.InputMethodEvent;33import java.awt.event.ItemEvent;34import java.awt.event.KeyEvent;35import java.awt.event.MouseEvent;36import java.awt.event.TextEvent;37import java.awt.event.WindowEvent;38import java.awt.peer.ComponentPeer;39import java.awt.peer.LightweightPeer;40import java.io.Serial;41import java.security.AccessControlContext;42import java.security.AccessController;43import java.util.EventObject;4445import sun.awt.AWTAccessor;4647/**48* The root event class for all AWT events.49* This class and its subclasses supersede the original50* java.awt.Event class.51* Subclasses of this root AWTEvent class defined outside of the52* java.awt.event package should define event ID values greater than53* the value defined by RESERVED_ID_MAX.54* <p>55* The event masks defined in this class are needed by Component subclasses56* which are using Component.enableEvents() to select for event types not57* selected by registered listeners. If a listener is registered on a58* component, the appropriate event mask is already set internally by the59* component.60* <p>61* The masks are also used to specify to which types of events an62* AWTEventListener should listen. The masks are bitwise-ORed together63* and passed to Toolkit.addAWTEventListener.64*65* @see Component#enableEvents66* @see Toolkit#addAWTEventListener67*68* @see java.awt.event.ActionEvent69* @see java.awt.event.AdjustmentEvent70* @see java.awt.event.ComponentEvent71* @see java.awt.event.ContainerEvent72* @see java.awt.event.FocusEvent73* @see java.awt.event.InputMethodEvent74* @see java.awt.event.InvocationEvent75* @see java.awt.event.ItemEvent76* @see java.awt.event.HierarchyEvent77* @see java.awt.event.KeyEvent78* @see java.awt.event.MouseEvent79* @see java.awt.event.MouseWheelEvent80* @see java.awt.event.PaintEvent81* @see java.awt.event.TextEvent82* @see java.awt.event.WindowEvent83*84* @author Carl Quinn85* @author Amy Fowler86* @since 1.187*/88public abstract class AWTEvent extends EventObject {8990/**91* The private data.92*/93private byte[] bdata;9495/**96* The event's id.97* @serial98* @see #getID()99* @see #AWTEvent100*/101protected int id;102103/**104* Controls whether or not the event is sent back down to the peer once the105* source has processed it - false means it's sent to the peer; true means106* it's not. Semantic events always have a 'true' value since they were107* generated by the peer in response to a low-level event.108* @serial109* @see #consume110* @see #isConsumed111*/112protected boolean consumed = false;113114/*115* The event's AccessControlContext.116*/117@SuppressWarnings("removal")118private transient volatile AccessControlContext acc =119AccessController.getContext();120121/*122* Returns the acc this event was constructed with.123*/124@SuppressWarnings("removal")125final AccessControlContext getAccessControlContext() {126if (acc == null) {127throw new SecurityException("AWTEvent is missing AccessControlContext");128}129return acc;130}131132transient boolean focusManagerIsDispatching = false;133transient boolean isPosted;134135/**136* Indicates whether this AWTEvent was generated by the system as137* opposed to by user code.138*/139private transient boolean isSystemGenerated;140141/**142* The event mask for selecting component events.143*/144public static final long COMPONENT_EVENT_MASK = 0x01;145146/**147* The event mask for selecting container events.148*/149public static final long CONTAINER_EVENT_MASK = 0x02;150151/**152* The event mask for selecting focus events.153*/154public static final long FOCUS_EVENT_MASK = 0x04;155156/**157* The event mask for selecting key events.158*/159public static final long KEY_EVENT_MASK = 0x08;160161/**162* The event mask for selecting mouse events.163*/164public static final long MOUSE_EVENT_MASK = 0x10;165166/**167* The event mask for selecting mouse motion events.168*/169public static final long MOUSE_MOTION_EVENT_MASK = 0x20;170171/**172* The event mask for selecting window events.173*/174public static final long WINDOW_EVENT_MASK = 0x40;175176/**177* The event mask for selecting action events.178*/179public static final long ACTION_EVENT_MASK = 0x80;180181/**182* The event mask for selecting adjustment events.183*/184public static final long ADJUSTMENT_EVENT_MASK = 0x100;185186/**187* The event mask for selecting item events.188*/189public static final long ITEM_EVENT_MASK = 0x200;190191/**192* The event mask for selecting text events.193*/194public static final long TEXT_EVENT_MASK = 0x400;195196/**197* The event mask for selecting input method events.198*/199public static final long INPUT_METHOD_EVENT_MASK = 0x800;200201/**202* The pseudo event mask for enabling input methods.203* We're using one bit in the eventMask so we don't need204* a separate field inputMethodsEnabled.205*/206static final long INPUT_METHODS_ENABLED_MASK = 0x1000;207208/**209* The event mask for selecting paint events.210*/211public static final long PAINT_EVENT_MASK = 0x2000;212213/**214* The event mask for selecting invocation events.215*/216public static final long INVOCATION_EVENT_MASK = 0x4000;217218/**219* The event mask for selecting hierarchy events.220*/221public static final long HIERARCHY_EVENT_MASK = 0x8000;222223/**224* The event mask for selecting hierarchy bounds events.225*/226public static final long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;227228/**229* The event mask for selecting mouse wheel events.230* @since 1.4231*/232public static final long MOUSE_WHEEL_EVENT_MASK = 0x20000;233234/**235* The event mask for selecting window state events.236* @since 1.4237*/238public static final long WINDOW_STATE_EVENT_MASK = 0x40000;239240/**241* The event mask for selecting window focus events.242* @since 1.4243*/244public static final long WINDOW_FOCUS_EVENT_MASK = 0x80000;245246/**247* WARNING: there are more mask defined privately. See248* SunToolkit.GRAB_EVENT_MASK.249*/250251/**252* The maximum value for reserved AWT event IDs. Programs defining253* their own event IDs should use IDs greater than this value.254*/255public static final int RESERVED_ID_MAX = 1999;256257/**258* Use serialVersionUID from JDK 1.1 for interoperability.259*/260@Serial261private static final long serialVersionUID = -1825314779160409405L;262263static {264/* ensure that the necessary native libraries are loaded */265Toolkit.loadLibraries();266if (!GraphicsEnvironment.isHeadless()) {267initIDs();268}269AWTAccessor.setAWTEventAccessor(270new AWTAccessor.AWTEventAccessor() {271public void setPosted(AWTEvent ev) {272ev.isPosted = true;273}274275public void setSystemGenerated(AWTEvent ev) {276ev.isSystemGenerated = true;277}278279public boolean isSystemGenerated(AWTEvent ev) {280return ev.isSystemGenerated;281}282283@SuppressWarnings("removal")284public AccessControlContext getAccessControlContext(AWTEvent ev) {285return ev.getAccessControlContext();286}287288public byte[] getBData(AWTEvent ev) {289return ev.bdata;290}291292public void setBData(AWTEvent ev, byte[] bdata) {293ev.bdata = bdata;294}295296});297}298299/**300* Initialize JNI field and method IDs for fields that may be301* accessed from C.302*/303private static native void initIDs();304305/**306* Constructs an AWTEvent object from the parameters of a 1.0-style event.307*308* @param event the old-style event309* @deprecated It is recommended that {@link #AWTEvent(Object, int)} be used310* instead311*/312@Deprecated(since = "9")313public AWTEvent(Event event) {314this(event.target, event.id);315}316317/**318* Constructs an AWTEvent object with the specified source object and type.319*320* @param source the object where the event originated321* @param id the event type322*/323public AWTEvent(Object source, int id) {324super(source);325this.id = id;326switch(id) {327case ActionEvent.ACTION_PERFORMED:328case ItemEvent.ITEM_STATE_CHANGED:329case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:330case TextEvent.TEXT_VALUE_CHANGED:331consumed = true;332break;333default:334}335}336337/**338* Retargets an event to a new source. This method is typically used to339* retarget an event to a lightweight child Component of the original340* heavyweight source.341* <p>342* This method is intended to be used only by event targeting subsystems,343* such as client-defined KeyboardFocusManagers. It is not for general344* client use.345*346* @param newSource the new Object to which the event should be dispatched347* @since 1.4348*/349public void setSource(Object newSource) {350if (source == newSource) {351return;352}353354Component comp = null;355if (newSource instanceof Component) {356comp = (Component)newSource;357while (comp != null && comp.peer != null &&358(comp.peer instanceof LightweightPeer)) {359comp = comp.parent;360}361}362363synchronized (this) {364source = newSource;365if (comp != null) {366ComponentPeer peer = comp.peer;367if (peer != null) {368nativeSetSource(peer);369}370}371}372}373374private native void nativeSetSource(ComponentPeer peer);375376/**377* Returns the event type.378*379* @return the event's type id380*/381public int getID() {382return id;383}384385/**386* Returns a String representation of this object.387*/388public String toString() {389String srcName = null;390if (source instanceof Component) {391srcName = ((Component)source).getName();392} else if (source instanceof MenuComponent) {393srcName = ((MenuComponent)source).getName();394}395return getClass().getName() + "[" + paramString() + "] on " +396(srcName != null? srcName : source);397}398399/**400* Returns a string representing the state of this {@code Event}.401* This method is intended to be used only for debugging purposes, and the402* content and format of the returned string may vary between403* implementations. The returned string may be empty but may not be404* {@code null}.405*406* @return a string representation of this event407*/408public String paramString() {409return "";410}411412/**413* Consumes this event, if this event can be consumed. Only low-level,414* system events can be consumed415*/416protected void consume() {417switch(id) {418case KeyEvent.KEY_PRESSED:419case KeyEvent.KEY_RELEASED:420case MouseEvent.MOUSE_PRESSED:421case MouseEvent.MOUSE_RELEASED:422case MouseEvent.MOUSE_MOVED:423case MouseEvent.MOUSE_DRAGGED:424case MouseEvent.MOUSE_ENTERED:425case MouseEvent.MOUSE_EXITED:426case MouseEvent.MOUSE_WHEEL:427case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:428case InputMethodEvent.CARET_POSITION_CHANGED:429consumed = true;430break;431default:432// event type cannot be consumed433}434}435436/**437* Returns whether this event has been consumed.438*439* @return {@code true} if this event has been consumed;440* otherwise {@code false}441*/442protected boolean isConsumed() {443return consumed;444}445446/**447* Converts a new event to an old one (used for compatibility).448* If the new event cannot be converted (because no old equivalent449* exists) then this returns null.450*451* Note: this method is here instead of in each individual new452* event class in java.awt.event because we don't want to make453* it public and it needs to be called from java.awt.454*/455@SuppressWarnings("deprecation")456Event convertToOld() {457Object src = getSource();458int newid = id;459460switch(id) {461case KeyEvent.KEY_PRESSED:462case KeyEvent.KEY_RELEASED:463KeyEvent ke = (KeyEvent)this;464if (ke.isActionKey()) {465newid = (id == KeyEvent.KEY_PRESSED?466Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);467}468int keyCode = ke.getKeyCode();469if (keyCode == KeyEvent.VK_SHIFT ||470keyCode == KeyEvent.VK_CONTROL ||471keyCode == KeyEvent.VK_ALT) {472return null; // suppress modifier keys in old event model.473}474// no mask for button1 existed in old Event - strip it out475return new Event(src, ke.getWhen(), newid, 0, 0,476Event.getOldEventKey(ke),477(ke.getModifiers() & ~InputEvent.BUTTON1_MASK));478479case MouseEvent.MOUSE_PRESSED:480case MouseEvent.MOUSE_RELEASED:481case MouseEvent.MOUSE_MOVED:482case MouseEvent.MOUSE_DRAGGED:483case MouseEvent.MOUSE_ENTERED:484case MouseEvent.MOUSE_EXITED:485MouseEvent me = (MouseEvent)this;486// no mask for button1 existed in old Event - strip it out487Event olde = new Event(src, me.getWhen(), newid,488me.getX(), me.getY(), 0,489(me.getModifiers() & ~InputEvent.BUTTON1_MASK));490olde.clickCount = me.getClickCount();491return olde;492493case FocusEvent.FOCUS_GAINED:494return new Event(src, Event.GOT_FOCUS, null);495496case FocusEvent.FOCUS_LOST:497return new Event(src, Event.LOST_FOCUS, null);498499case WindowEvent.WINDOW_CLOSING:500case WindowEvent.WINDOW_ICONIFIED:501case WindowEvent.WINDOW_DEICONIFIED:502return new Event(src, newid, null);503504case ComponentEvent.COMPONENT_MOVED:505if (src instanceof Frame || src instanceof Dialog) {506Point p = ((Component)src).getLocation();507return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);508}509break;510511case ActionEvent.ACTION_PERFORMED:512ActionEvent ae = (ActionEvent)this;513String cmd;514if (src instanceof Button) {515cmd = ((Button)src).getLabel();516} else if (src instanceof MenuItem) {517cmd = ((MenuItem)src).getLabel();518} else {519cmd = ae.getActionCommand();520}521return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);522523case ItemEvent.ITEM_STATE_CHANGED:524ItemEvent ie = (ItemEvent)this;525Object arg;526if (src instanceof List) {527newid = (ie.getStateChange() == ItemEvent.SELECTED?528Event.LIST_SELECT : Event.LIST_DESELECT);529arg = ie.getItem();530} else {531newid = Event.ACTION_EVENT;532if (src instanceof Choice) {533arg = ie.getItem();534535} else { // Checkbox536arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED);537}538}539return new Event(src, newid, arg);540541case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:542AdjustmentEvent aje = (AdjustmentEvent)this;543switch(aje.getAdjustmentType()) {544case AdjustmentEvent.UNIT_INCREMENT:545newid = Event.SCROLL_LINE_DOWN;546break;547case AdjustmentEvent.UNIT_DECREMENT:548newid = Event.SCROLL_LINE_UP;549break;550case AdjustmentEvent.BLOCK_INCREMENT:551newid = Event.SCROLL_PAGE_DOWN;552break;553case AdjustmentEvent.BLOCK_DECREMENT:554newid = Event.SCROLL_PAGE_UP;555break;556case AdjustmentEvent.TRACK:557if (aje.getValueIsAdjusting()) {558newid = Event.SCROLL_ABSOLUTE;559}560else {561newid = Event.SCROLL_END;562}563break;564default:565return null;566}567return new Event(src, newid, Integer.valueOf(aje.getValue()));568569default:570}571return null;572}573574/**575* Copies all private data from this event into that.576* Space is allocated for the copied data that will be577* freed when the that is finalized. Upon completion,578* this event is not changed.579*/580void copyPrivateDataInto(AWTEvent that) {581that.bdata = this.bdata;582// Copy canAccessSystemClipboard value from this into that.583if (this instanceof InputEvent && that instanceof InputEvent) {584585AWTAccessor.InputEventAccessor accessor586= AWTAccessor.getInputEventAccessor();587588boolean b = accessor.canAccessSystemClipboard((InputEvent) this);589accessor.setCanAccessSystemClipboard((InputEvent) that, b);590}591that.isSystemGenerated = this.isSystemGenerated;592}593594void dispatched() {595if (this instanceof InputEvent) {596AWTAccessor.getInputEventAccessor().597setCanAccessSystemClipboard((InputEvent) this, false);598}599}600} // class AWTEvent601602603