提交 d9f49ea1 编写于 作者: S ssadetsky

8139218: Dialog that opens and closes quickly changes focus in original focusowner

8159432: [PIT][macosx] StackOverflow in closed/java/awt/Dialog/DialogDeadlock/DialogDeadlockTest
Reviewed-by: alexsch, serb, azvegint
上级 040ade78
......@@ -76,6 +76,7 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
private LinkedList<KeyEvent> enqueuedKeyEvents = new LinkedList<KeyEvent>();
private LinkedList<TypeAheadMarker> typeAheadMarkers = new LinkedList<TypeAheadMarker>();
private boolean consumeNextKeyTyped;
private Component restoreFocusTo;
static {
AWTAccessor.setDefaultKeyboardFocusManagerAccessor(
......@@ -146,12 +147,28 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
}
private boolean restoreFocus(Window aWindow, Component vetoedComponent,
boolean clearOnFailure) {
restoreFocusTo = null;
Component toFocus =
KeyboardFocusManager.getMostRecentFocusOwner(aWindow);
if (toFocus != null && toFocus != vetoedComponent && doRestoreFocus(toFocus, vetoedComponent, false)) {
return true;
} else if (clearOnFailure) {
if (toFocus != null && toFocus != vetoedComponent) {
if (getHeavyweight(aWindow) != getNativeFocusOwner()) {
// cannot restore focus synchronously
if (!toFocus.isShowing() || !toFocus.canBeFocusOwner()) {
toFocus = toFocus.getNextFocusCandidate();
}
if (toFocus != null && toFocus != vetoedComponent) {
if (!toFocus.requestFocus(false,
CausedFocusEvent.Cause.ROLLBACK)) {
restoreFocusTo = toFocus;
}
return true;
}
} else if (doRestoreFocus(toFocus, vetoedComponent, false)) {
return true;
}
}
if (clearOnFailure) {
clearGlobalFocusOwnerPriv();
return true;
} else {
......@@ -413,6 +430,8 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
// may cause deadlock, thus we don't synchronize this block.
Component toFocus = KeyboardFocusManager.
getMostRecentFocusOwner(newFocusedWindow);
boolean isFocusRestore = restoreFocusTo != null &&
toFocus == restoreFocusTo;
if ((toFocus == null) &&
newFocusedWindow.isFocusableWindow())
{
......@@ -431,7 +450,10 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
tempLost, toFocus);
}
if (tempLost != null) {
tempLost.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
tempLost.requestFocusInWindow(
isFocusRestore && tempLost == toFocus ?
CausedFocusEvent.Cause.ROLLBACK :
CausedFocusEvent.Cause.ACTIVATION);
}
if (toFocus != null && toFocus != tempLost) {
......@@ -440,6 +462,7 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
toFocus.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
}
}
restoreFocusTo = null;
Window realOppositeWindow = this.realOppositeWindowWR.get();
if (realOppositeWindow != we.getOppositeWindow()) {
......@@ -489,6 +512,7 @@ public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
}
case FocusEvent.FOCUS_GAINED: {
restoreFocusTo = null;
FocusEvent fe = (FocusEvent)e;
CausedFocusEvent.Cause cause = (fe instanceof CausedFocusEvent) ?
((CausedFocusEvent)fe).getCause() : CausedFocusEvent.Cause.UNKNOWN;
......
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 8139218
@summary Dialog that opens and closes quickly changes focus in original
focusowner
@author Semyon Sadetsky: area=awt-focus
@run main RollbackFocusFromAnotherWindowTest
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RollbackFocusFromAnotherWindowTest extends JFrame implements KeyListener
{
private static RollbackFocusFromAnotherWindowTest tfs;
private static Robot robot;
public static void main(String[] args) throws Exception {
robot = new Robot();
SwingUtilities.invokeAndWait(() -> {
tfs = new RollbackFocusFromAnotherWindowTest();
tfs.setVisible(true);
});
robot.waitForIdle();
robot.delay(200);
try {
for (int i = 0; i < 10; i++) {
robot.keyPress(KeyEvent.VK_A);
robot.delay(10);
robot.keyRelease(KeyEvent.VK_A);
robot.waitForIdle();
robot.delay(200);
SwingUtilities.invokeAndWait(() -> {
String name = tfs.getFocusOwner().getName();
System.out.println(name);
if (!"Comp0".equals(name)) {
throw new RuntimeException(
"Focus is not restored correctly");
}
});
}
System.out.println("ok");
} finally {
SwingUtilities.invokeLater(() -> tfs.dispose());
}
}
public RollbackFocusFromAnotherWindowTest()
{
setUndecorated(true);
Container c = getContentPane();
c.setLayout(new FlowLayout());
for (int i = 0; i < 10; i++)
{
JTextField tf = new JTextField("" + i, 10);
tf.setName("Comp" + i);
c.add(tf);
tf.addKeyListener(this);
}
pack();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
Frame frame = new Frame();
frame.setVisible(true);
try {
Thread.sleep(2);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
frame.dispose();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册