Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/awt/KeyboardFocusManagerPeerImpl.java
38827 views
/*1* Copyright (c) 2003, 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*/24package sun.awt;2526import java.awt.Component;27import java.awt.KeyboardFocusManager;28import java.awt.Window;29import java.awt.Canvas;30import java.awt.Scrollbar;31import java.awt.Panel;3233import java.awt.event.FocusEvent;3435import java.awt.peer.KeyboardFocusManagerPeer;36import java.awt.peer.ComponentPeer;3738import java.lang.reflect.InvocationTargetException;39import java.lang.reflect.Method;4041import sun.util.logging.PlatformLogger;4243public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {4445private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");4647private static AWTAccessor.KeyboardFocusManagerAccessor kfmAccessor =48AWTAccessor.getKeyboardFocusManagerAccessor();4950// The constants are copied from java.awt.KeyboardFocusManager51public static final int SNFH_FAILURE = 0;52public static final int SNFH_SUCCESS_HANDLED = 1;53public static final int SNFH_SUCCESS_PROCEED = 2;5455@Override56public void clearGlobalFocusOwner(Window activeWindow) {57if (activeWindow != null) {58Component focusOwner = activeWindow.getFocusOwner();59if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {60focusLog.fine("Clearing global focus owner " + focusOwner);61}62if (focusOwner != null) {63FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,64CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);65SunToolkit.postPriorityEvent(fl);66}67}68}6970/*71* WARNING: Don't call it on the Toolkit thread.72*73* Checks if the component:74* 1) accepts focus on click (in general)75* 2) may be a focus owner (in particular)76*/77@SuppressWarnings("deprecation")78public static boolean shouldFocusOnClick(Component component) {79boolean acceptFocusOnClick = false;8081// A component is generally allowed to accept focus on click82// if its peer is focusable. There're some exceptions though.838485// CANVAS & SCROLLBAR accept focus on click86if (component instanceof Canvas ||87component instanceof Scrollbar)88{89acceptFocusOnClick = true;9091// PANEL, empty only, accepts focus on click92} else if (component instanceof Panel) {93acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);949596// Other components97} else {98ComponentPeer peer = (component != null ? component.getPeer() : null);99acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);100}101return acceptFocusOnClick &&102AWTAccessor.getComponentAccessor().canBeFocusOwner(component);103}104105/*106* Posts proper lost/gain focus events to the event queue.107*/108@SuppressWarnings("deprecation")109public static boolean deliverFocus(Component lightweightChild,110Component target,111boolean temporary,112boolean focusedWindowChangeAllowed,113long time,114CausedFocusEvent.Cause cause,115Component currentFocusOwner) // provided by the descendant peers116{117if (lightweightChild == null) {118lightweightChild = target;119}120121Component currentOwner = currentFocusOwner;122if (currentOwner != null && currentOwner.getPeer() == null) {123currentOwner = null;124}125if (currentOwner != null) {126FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,127false, lightweightChild, cause);128129if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {130focusLog.finer("Posting focus event: " + fl);131}132SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);133}134135FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,136false, currentOwner, cause);137138if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {139focusLog.finer("Posting focus event: " + fg);140}141SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);142return true;143}144145// WARNING: Don't call it on the Toolkit thread.146public static boolean requestFocusFor(Component target, CausedFocusEvent.Cause cause) {147return AWTAccessor.getComponentAccessor().requestFocus(target, cause);148}149150// WARNING: Don't call it on the Toolkit thread.151public static int shouldNativelyFocusHeavyweight(Component heavyweight,152Component descendant,153boolean temporary,154boolean focusedWindowChangeAllowed,155long time,156CausedFocusEvent.Cause cause)157{158return kfmAccessor.shouldNativelyFocusHeavyweight(159heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);160}161162public static void removeLastFocusRequest(Component heavyweight) {163kfmAccessor.removeLastFocusRequest(heavyweight);164}165166// WARNING: Don't call it on the Toolkit thread.167public static boolean processSynchronousLightweightTransfer(Component heavyweight,168Component descendant,169boolean temporary,170boolean focusedWindowChangeAllowed,171long time)172{173return kfmAccessor.processSynchronousLightweightTransfer(174heavyweight, descendant, temporary, focusedWindowChangeAllowed, time);175}176}177178179