package sun.awt.X11;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.peer.KeyboardFocusManagerPeer;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.awt.CausedFocusEvent;
import sun.awt.AWTAccessor;
public class XKeyboardFocusManagerPeer implements KeyboardFocusManagerPeer {
private static final Logger focusLog = Logger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
KeyboardFocusManager manager;
XKeyboardFocusManagerPeer(KeyboardFocusManager manager) {
this.manager = manager;
}
private static Object lock = new Object() {};
private static Component currentFocusOwner;
private static Window currentFocusedWindow;
static void setCurrentNativeFocusOwner(Component comp) {
if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focus owner " + comp);
synchronized(lock) {
currentFocusOwner = comp;
}
}
static void setCurrentNativeFocusedWindow(Window win) {
if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Setting current native focused window " + win);
XWindowPeer from = null, to = null;
synchronized(lock) {
if (currentFocusedWindow != null) {
from = (XWindowPeer)currentFocusedWindow.getPeer();
}
currentFocusedWindow = win;
if (currentFocusedWindow != null) {
to = (XWindowPeer)currentFocusedWindow.getPeer();
}
}
if (from != null) {
from.updateSecurityWarningVisibility();
}
if (to != null) {
to.updateSecurityWarningVisibility();
}
}
static Component getCurrentNativeFocusOwner() {
synchronized(lock) {
return currentFocusOwner;
}
}
static Window getCurrentNativeFocusedWindow() {
synchronized(lock) {
return currentFocusedWindow;
}
}
public Window getCurrentFocusedWindow() {
return getCurrentNativeFocusedWindow();
}
public void setCurrentFocusOwner(Component comp) {
setCurrentNativeFocusOwner(comp);
}
public Component getCurrentFocusOwner() {
return getCurrentNativeFocusOwner();
}
public void clearGlobalFocusOwner(Window activeWindow) {
if (activeWindow != null) {
Component focusOwner = activeWindow.getFocusOwner();
if (focusLog.isLoggable(Level.FINE)) focusLog.fine("Clearing global focus owner " + focusOwner);
if (focusOwner != null) {
FocusEvent fl = new CausedFocusEvent(focusOwner, FocusEvent.FOCUS_LOST, false, null,
CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
XWindow.sendEvent(fl);
}
}
}
static boolean simulateMotifRequestFocus(Component lightweightChild, Component target, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
{
if (lightweightChild == null) {
lightweightChild = (Component)target;
}
Component currentOwner = XKeyboardFocusManagerPeer.getCurrentNativeFocusOwner();
if (currentOwner != null && currentOwner.getPeer() == null) {
currentOwner = null;
}
if (focusLog.isLoggable(Level.FINER)) focusLog.finer("Simulating transfer from " + currentOwner + " to " + lightweightChild);
FocusEvent fg = new CausedFocusEvent(lightweightChild, FocusEvent.FOCUS_GAINED, false, currentOwner, cause);
FocusEvent fl = null;
if (currentOwner != null) {
fl = new CausedFocusEvent(currentOwner, FocusEvent.FOCUS_LOST, false, lightweightChild, cause);
}
if (fl != null) {
XWindow.sendEvent(fl);
}
XWindow.sendEvent(fg);
return true;
}
static int shouldNativelyFocusHeavyweight(Component heavyweight,
Component descendant, boolean temporary,
boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
{
return AWTAccessor.getKeyboardFocusManagerAccessor()
.shouldNativelyFocusHeavyweight(heavyweight,
descendant,
temporary,
focusedWindowChangeAllowed,
time,
cause);
}
}