0N/A/*
2362N/A * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.awt;
0N/A
0N/Aimport java.awt.Component;
0N/Aimport java.awt.KeyboardFocusManager;
0N/Aimport java.awt.Window;
1058N/Aimport java.awt.Canvas;
1058N/Aimport java.awt.Scrollbar;
1058N/Aimport java.awt.Panel;
1058N/A
1058N/Aimport java.awt.event.FocusEvent;
0N/A
0N/Aimport java.awt.peer.KeyboardFocusManagerPeer;
1058N/Aimport java.awt.peer.ComponentPeer;
0N/A
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.lang.reflect.Method;
0N/A
1696N/Aimport sun.util.logging.PlatformLogger;
0N/A
1058N/Apublic abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManagerPeer {
1058N/A
1696N/A private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.focus.KeyboardFocusManagerPeerImpl");
1058N/A
1058N/A private static AWTAccessor.KeyboardFocusManagerAccessor kfmAccessor =
1058N/A AWTAccessor.getKeyboardFocusManagerAccessor();
0N/A
1058N/A // The constants are copied from java.awt.KeyboardFocusManager
1058N/A public static final int SNFH_FAILURE = 0;
1058N/A public static final int SNFH_SUCCESS_HANDLED = 1;
1058N/A public static final int SNFH_SUCCESS_PROCEED = 2;
1058N/A
1058N/A @Override
1058N/A public void clearGlobalFocusOwner(Window activeWindow) {
1058N/A if (activeWindow != null) {
1058N/A Component focusOwner = activeWindow.getFocusOwner();
1696N/A if (focusLog.isLoggable(PlatformLogger.FINE))
1696N/A focusLog.fine("Clearing global focus owner " + focusOwner);
1058N/A if (focusOwner != null) {
1058N/A FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
1058N/A CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
1058N/A SunToolkit.postPriorityEvent(fl);
1058N/A }
1058N/A }
0N/A }
0N/A
1058N/A /*
1058N/A * WARNING: Don't call it on the Toolkit thread.
1058N/A *
1058N/A * Checks if the component:
1058N/A * 1) accepts focus on click (in general)
1058N/A * 2) may be a focus owner (in particular)
1058N/A */
1058N/A public static boolean shouldFocusOnClick(Component component) {
1058N/A boolean acceptFocusOnClick = false;
1058N/A
1058N/A // A component is generally allowed to accept focus on click
1058N/A // if its peer is focusable. There're some exceptions though.
1058N/A
1058N/A
1058N/A // CANVAS & SCROLLBAR accept focus on click
1058N/A if (component instanceof Canvas ||
1058N/A component instanceof Scrollbar)
1058N/A {
1058N/A acceptFocusOnClick = true;
1058N/A
1058N/A // PANEL, empty only, accepts focus on click
1058N/A } else if (component instanceof Panel) {
1058N/A acceptFocusOnClick = (((Panel)component).getComponentCount() == 0);
1058N/A
1058N/A
1058N/A // Other components
1058N/A } else {
1058N/A ComponentPeer peer = (component != null ? component.getPeer() : null);
1058N/A acceptFocusOnClick = (peer != null ? peer.isFocusable() : false);
1058N/A }
1058N/A return acceptFocusOnClick &&
1058N/A AWTAccessor.getComponentAccessor().canBeFocusOwner(component);
0N/A }
0N/A
1058N/A /*
1058N/A * Posts proper lost/gain focus events to the event queue.
1058N/A */
1058N/A public static boolean deliverFocus(Component lightweightChild,
1058N/A Component target,
1058N/A boolean temporary,
1058N/A boolean focusedWindowChangeAllowed,
1058N/A long time,
1058N/A CausedFocusEvent.Cause cause,
1058N/A Component currentFocusOwner) // provided by the descendant peers
1058N/A {
1058N/A if (lightweightChild == null) {
1058N/A lightweightChild = (Component)target;
1058N/A }
1058N/A
1058N/A Component currentOwner = currentFocusOwner;
1058N/A if (currentOwner != null && currentOwner.getPeer() == null) {
1058N/A currentOwner = null;
1058N/A }
1058N/A if (currentOwner != null) {
1058N/A FocusEvent fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST,
1058N/A false, lightweightChild, cause);
1058N/A
1696N/A if (focusLog.isLoggable(PlatformLogger.FINER))
1696N/A focusLog.finer("Posting focus event: " + fl);
1058N/A SunToolkit.postPriorityEvent(fl);
1058N/A }
1058N/A
1058N/A FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED,
1058N/A false, currentOwner, cause);
1058N/A
1696N/A if (focusLog.isLoggable(PlatformLogger.FINER))
1696N/A focusLog.finer("Posting focus event: " + fg);
1058N/A SunToolkit.postPriorityEvent(fg);
1058N/A return true;
0N/A }
0N/A
1058N/A // WARNING: Don't call it on the Toolkit thread.
1058N/A public static boolean requestFocusFor(Component target, CausedFocusEvent.Cause cause) {
1058N/A return AWTAccessor.getComponentAccessor().requestFocus(target, cause);
1058N/A }
1058N/A
1058N/A // WARNING: Don't call it on the Toolkit thread.
1058N/A public static int shouldNativelyFocusHeavyweight(Component heavyweight,
1058N/A Component descendant,
1058N/A boolean temporary,
1058N/A boolean focusedWindowChangeAllowed,
1058N/A long time,
1058N/A CausedFocusEvent.Cause cause)
1058N/A {
1058N/A return kfmAccessor.shouldNativelyFocusHeavyweight(
1058N/A heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);
1058N/A }
1058N/A
0N/A public static void removeLastFocusRequest(Component heavyweight) {
1058N/A kfmAccessor.removeLastFocusRequest(heavyweight);
1058N/A }
1058N/A
1058N/A // WARNING: Don't call it on the Toolkit thread.
1058N/A public static boolean processSynchronousLightweightTransfer(Component heavyweight,
1058N/A Component descendant,
1058N/A boolean temporary,
1058N/A boolean focusedWindowChangeAllowed,
1058N/A long time)
1058N/A {
1058N/A return kfmAccessor.processSynchronousLightweightTransfer(
1058N/A heavyweight, descendant, temporary, focusedWindowChangeAllowed, time);
0N/A }
0N/A}