Path: blob/master/src/java.desktop/share/classes/sun/awt/KeyboardFocusManagerPeerImpl.java
41152 views
/*1* Copyright (c) 2003, 2016, 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.Window;28import java.awt.Canvas;29import java.awt.Scrollbar;30import java.awt.Panel;3132import java.awt.event.FocusEvent;3334import java.awt.peer.KeyboardFocusManagerPeer;35import java.awt.peer.ComponentPeer;3637import sun.awt.AWTAccessor.ComponentAccessor;38import sun.util.logging.PlatformLogger;3940public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {4142private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");4344private static class KfmAccessor {45private static AWTAccessor.KeyboardFocusManagerAccessor instance =46AWTAccessor.getKeyboardFocusManagerAccessor();47}4849// The constants are copied from java.awt.KeyboardFocusManager50public static final int SNFH_FAILURE = 0;51public static final int SNFH_SUCCESS_HANDLED = 1;52public static final int SNFH_SUCCESS_PROCEED = 2;5354@Override55public void clearGlobalFocusOwner(Window activeWindow) {56if (activeWindow != null) {57Component focusOwner = activeWindow.getFocusOwner();58if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {59focusLog.fine("Clearing global focus owner " + focusOwner);60}61if (focusOwner != null) {62FocusEvent fl = new FocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,63FocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);64SunToolkit.postPriorityEvent(fl);65}66}67}6869/*70* WARNING: Don't call it on the Toolkit thread.71*72* Checks if the component:73* 1) accepts focus on click (in general)74* 2) may be a focus owner (in particular)75*/76public static boolean shouldFocusOnClick(Component component) {77boolean acceptFocusOnClick = false;7879// A component is generally allowed to accept focus on click80// if its peer is focusable. There're some exceptions though.818283// CANVAS & SCROLLBAR accept focus on click84final ComponentAccessor acc = AWTAccessor.getComponentAccessor();85if (component instanceof Canvas ||86component instanceof Scrollbar)87{88acceptFocusOnClick = true;8990// PANEL, empty only, accepts focus on click91} else if (component instanceof Panel) {92acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);939495// Other components96} else {97ComponentPeer peer = (component != null ? acc.getPeer(component) : null);98acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);99}100return acceptFocusOnClick && acc.canBeFocusOwner(component);101}102103/*104* Posts proper lost/gain focus events to the event queue.105*/106@SuppressWarnings("deprecation")107public static boolean deliverFocus(Component lightweightChild,108Component target,109boolean temporary,110boolean focusedWindowChangeAllowed,111long time,112FocusEvent.Cause cause,113Component currentFocusOwner) // provided by the descendant peers114{115if (lightweightChild == null) {116lightweightChild = target;117}118119Component currentOwner = currentFocusOwner;120if (currentOwner != null && !currentOwner.isDisplayable()) {121currentOwner = null;122}123if (currentOwner != null) {124FocusEvent fl = new FocusEvent(currentOwner, FocusEvent.FOCUS_LOST,125false, lightweightChild, cause);126127if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {128focusLog.finer("Posting focus event: " + fl);129}130SunToolkit.postEvent(SunToolkit.targetToAppContext(currentOwner), fl);131}132133FocusEvent fg = new FocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,134false, currentOwner, cause);135136if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {137focusLog.finer("Posting focus event: " + fg);138}139SunToolkit.postEvent(SunToolkit.targetToAppContext(lightweightChild), fg);140return true;141}142143// WARNING: Don't call it on the Toolkit thread.144public static void requestFocusFor(Component target, FocusEvent.Cause cause) {145AWTAccessor.getComponentAccessor().requestFocus(target, cause);146}147148// WARNING: Don't call it on the Toolkit thread.149public static int shouldNativelyFocusHeavyweight(Component heavyweight,150Component descendant,151boolean temporary,152boolean focusedWindowChangeAllowed,153long time,154FocusEvent.Cause cause)155{156return KfmAccessor.instance.shouldNativelyFocusHeavyweight(157heavyweight, descendant, temporary, focusedWindowChangeAllowed,158time, cause);159}160161public static void removeLastFocusRequest(Component heavyweight) {162KfmAccessor.instance.removeLastFocusRequest(heavyweight);163}164165// WARNING: Don't call it on the Toolkit thread.166public static boolean processSynchronousLightweightTransfer(Component heavyweight,167Component descendant,168boolean temporary,169boolean focusedWindowChangeAllowed,170long time)171{172return KfmAccessor.instance.processSynchronousLightweightTransfer(173heavyweight, descendant, temporary, focusedWindowChangeAllowed,174time);175}176}177178179