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