Path: blob/master/src/java.desktop/share/classes/sun/awt/CausedFocusEvent.java
41152 views
/*1* Copyright (c) 2003, 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 sun.awt;2627import java.awt.Component;28import java.awt.event.FocusEvent;29import java.io.ObjectStreamException;30import java.io.Serial;31import java.lang.reflect.Field;32import java.security.AccessController;33import java.security.PrivilegedAction;3435/**36* This class exists for deserialization compatibility only.37*/38class CausedFocusEvent extends FocusEvent {3940/**41* Use serialVersionUID from JDK 9 for interoperability.42*/43@Serial44private static final long serialVersionUID = -3647309088427840738L;4546private enum Cause {47UNKNOWN,48MOUSE_EVENT,49TRAVERSAL,50TRAVERSAL_UP,51TRAVERSAL_DOWN,52TRAVERSAL_FORWARD,53TRAVERSAL_BACKWARD,54MANUAL_REQUEST,55AUTOMATIC_TRAVERSE,56ROLLBACK,57NATIVE_SYSTEM,58ACTIVATION,59CLEAR_GLOBAL_FOCUS_OWNER,60RETARGETED;61};6263@SuppressWarnings("serial")64private static final Component dummy = new Component(){};6566private final Cause cause;6768private CausedFocusEvent(Component source, int id, boolean temporary,69Component opposite, Cause cause) {70super(source, id, temporary, opposite);71throw new IllegalStateException();72}7374@SuppressWarnings("removal")75@Serial76Object readResolve() throws ObjectStreamException {77FocusEvent.Cause newCause;78switch (cause) {79case UNKNOWN:80newCause = FocusEvent.Cause.UNKNOWN;81break;82case MOUSE_EVENT:83newCause = FocusEvent.Cause.MOUSE_EVENT;84break;85case TRAVERSAL:86newCause = FocusEvent.Cause.TRAVERSAL;87break;88case TRAVERSAL_UP:89newCause = FocusEvent.Cause.TRAVERSAL_UP;90break;91case TRAVERSAL_DOWN:92newCause = FocusEvent.Cause.TRAVERSAL_DOWN;93break;94case TRAVERSAL_FORWARD:95newCause = FocusEvent.Cause.TRAVERSAL_FORWARD;96break;97case TRAVERSAL_BACKWARD:98newCause = FocusEvent.Cause.TRAVERSAL_BACKWARD;99break;100case ROLLBACK:101newCause = FocusEvent.Cause.ROLLBACK;102break;103case NATIVE_SYSTEM:104newCause = FocusEvent.Cause.UNEXPECTED;105break;106case ACTIVATION:107newCause = FocusEvent.Cause.ACTIVATION;108break;109case CLEAR_GLOBAL_FOCUS_OWNER:110newCause = FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER;111break;112default:113newCause = FocusEvent.Cause.UNKNOWN;114}115116FocusEvent focusEvent = new FocusEvent(dummy, getID(), isTemporary(),117getOppositeComponent(), newCause);118focusEvent.setSource(null);119try {120final Field consumedField = FocusEvent.class.getField("consumed");121AccessController.doPrivileged(new PrivilegedAction<Object>() {122@Override123public Object run() {124consumedField.setAccessible(true);125try {126consumedField.set(focusEvent, consumed);127} catch (IllegalAccessException e) {128}129return null;130}131});132} catch (NoSuchFieldException e) {133}134135AWTAccessor.AWTEventAccessor accessor =136AWTAccessor.getAWTEventAccessor();137accessor.setBData(focusEvent, accessor.getBData(this));138return focusEvent;139}140}141142143