7124375: [macosx] Focus isn't transfered as expected between components

Reviewed-by: art, ant, serb
This commit is contained in:
Leonid Romanov 2012-08-29 19:53:35 +04:00
parent 31ec802d7e
commit 18e3872928
21 changed files with 137 additions and 188 deletions

View File

@ -40,6 +40,7 @@ import java.awt.image.VolatileImage;
import java.awt.peer.ComponentPeer; import java.awt.peer.ComponentPeer;
import java.awt.peer.ContainerPeer; import java.awt.peer.ContainerPeer;
import java.awt.peer.KeyboardFocusManagerPeer;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.security.AccessController; import java.security.AccessController;
@ -894,14 +895,14 @@ public abstract class LWComponentPeer<T extends Component, D extends JComponent>
", focusedWindowChangeAllowed=" + focusedWindowChangeAllowed + ", focusedWindowChangeAllowed=" + focusedWindowChangeAllowed +
", time= " + time + ", cause=" + cause); ", time= " + time + ", cause=" + cause);
} }
if (LWKeyboardFocusManagerPeer.getInstance(getAppContext()). if (LWKeyboardFocusManagerPeer.processSynchronousLightweightTransfer(
processSynchronousLightweightTransfer(getTarget(), lightweightChild, temporary, getTarget(), lightweightChild, temporary,
focusedWindowChangeAllowed, time)) { focusedWindowChangeAllowed, time)) {
return true; return true;
} }
int result = LWKeyboardFocusManagerPeer.getInstance(getAppContext()). int result = LWKeyboardFocusManagerPeer.shouldNativelyFocusHeavyweight(
shouldNativelyFocusHeavyweight(getTarget(), lightweightChild, temporary, getTarget(), lightweightChild, temporary,
focusedWindowChangeAllowed, time, cause); focusedWindowChangeAllowed, time, cause);
switch (result) { switch (result) {
case LWKeyboardFocusManagerPeer.SNFH_FAILURE: case LWKeyboardFocusManagerPeer.SNFH_FAILURE:
@ -951,14 +952,13 @@ public abstract class LWComponentPeer<T extends Component, D extends JComponent>
return false; return false;
} }
LWComponentPeer focusOwnerPeer = KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
LWKeyboardFocusManagerPeer.getInstance(getAppContext()). Component focusOwner = kfmPeer.getCurrentFocusOwner();
getFocusOwner();
Component focusOwner = (focusOwnerPeer != null) ? focusOwnerPeer.getTarget() : null;
return LWKeyboardFocusManagerPeer.deliverFocus(lightweightChild, return LWKeyboardFocusManagerPeer.deliverFocus(lightweightChild,
getTarget(), temporary, getTarget(), temporary,
focusedWindowChangeAllowed, focusedWindowChangeAllowed,
time, cause, focusOwner); time, cause, focusOwner);
case LWKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED: case LWKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED:
return true; return true;
} }
@ -1263,8 +1263,8 @@ public abstract class LWComponentPeer<T extends Component, D extends JComponent>
protected void handleJavaFocusEvent(FocusEvent e) { protected void handleJavaFocusEvent(FocusEvent e) {
// Note that the peer receives all the FocusEvents from // Note that the peer receives all the FocusEvents from
// its lightweight children as well // its lightweight children as well
LWKeyboardFocusManagerPeer.getInstance(getAppContext()). KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
setFocusOwner(e.getID() == FocusEvent.FOCUS_GAINED ? this : null); kfmPeer.setCurrentFocusOwner(e.getID() == FocusEvent.FOCUS_GAINED ? getTarget() : null);
} }
/** /**

View File

@ -26,85 +26,47 @@
package sun.lwawt; package sun.lwawt;
import java.awt.Component; import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Window; import java.awt.Window;
import java.util.Map;
import java.util.HashMap;
import sun.awt.AWTAccessor;
import sun.awt.AppContext;
import sun.awt.KeyboardFocusManagerPeerImpl; import sun.awt.KeyboardFocusManagerPeerImpl;
public class LWKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl { public class LWKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
private static final LWKeyboardFocusManagerPeer inst = new LWKeyboardFocusManagerPeer();
private Object lock = new Object(); private Window focusedWindow;
private LWWindowPeer focusedWindow; private Component focusOwner;
private LWComponentPeer focusOwner;
private static Map<KeyboardFocusManager, LWKeyboardFocusManagerPeer> instances = public static LWKeyboardFocusManagerPeer getInstance() {
new HashMap<KeyboardFocusManager, LWKeyboardFocusManagerPeer>(); return inst;
public static synchronized LWKeyboardFocusManagerPeer getInstance(AppContext ctx) {
return getInstance(AWTAccessor.getKeyboardFocusManagerAccessor().
getCurrentKeyboardFocusManager(ctx));
} }
public static synchronized LWKeyboardFocusManagerPeer getInstance(KeyboardFocusManager manager) { private LWKeyboardFocusManagerPeer() {
LWKeyboardFocusManagerPeer instance = instances.get(manager);
if (instance == null) {
instance = new LWKeyboardFocusManagerPeer(manager);
instances.put(manager, instance);
}
return instance;
} }
public LWKeyboardFocusManagerPeer(KeyboardFocusManager manager) { @Override
super(manager); public void setCurrentFocusedWindow(Window win) {
synchronized (this) {
focusedWindow = win;
}
} }
@Override @Override
public Window getCurrentFocusedWindow() { public Window getCurrentFocusedWindow() {
synchronized (lock) { synchronized (this) {
return (focusedWindow != null) ? (Window)focusedWindow.getTarget() : null; return focusedWindow;
} }
} }
@Override @Override
public Component getCurrentFocusOwner() { public Component getCurrentFocusOwner() {
synchronized (lock) { synchronized (this) {
return (focusOwner != null) ? focusOwner.getTarget() : null; return focusOwner;
} }
} }
@Override @Override
public void setCurrentFocusOwner(Component comp) { public void setCurrentFocusOwner(Component comp) {
synchronized (lock) { synchronized (this) {
focusOwner = (comp != null) ? (LWComponentPeer)comp.getPeer() : null; focusOwner = comp;
}
}
void setFocusedWindow(LWWindowPeer peer) {
synchronized (lock) {
focusedWindow = peer;
}
}
LWWindowPeer getFocusedWindow() {
synchronized (lock) {
return focusedWindow;
}
}
void setFocusOwner(LWComponentPeer peer) {
synchronized (lock) {
focusOwner = peer;
}
}
LWComponentPeer getFocusOwner() {
synchronized (lock) {
return focusOwner;
} }
} }
} }

View File

@ -415,8 +415,8 @@ public abstract class LWToolkit extends SunToolkit implements Runnable {
} }
@Override @Override
public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) { public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
return LWKeyboardFocusManagerPeer.getInstance(manager); return LWKeyboardFocusManagerPeer.getInstance();
} }
@Override @Override

View File

@ -238,8 +238,7 @@ public class LWWindowPeer
// TODO: update graphicsConfig, see 4868278 // TODO: update graphicsConfig, see 4868278
platformWindow.setVisible(visible); platformWindow.setVisible(visible);
if (isSimpleWindow()) { if (isSimpleWindow()) {
LWKeyboardFocusManagerPeer manager = LWKeyboardFocusManagerPeer. KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
getInstance(getAppContext());
if (visible) { if (visible) {
if (!getTarget().isAutoRequestFocus()) { if (!getTarget().isAutoRequestFocus()) {
@ -248,7 +247,7 @@ public class LWWindowPeer
requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION); requestWindowFocus(CausedFocusEvent.Cause.ACTIVATION);
} }
// Focus the owner in case this window is focused. // Focus the owner in case this window is focused.
} else if (manager.getCurrentFocusedWindow() == getTarget()) { } else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
// Transfer focus to the owner. // Transfer focus to the owner.
LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this); LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
if (owner != null) { if (owner != null) {
@ -915,9 +914,8 @@ public class LWWindowPeer
public void dispatchKeyEvent(int id, long when, int modifiers, public void dispatchKeyEvent(int id, long when, int modifiers,
int keyCode, char keyChar, int keyLocation) int keyCode, char keyChar, int keyLocation)
{ {
LWComponentPeer focusOwner = KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
LWKeyboardFocusManagerPeer.getInstance(getAppContext()). Component focusOwner = kfmPeer.getCurrentFocusOwner();
getFocusOwner();
// Null focus owner may receive key event when // Null focus owner may receive key event when
// application hides the focused window upon ESC press // application hides the focused window upon ESC press
@ -925,9 +923,10 @@ public class LWWindowPeer
// may come to already hidden window. This check eliminates NPE. // may come to already hidden window. This check eliminates NPE.
if (focusOwner != null) { if (focusOwner != null) {
KeyEvent event = KeyEvent event =
new KeyEvent(focusOwner.getTarget(), id, when, modifiers, new KeyEvent(focusOwner, id, when, modifiers,
keyCode, keyChar, keyLocation); keyCode, keyChar, keyLocation);
focusOwner.postEvent(event); LWComponentPeer peer = (LWComponentPeer)focusOwner.getPeer();
peer.postEvent(event);
} }
} }
@ -1244,10 +1243,8 @@ public class LWWindowPeer
} }
} }
LWKeyboardFocusManagerPeer manager = LWKeyboardFocusManagerPeer. KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
getInstance(getAppContext()); Window oppositeWindow = becomesFocused ? kfmPeer.getCurrentFocusedWindow() : null;
Window oppositeWindow = becomesFocused ? manager.getCurrentFocusedWindow() : null;
// Note, the method is not called: // Note, the method is not called:
// - when the opposite (gaining focus) window is an owned/owner window. // - when the opposite (gaining focus) window is an owned/owner window.
@ -1260,7 +1257,7 @@ public class LWWindowPeer
grabbingWindow.ungrab(); grabbingWindow.ungrab();
} }
manager.setFocusedWindow(becomesFocused ? LWWindowPeer.this : null); kfmPeer.setCurrentFocusedWindow(becomesFocused ? getTarget() : null);
int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS; int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
WindowEvent windowEvent = new WindowEvent(getTarget(), eventID, oppositeWindow); WindowEvent windowEvent = new WindowEvent(getTarget(), eventID, oppositeWindow);

View File

@ -56,7 +56,6 @@ import java.util.WeakHashMap;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
import sun.awt.AppContext; import sun.awt.AppContext;
import sun.awt.HeadlessToolkit;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
import sun.awt.CausedFocusEvent; import sun.awt.CausedFocusEvent;
import sun.awt.KeyboardFocusManagerPeerProvider; import sun.awt.KeyboardFocusManagerPeerProvider;
@ -443,7 +442,7 @@ public abstract class KeyboardFocusManager
private void initPeer() { private void initPeer() {
Toolkit tk = Toolkit.getDefaultToolkit(); Toolkit tk = Toolkit.getDefaultToolkit();
KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk; KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk;
peer = peerProvider.createKeyboardFocusManagerPeer(this); peer = peerProvider.getKeyboardFocusManagerPeer();
} }
/** /**

View File

@ -33,6 +33,14 @@ import java.awt.Window;
*/ */
public interface KeyboardFocusManagerPeer { public interface KeyboardFocusManagerPeer {
/**
* Sets the window that should become the focused window.
*
* @param win the window that should become the focused window
*
*/
void setCurrentFocusedWindow(Window win);
/** /**
* Returns the currently focused window. * Returns the currently focused window.
* *

View File

@ -44,6 +44,14 @@ import java.util.Properties;
public class HToolkit extends SunToolkit public class HToolkit extends SunToolkit
implements ComponentFactory { implements ComponentFactory {
private static final KeyboardFocusManagerPeer kfmPeer = new KeyboardFocusManagerPeer() {
public void setCurrentFocusedWindow(Window win) {}
public Window getCurrentFocusedWindow() { return null; }
public void setCurrentFocusOwner(Component comp) {}
public Component getCurrentFocusOwner() { return null; }
public void clearGlobalFocusOwner(Window activeWindow) {}
};
public HToolkit() { public HToolkit() {
} }
@ -152,15 +160,9 @@ public class HToolkit extends SunToolkit
throw new HeadlessException(); throw new HeadlessException();
} }
public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) { public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
// See 6833019. // See 6833019.
return return kfmPeer;
new KeyboardFocusManagerPeer() {
public Window getCurrentFocusedWindow() { return null; }
public void setCurrentFocusOwner(Component comp) {}
public Component getCurrentFocusOwner() { return null; }
public void clearGlobalFocusOwner(Window activeWindow) {}
};
} }
public TrayIconPeer createTrayIcon(TrayIcon target) public TrayIconPeer createTrayIcon(TrayIcon target)

View File

@ -30,22 +30,25 @@ import java.awt.dnd.*;
import java.awt.dnd.peer.DragSourceContextPeer; import java.awt.dnd.peer.DragSourceContextPeer;
import java.awt.event.*; import java.awt.event.*;
import java.awt.im.InputMethodHighlight; import java.awt.im.InputMethodHighlight;
import java.awt.im.spi.InputMethodDescriptor;
import java.awt.image.*; import java.awt.image.*;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.peer.*; import java.awt.peer.*;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL; import java.net.URL;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import sun.awt.im.InputContext;
import sun.awt.image.ImageRepresentation;
public class HeadlessToolkit extends Toolkit public class HeadlessToolkit extends Toolkit
implements ComponentFactory, KeyboardFocusManagerPeerProvider { implements ComponentFactory, KeyboardFocusManagerPeerProvider {
private static final KeyboardFocusManagerPeer kfmPeer = new KeyboardFocusManagerPeer() {
public void setCurrentFocusedWindow(Window win) {}
public Window getCurrentFocusedWindow() { return null; }
public void setCurrentFocusOwner(Component comp) {}
public Component getCurrentFocusOwner() { return null; }
public void clearGlobalFocusOwner(Window activeWindow) {}
};
private Toolkit tk; private Toolkit tk;
private ComponentFactory componentFactory; private ComponentFactory componentFactory;
@ -179,15 +182,9 @@ public class HeadlessToolkit extends Toolkit
throw new HeadlessException(); throw new HeadlessException();
} }
public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) { public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() {
// See 6833019. // See 6833019.
return return kfmPeer;
new KeyboardFocusManagerPeer() {
public Window getCurrentFocusedWindow() { return null; }
public void setCurrentFocusOwner(Component comp) {}
public Component getCurrentFocusOwner() { return null; }
public void clearGlobalFocusOwner(Window activeWindow) {}
};
} }
public TrayIconPeer createTrayIcon(TrayIcon target) public TrayIconPeer createTrayIcon(TrayIcon target)

View File

@ -53,12 +53,6 @@ public abstract class KeyboardFocusManagerPeerImpl implements KeyboardFocusManag
public static final int SNFH_SUCCESS_HANDLED = 1; public static final int SNFH_SUCCESS_HANDLED = 1;
public static final int SNFH_SUCCESS_PROCEED = 2; public static final int SNFH_SUCCESS_PROCEED = 2;
protected KeyboardFocusManager manager;
public KeyboardFocusManagerPeerImpl(KeyboardFocusManager manager) {
this.manager = manager;
}
@Override @Override
public void clearGlobalFocusOwner(Window activeWindow) { public void clearGlobalFocusOwner(Window activeWindow) {
if (activeWindow != null) { if (activeWindow != null) {

View File

@ -25,20 +25,19 @@
package sun.awt; package sun.awt;
import java.awt.KeyboardFocusManager;
import java.awt.peer.KeyboardFocusManagerPeer; import java.awt.peer.KeyboardFocusManagerPeer;
/** /**
* {@link KeyboardFocusManagerPeerProvider} is required to be implemented by * {@link KeyboardFocusManagerPeerProvider} is required to be implemented by
* the currently used {@link java.awt.Toolkit} instance. In order to initialize * the currently used {@link java.awt.Toolkit} instance. In order to initialize
* {@link java.awt.KeyboardFocusManager}, an instance of {@link KeyboardFocusManagerPeer} * {@link java.awt.KeyboardFocusManager}, a singleton instance of {@link KeyboardFocusManagerPeer}
* is needed. To create that instance, the {@link #createKeyboardFocusManagerPeer} * is needed. To obtain that instance, the {@link #getKeyboardFocusManagerPeer}
* method of the current toolkit is called. * method of the current toolkit is called.
*/ */
public interface KeyboardFocusManagerPeerProvider { public interface KeyboardFocusManagerPeerProvider {
/** /**
* Creates a KeyboardFocusManagerPeer for the specified KeyboardFocusManager. * Gets a singleton KeyboardFocusManagerPeer instance.
*/ */
KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager); KeyboardFocusManagerPeer getKeyboardFocusManagerPeer();
} }

View File

@ -53,7 +53,6 @@ import sun.security.action.GetPropertyAction;
import sun.security.action.GetBooleanAction; import sun.security.action.GetBooleanAction;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
@ -204,7 +203,7 @@ public abstract class SunToolkit extends Toolkit
public abstract RobotPeer createRobot(Robot target, GraphicsDevice screen) public abstract RobotPeer createRobot(Robot target, GraphicsDevice screen)
throws AWTException; throws AWTException;
public abstract KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) public abstract KeyboardFocusManagerPeer getKeyboardFocusManagerPeer()
throws HeadlessException; throws HeadlessException;
/** /**

View File

@ -601,7 +601,7 @@ public class XComponentPeer extends XWindow implements ComponentPeer, DropTarget
final XWindowPeer parentXWindow = getParentTopLevel(); final XWindowPeer parentXWindow = getParentTopLevel();
Window parentWindow = (Window)parentXWindow.getTarget(); Window parentWindow = (Window)parentXWindow.getTarget();
if (parentXWindow.isFocusableWindow() && parentXWindow.isSimpleWindow() && if (parentXWindow.isFocusableWindow() && parentXWindow.isSimpleWindow() &&
XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() != parentWindow) XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() != parentWindow)
{ {
postEvent(new InvocationEvent(parentWindow, new Runnable() { postEvent(new InvocationEvent(parentWindow, new Runnable() {
public void run() { public void run() {

View File

@ -1108,7 +1108,7 @@ abstract class XDecoratedPeer extends XWindowPeer {
focusLog.fine("Request for decorated window focus"); focusLog.fine("Request for decorated window focus");
// If this is Frame or Dialog we can't assure focus request success - but we still can try // If this is Frame or Dialog we can't assure focus request success - but we still can try
// If this is Window and its owner Frame is active we can be sure request succedded. // If this is Window and its owner Frame is active we can be sure request succedded.
Window focusedWindow = XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow(); Window focusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow); Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow);
focusLog.finer("Current window is: active={0}, focused={1}", focusLog.finer("Current window is: active={0}, focused={1}",
@ -1201,7 +1201,7 @@ abstract class XDecoratedPeer extends XWindowPeer {
} }
public void handleWindowFocusOut(Window oppositeWindow, long serial) { public void handleWindowFocusOut(Window oppositeWindow, long serial) {
Window actualFocusedWindow = XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow(); Window actualFocusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
// If the actual focused window is not this decorated window then retain it. // If the actual focused window is not this decorated window then retain it.
if (actualFocusedWindow != null && actualFocusedWindow != target) { if (actualFocusedWindow != null && actualFocusedWindow != target) {

View File

@ -135,7 +135,7 @@ class XDialogPeer extends XDecoratedPeer implements DialogPeer {
* Thus we don't have to perform any transitive (a blocker of a blocker) checks. * Thus we don't have to perform any transitive (a blocker of a blocker) checks.
*/ */
boolean isFocusedWindowModalBlocker() { boolean isFocusedWindowModalBlocker() {
Window focusedWindow = XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow(); Window focusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
XWindowPeer focusedWindowPeer = null; XWindowPeer focusedWindowPeer = null;
if (focusedWindow != null) { if (focusedWindow != null) {

View File

@ -96,11 +96,11 @@ public class XEmbedChildProxyPeer implements ComponentPeer, XEventDispatcher{
public void handleEvent(AWTEvent e) { public void handleEvent(AWTEvent e) {
switch (e.getID()) { switch (e.getID()) {
case FocusEvent.FOCUS_GAINED: case FocusEvent.FOCUS_GAINED:
XKeyboardFocusManagerPeer.setCurrentNativeFocusOwner(proxy); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusOwner(proxy);
container.focusGained(handle); container.focusGained(handle);
break; break;
case FocusEvent.FOCUS_LOST: case FocusEvent.FOCUS_LOST:
XKeyboardFocusManagerPeer.setCurrentNativeFocusOwner(null); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusOwner(null);
container.focusLost(handle); container.focusLost(handle);
break; break;
case KeyEvent.KEY_PRESSED: case KeyEvent.KEY_PRESSED:
@ -172,7 +172,7 @@ public class XEmbedChildProxyPeer implements ComponentPeer, XEventDispatcher{
if (lightweightChild == null) { if (lightweightChild == null) {
lightweightChild = (Component)proxy; lightweightChild = (Component)proxy;
} }
Component currentOwner = XKeyboardFocusManagerPeer.getCurrentNativeFocusOwner(); Component currentOwner = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusOwner();
if (currentOwner != null && currentOwner.getPeer() == null) { if (currentOwner != null && currentOwner.getPeer() == null) {
currentOwner = null; currentOwner = null;
} }
@ -224,7 +224,8 @@ public class XEmbedChildProxyPeer implements ComponentPeer, XEventDispatcher{
if (parent != null) { if (parent != null) {
Window parentWindow = (Window)parent; Window parentWindow = (Window)parent;
// and check that it is focused // and check that it is focused
if (!parentWindow.isFocused() && XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() == parentWindow) { if (!parentWindow.isFocused() &&
XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() == parentWindow) {
// if it is not - skip requesting focus on Solaris // if it is not - skip requesting focus on Solaris
// but return true for compatibility. // but return true for compatibility.
return true; return true;

View File

@ -204,7 +204,7 @@ public class XEmbedClientHelper extends XEmbedHelper implements XEventDispatcher
// XEMBED_FOCUS_OUT client messages), so we first need to check if // XEMBED_FOCUS_OUT client messages), so we first need to check if
// embedded is an active window before sending WINDOW_LOST_FOCUS // embedded is an active window before sending WINDOW_LOST_FOCUS
// to shared code // to shared code
if (XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() == embedded.target) { if (XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() == embedded.target) {
embedded.handleWindowFocusOut(null, 0); embedded.handleWindowFocusOut(null, 0);
} }
} }

View File

@ -25,66 +25,48 @@
package sun.awt.X11; package sun.awt.X11;
import java.awt.Component; import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Window; import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.peer.KeyboardFocusManagerPeer;
import java.awt.peer.ComponentPeer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger;
import sun.awt.CausedFocusEvent; import sun.awt.CausedFocusEvent;
import sun.awt.SunToolkit;
import sun.awt.KeyboardFocusManagerPeerImpl; import sun.awt.KeyboardFocusManagerPeerImpl;
public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl { public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer"); private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XKeyboardFocusManagerPeer");
private static final XKeyboardFocusManagerPeer inst = new XKeyboardFocusManagerPeer();
private static Object lock = new Object() {}; private Component currentFocusOwner;
private static Component currentFocusOwner; private Window currentFocusedWindow;
private static Window currentFocusedWindow;
XKeyboardFocusManagerPeer(KeyboardFocusManager manager) { public static XKeyboardFocusManagerPeer getInstance() {
super(manager); return inst;
}
private XKeyboardFocusManagerPeer() {
} }
@Override @Override
public void setCurrentFocusOwner(Component comp) { public void setCurrentFocusOwner(Component comp) {
setCurrentNativeFocusOwner(comp); synchronized (this) {
}
@Override
public Component getCurrentFocusOwner() {
return getCurrentNativeFocusOwner();
}
@Override
public Window getCurrentFocusedWindow() {
return getCurrentNativeFocusedWindow();
}
public static void setCurrentNativeFocusOwner(Component comp) {
synchronized (lock) {
currentFocusOwner = comp; currentFocusOwner = comp;
} }
} }
public static Component getCurrentNativeFocusOwner() { @Override
synchronized(lock) { public Component getCurrentFocusOwner() {
synchronized(this) {
return currentFocusOwner; return currentFocusOwner;
} }
} }
public static void setCurrentNativeFocusedWindow(Window win) { @Override
if (focusLog.isLoggable(PlatformLogger.FINER)) focusLog.finer("Setting current native focused window " + win); public void setCurrentFocusedWindow(Window win) {
if (focusLog.isLoggable(PlatformLogger.FINER)) {
focusLog.finer("Setting current focused window " + win);
}
XWindowPeer from = null, to = null; XWindowPeer from = null, to = null;
synchronized(lock) { synchronized(this) {
if (currentFocusedWindow != null) { if (currentFocusedWindow != null) {
from = (XWindowPeer)currentFocusedWindow.getPeer(); from = (XWindowPeer)currentFocusedWindow.getPeer();
} }
@ -104,8 +86,9 @@ public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
} }
} }
public static Window getCurrentNativeFocusedWindow() { @Override
synchronized(lock) { public Window getCurrentFocusedWindow() {
synchronized(this) {
return currentFocusedWindow; return currentFocusedWindow;
} }
} }
@ -124,6 +107,6 @@ public class XKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
focusedWindowChangeAllowed, focusedWindowChangeAllowed,
time, time,
cause, cause,
getCurrentNativeFocusOwner()); getInstance().getCurrentFocusOwner());
} }
} }

View File

@ -50,7 +50,6 @@ import javax.swing.LookAndFeel;
import javax.swing.UIDefaults; import javax.swing.UIDefaults;
import sun.awt.*; import sun.awt.*;
import sun.font.FontConfigManager; import sun.font.FontConfigManager;
import sun.font.FontManager;
import sun.java2d.SunGraphicsEnvironment; import sun.java2d.SunGraphicsEnvironment;
import sun.misc.PerformanceLogger; import sun.misc.PerformanceLogger;
import sun.print.PrintJob2D; import sun.print.PrintJob2D;
@ -667,7 +666,7 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
long w = 0; long w = 0;
if (windowToXWindow(ev.get_xany().get_window()) != null) { if (windowToXWindow(ev.get_xany().get_window()) != null) {
Component owner = Component owner =
XKeyboardFocusManagerPeer.getCurrentNativeFocusOwner(); XKeyboardFocusManagerPeer.getInstance().getCurrentFocusOwner();
if (owner != null) { if (owner != null) {
XWindow ownerWindow = (XWindow) AWTAccessor.getComponentAccessor().getPeer(owner); XWindow ownerWindow = (XWindow) AWTAccessor.getComponentAccessor().getPeer(owner);
if (ownerWindow != null) { if (ownerWindow != null) {
@ -1159,9 +1158,8 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
return peer; return peer;
} }
public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) throws HeadlessException { public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer() throws HeadlessException {
XKeyboardFocusManagerPeer peer = new XKeyboardFocusManagerPeer(manager); return XKeyboardFocusManagerPeer.getInstance();
return peer;
} }
/** /**

View File

@ -617,7 +617,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
public void handleWindowFocusIn_Dispatch() { public void handleWindowFocusIn_Dispatch() {
if (EventQueue.isDispatchThread()) { if (EventQueue.isDispatchThread()) {
XKeyboardFocusManagerPeer.setCurrentNativeFocusedWindow((Window) target); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow((Window) target);
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS); WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS);
SunToolkit.setSystemGenerated(we); SunToolkit.setSystemGenerated(we);
target.dispatchEvent(we); target.dispatchEvent(we);
@ -626,7 +626,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
public void handleWindowFocusInSync(long serial) { public void handleWindowFocusInSync(long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS); WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS);
XKeyboardFocusManagerPeer.setCurrentNativeFocusedWindow((Window) target); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow((Window) target);
sendEvent(we); sendEvent(we);
} }
// NOTE: This method may be called by privileged threads. // NOTE: This method may be called by privileged threads.
@ -634,7 +634,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
public void handleWindowFocusIn(long serial) { public void handleWindowFocusIn(long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS); WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_GAINED_FOCUS);
/* wrap in Sequenced, then post*/ /* wrap in Sequenced, then post*/
XKeyboardFocusManagerPeer.setCurrentNativeFocusedWindow((Window) target); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow((Window) target);
postEvent(wrapInSequenced((AWTEvent) we)); postEvent(wrapInSequenced((AWTEvent) we));
} }
@ -642,15 +642,15 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
// DO NOT INVOKE CLIENT CODE ON THIS THREAD! // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
public void handleWindowFocusOut(Window oppositeWindow, long serial) { public void handleWindowFocusOut(Window oppositeWindow, long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow); WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow);
XKeyboardFocusManagerPeer.setCurrentNativeFocusedWindow(null); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow(null);
XKeyboardFocusManagerPeer.setCurrentNativeFocusOwner(null); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusOwner(null);
/* wrap in Sequenced, then post*/ /* wrap in Sequenced, then post*/
postEvent(wrapInSequenced((AWTEvent) we)); postEvent(wrapInSequenced((AWTEvent) we));
} }
public void handleWindowFocusOutSync(Window oppositeWindow, long serial) { public void handleWindowFocusOutSync(Window oppositeWindow, long serial) {
WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow); WindowEvent we = new WindowEvent((Window)target, WindowEvent.WINDOW_LOST_FOCUS, oppositeWindow);
XKeyboardFocusManagerPeer.setCurrentNativeFocusedWindow(null); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusedWindow(null);
XKeyboardFocusManagerPeer.setCurrentNativeFocusOwner(null); XKeyboardFocusManagerPeer.getInstance().setCurrentFocusOwner(null);
sendEvent(we); sendEvent(we);
} }
@ -1138,7 +1138,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
// getWMState() always returns 0 (Withdrawn) for simple windows. Hence // getWMState() always returns 0 (Withdrawn) for simple windows. Hence
// we ignore the state for such windows. // we ignore the state for such windows.
if (isVisible() && (state == XUtilConstants.NormalState || isSimpleWindow())) { if (isVisible() && (state == XUtilConstants.NormalState || isSimpleWindow())) {
if (XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow() == if (XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow() ==
getTarget()) getTarget())
{ {
show = true; show = true;
@ -1185,7 +1185,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
* receive WM_TAKE_FOCUS. * receive WM_TAKE_FOCUS.
*/ */
if (isSimpleWindow()) { if (isSimpleWindow()) {
if (target == XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow()) { if (target == XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow()) {
Window owner = getDecoratedOwner((Window)target); Window owner = getDecoratedOwner((Window)target);
((XWindowPeer)AWTAccessor.getComponentAccessor().getPeer(owner)).requestWindowFocus(); ((XWindowPeer)AWTAccessor.getComponentAccessor().getPeer(owner)).requestWindowFocus();
} }
@ -1825,7 +1825,7 @@ class XWindowPeer extends XPanelPeer implements WindowPeer,
// If this is Frame or Dialog we can't assure focus request success - but we still can try // If this is Frame or Dialog we can't assure focus request success - but we still can try
// If this is Window and its owner Frame is active we can be sure request succedded. // If this is Window and its owner Frame is active we can be sure request succedded.
Window ownerWindow = XWindowPeer.getDecoratedOwner((Window)target); Window ownerWindow = XWindowPeer.getDecoratedOwner((Window)target);
Window focusedWindow = XKeyboardFocusManagerPeer.getCurrentNativeFocusedWindow(); Window focusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow); Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow);
if (isWMStateNetHidden()) { if (isWMStateNetHidden()) {

View File

@ -25,7 +25,6 @@
package sun.awt.windows; package sun.awt.windows;
import java.awt.KeyboardFocusManager;
import java.awt.Window; import java.awt.Window;
import java.awt.Component; import java.awt.Component;
import java.awt.peer.ComponentPeer; import java.awt.peer.ComponentPeer;
@ -37,8 +36,13 @@ class WKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
static native Component getNativeFocusOwner(); static native Component getNativeFocusOwner();
static native Window getNativeFocusedWindow(); static native Window getNativeFocusedWindow();
WKeyboardFocusManagerPeer(KeyboardFocusManager manager) { private static final WKeyboardFocusManagerPeer inst = new WKeyboardFocusManagerPeer();
super(manager);
public static WKeyboardFocusManagerPeer getInstance() {
return inst;
}
private WKeyboardFocusManagerPeer() {
} }
@Override @Override
@ -51,6 +55,12 @@ class WKeyboardFocusManagerPeer extends KeyboardFocusManagerPeerImpl {
return getNativeFocusOwner(); return getNativeFocusOwner();
} }
@Override
public void setCurrentFocusedWindow(Window win) {
// Not used on Windows
throw new RuntimeException("not implemented");
}
@Override @Override
public Window getCurrentFocusedWindow() { public Window getCurrentFocusedWindow() {
return getNativeFocusedWindow(); return getNativeFocusedWindow();

View File

@ -506,10 +506,10 @@ public class WToolkit extends SunToolkit implements Runnable {
return true; return true;
} }
public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager manager) public KeyboardFocusManagerPeer getKeyboardFocusManagerPeer()
throws HeadlessException throws HeadlessException
{ {
return new WKeyboardFocusManagerPeer(manager); return WKeyboardFocusManagerPeer.getInstance();
} }
protected native void setDynamicLayoutNative(boolean b); protected native void setDynamicLayoutNative(boolean b);