8154043: Fields not reachable anymore by tab-key, because of new tabbing behaviour of radio button groups.
authorssadetsky
Mon, 26 Sep 2016 11:59:46 +0300
changeset 41396 d72f3a09b3ae
parent 41395 2a6f7eb1dc23
child 41397 f44bb269125c
8154043: Fields not reachable anymore by tab-key, because of new tabbing behaviour of radio button groups. Reviewed-by: alexsch
jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java
jdk/test/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java
jdk/test/javax/swing/JRadioButton/8033699/bug8033699.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java	Mon Sep 26 12:33:40 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/LayoutFocusTraversalPolicy.java	Mon Sep 26 11:59:46 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 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
@@ -26,9 +26,9 @@
 
 import java.awt.Component;
 import java.awt.Container;
-import java.awt.ComponentOrientation;
 import java.util.Comparator;
 import java.io.*;
+import java.util.Enumeration;
 import sun.awt.SunToolkit;
 
 
@@ -236,6 +236,31 @@
             JComboBox<?> box = (JComboBox)aComponent;
             return box.getUI().isFocusTraversable(box);
         } else if (aComponent instanceof JComponent) {
+            if (SunToolkit.isInstanceOf(aComponent,
+                                                 "javax.swing.JToggleButton")) {
+                JToggleButton.ToggleButtonModel model =
+                        (JToggleButton.ToggleButtonModel) ((JToggleButton)
+                                aComponent).getModel();
+                if (model != null) {
+                    ButtonGroup group = model.getGroup();
+                    if (group != null) {
+                        Enumeration<AbstractButton> elements =
+                                                            group.getElements();
+                        int idx = 0;
+                        while (elements.hasMoreElements()) {
+                            AbstractButton member = elements.nextElement();
+                            if (member.isVisible() && member.isDisplayable() &&
+                                   member.isEnabled() && member.isFocusable()) {
+                                if (member == aComponent) {
+                                    return idx == 0;
+                                }
+                                idx++;
+                            }
+                        }
+                    }
+                }
+            }
+
             JComponent jComponent = (JComponent)aComponent;
             InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED,
                                                        false);
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java	Mon Sep 26 12:33:40 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRadioButtonUI.java	Mon Sep 26 11:59:46 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 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
@@ -438,21 +438,7 @@
         // Check if the next object to gain focus belongs
         // to the button group or not
         Component getFocusTransferBaseComponent(boolean next){
-            Component focusBaseComp = activeBtn;
-            Container container = focusBaseComp.getFocusCycleRootAncestor();
-            if (container != null) {
-                FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
-                Component comp = next ? policy.getComponentAfter(container, activeBtn)
-                                      : policy.getComponentBefore(container, activeBtn);
-
-                // If next component in the button group, use last/first button as base focus
-                // otherwise, use the activeBtn as the base focus
-                if (containsInGroup(comp)) {
-                    focusBaseComp = next ? lastBtn : firstBtn;
-                }
-            }
-
-            return focusBaseComp;
+            return firstBtn;
         }
 
         boolean getButtonGroupInfo() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java	Mon Sep 26 11:59:46 2016 +0300
