8139218: Dialog that opens and closes quickly changes focus in original focusowner
authorssadetsky
Tue, 31 May 2016 15:57:01 +0300
changeset 39016 f52c1429ae85
parent 39015 3fa4e9304a9a
child 39017 6a7eec343e5e
8139218: Dialog that opens and closes quickly changes focus in original focusowner Reviewed-by: alexsch
jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java
jdk/test/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java
--- a/jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java	Tue May 31 16:03:21 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java	Tue May 31 15:57:01 2016 +0300
@@ -148,9 +148,20 @@
         Component toFocus =
             KeyboardFocusManager.getMostRecentFocusOwner(aWindow);
 
-        if (toFocus != null && toFocus != vetoedComponent && doRestoreFocus(toFocus, vetoedComponent, false)) {
-            return true;
-        } else if (clearOnFailure) {
+        if (toFocus != null && toFocus != vetoedComponent) {
+            Component heavyweight = getHeavyweight(aWindow);
+            if (heavyweight != null) {
+                setNativeFocusOwner(heavyweight);
+                Toolkit.getEventQueue().createSecondaryLoop(
+                        () -> getGlobalFocusedWindow() != aWindow, null, 50)
+                        .enter();
+            }
+            if (getGlobalFocusedWindow() == aWindow &&
+                              doRestoreFocus(toFocus, vetoedComponent, false)) {
+                return true;
+            }
+        }
+        if (clearOnFailure) {
             clearGlobalFocusOwnerPriv();
             return true;
         } else {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java	Tue May 31 15:57:01 2016 +0300
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2016, 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
+  @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) {
+
+    }
+}