@@ -0,0 +1,158 @@
+/*
+ * 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 8154043
+  @summary Fields not reachable anymore by tab-key, because of new tabbing
+  behaviour of radio button groups.
+  @run main ButtonGroupLayoutTraversalTest
+*/
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.KeyEvent;
+
+public class ButtonGroupLayoutTraversalTest {
+    static int nx = 3;
+    static int ny = 3;
+
+    static int focusCnt[] = new int[nx * ny];
+    private static JFrame window;
+
+
+    public static void main(String[] args) throws Exception {
+
+        SwingUtilities.invokeAndWait(()->initLayout(nx, ny));
+        Robot robot = new Robot();
+        robot.setAutoDelay(100);
+        robot.waitForIdle();
+        robot.delay(200);
+
+
+        for(int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
+            robot.keyPress(KeyEvent.VK_RIGHT);
+            robot.keyRelease(KeyEvent.VK_RIGHT);
+        }
+
+        for(int i = 0; i < nx * ny / 2; i++) {
+            robot.keyPress(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_TAB);
+        }
+
+        robot.waitForIdle();
+        robot.delay(200);
+
+        for(int i = 0; i < nx * ny; i++) {
+            if(focusCnt[i] < 1) {
+                SwingUtilities.invokeLater(window::dispose);
+                throw new RuntimeException("Component " + i +
+                        " is not reachable in the forward focus cycle");
+            } else if (focusCnt[i] > 1) {
+                SwingUtilities.invokeLater(window::dispose);
+                throw new RuntimeException("Component " + i +
+                        " got focus more than once in the forward focus cycle");
+            }
+        }
+
+        for(int i = 0; i < nx * ny / 2; i++) {
+            robot.keyPress(KeyEvent.VK_SHIFT);
+            robot.keyPress(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_TAB);
+            robot.keyRelease(KeyEvent.VK_SHIFT);
+        }
+
+        for(int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
+            robot.keyPress(KeyEvent.VK_LEFT);
+            robot.keyRelease(KeyEvent.VK_LEFT);
+        }
+
+        robot.keyPress(KeyEvent.VK_SHIFT);
+        robot.keyPress(KeyEvent.VK_TAB);
+        robot.keyRelease(KeyEvent.VK_TAB);
+        robot.keyRelease(KeyEvent.VK_SHIFT);
+
+        robot.waitForIdle();
+        robot.delay(200);
+
+        for(int i = 0; i < nx * ny; i++) {
+            if(focusCnt[i] < 2) {
+                SwingUtilities.invokeLater(window::dispose);
+                throw new RuntimeException("Component " + i +
+                        " is not reachable in the backward focus cycle");
+            } else if (focusCnt[i] > 2) {
+                SwingUtilities.invokeLater(window::dispose);
+                throw new RuntimeException("Component " + i +
+                        " got focus more than once in the backward focus cycle");
+            }
+        }
+
+        SwingUtilities.invokeLater(window::dispose);
+    }
+
+    public static void initLayout(int nx, int ny)
+    {
+        window = new JFrame("Test");
+        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        JPanel rootPanel = new JPanel();
+        rootPanel.setLayout(new BorderLayout());
+        JPanel formPanel = new JPanel(new GridLayout(nx, ny));
+        formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
+        formPanel.setFocusCycleRoot(true);
+        ButtonGroup radioButtonGroup = new ButtonGroup();
+        for(int i = 0; i < nx * ny; i++) {
+            JToggleButton comp;
+            if(i % 2 == 0) {
+                comp = new JRadioButton("Grouped component");
+                radioButtonGroup.add(comp);
+            } else {
+                comp = new JRadioButton("Single component");
+            }
+            formPanel.add(comp);
+            int fi = i;
+            comp.setBackground(Color.red);
+            comp.addFocusListener(new FocusAdapter() {
+                @Override
+                public void focusGained(FocusEvent e) {
+                    focusCnt[fi]++;
+                    if( focusCnt[fi] == 1) {
+                        ((JComponent) e.getSource())
+                                .setBackground(Color.yellow);
+                    } else if(focusCnt[fi] == 2) {
+                        ((JComponent) e.getSource())
+                                .setBackground(Color.green);
+                    } else {
+                        ((JComponent) e.getSource())
+                                .setBackground(Color.red);
+                    }
+                }
+            });
+        }
+        rootPanel.add(formPanel, BorderLayout.CENTER);
+        window.add(rootPanel);
+        window.pack();
+        window.setVisible(true);
+    }
+}
--- a/jdk/test/javax/swing/JRadioButton/8033699/bug8033699.java	Mon Sep 26 12:33:40 2016 +0530
+++ b/jdk/test/javax/swing/JRadioButton/8033699/bug8033699.java	Mon Sep 26 11:59:46 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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
@@ -26,7 +26,7 @@
  * @key headful
  * @library ../../regtesthelpers
  * @build Util
- * @bug 8033699
+ * @bug 8033699 8154043
  * @summary  Incorrect radio button behavior when pressing tab key
  * @author Vivi An
  * @run main bug8033699
@@ -135,6 +135,7 @@
     private static void runTest1() throws Exception{
         hitKey(robot, KeyEvent.VK_TAB);
         hitKey(robot, KeyEvent.VK_TAB);
+        hitKey(robot, KeyEvent.VK_TAB);
 
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
@@ -163,11 +164,12 @@
     private static void runTest3() throws Exception{
         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
+        hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
-                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
+                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
                     System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");
-                    throw new RuntimeException("Focus is not on Radio Button C as Expected");
+                    throw new RuntimeException("Focus is not on Radio Button A as Expected");
                 }
             }
         });
@@ -175,39 +177,39 @@
 
     // Using arrow key to move focus in radio button group
     private static void runTest4() throws Exception{
-        hitKey(robot, KeyEvent.VK_UP);
-        hitKey(robot, KeyEvent.VK_LEFT);
+        hitKey(robot, KeyEvent.VK_DOWN);
+        hitKey(robot, KeyEvent.VK_RIGHT);
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
-                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
+                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
                     System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");
-                    throw new RuntimeException("Focus is not on Radio Button A as Expected");
+                    throw new RuntimeException("Focus is not on Radio Button C as Expected");
                 }
             }
         });
     }
 
     private static void runTest5() throws Exception{
-        hitKey(robot, KeyEvent.VK_DOWN);
-        hitKey(robot, KeyEvent.VK_RIGHT);
+        hitKey(robot, KeyEvent.VK_UP);
+        hitKey(robot, KeyEvent.VK_LEFT);
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
-                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
+                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
                     System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");
-                    throw new RuntimeException("Focus is not on Radio Button C as Expected");
+                    throw new RuntimeException("Focus is not on Radio Button A as Expected");
                 }
             }
         });
     }
 
     private static void runTest6() throws Exception{
-        hitKey(robot, KeyEvent.VK_DOWN);
-        hitKey(robot, KeyEvent.VK_DOWN);
+        hitKey(robot, KeyEvent.VK_UP);
+        hitKey(robot, KeyEvent.VK_UP);
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {
                     System.out.println("Radio button Group Circle Back To First Button Test");
-                    throw new RuntimeException("Focus is not on Radio Button A as Expected");
+                    throw new RuntimeException("Focus is not on Radio Button B as Expected");
                 }
             }
         });
@@ -229,9 +231,9 @@
         hitKey(robot, KeyEvent.VK_TAB);
         SwingUtilities.invokeAndWait(new Runnable() {
             public void run() {
-                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
+                if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
                     System.out.println("Separate Component added in button group layout");
-                    throw new RuntimeException("Focus is not on Radio Button C as Expected");
+                    throw new RuntimeException("Focus is not on Radio Button Single as Expected");
                 }
             }
         });