< prev index next >

test/javax/swing/JRadioButton/FocusTraversal/FocusTraversal.java

Print this page




   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 8129940
  26    @summary JRadioButton does not honor non-standard FocusTraversalKeys
  27    @author Semyon Sadetsky
  28   */
  29 
  30 import javax.swing.*;
  31 import java.awt.*;

  32 import java.awt.event.KeyEvent;
  33 import java.util.HashSet;
  34 import java.util.Set;












  35 
  36 public class FocusTraversal {
  37 
  38     private static JFrame frame;
  39     private static JRadioButton a;


  40     private static JRadioButton d;
  41     private static JTextField next;
  42     private static JTextField prev;

  43 
  44     public static void main(String[] args) throws Exception {
























  45         SwingUtilities.invokeAndWait(new Runnable() {
  46             @Override
  47             public void run() {
  48                 frame = new JFrame("FocusTraversalTest");
  49                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  50                 frame.setUndecorated(true);
  51 
  52                 Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();
  53                 keystrokes.add(KeyStroke.getKeyStroke("TAB"));
  54                 keystrokes.add(KeyStroke.getKeyStroke("ENTER"));



  55                 frame.setFocusTraversalKeys(
  56                         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  57                         keystrokes);
  58 
  59                 a = new JRadioButton("a");
  60                 JRadioButton b = new JRadioButton("b");
  61                 JRadioButton c = new JRadioButton("c");
  62                 d = new JRadioButton("d");
  63 
  64                 ButtonGroup radioButtonGroup = new ButtonGroup();
  65                 radioButtonGroup.add(a);
  66                 radioButtonGroup.add(b);
  67                 radioButtonGroup.add(c);
  68                 radioButtonGroup.add(d);
  69 
  70                 JPanel panel = new JPanel();
  71                 prev = new JTextField("text");
  72                 panel.add(prev);
  73                 panel.add(a);
  74                 panel.add(b);
  75                 panel.add(c);
  76                 panel.add(d);
  77                 next = new JTextField("text");
  78                 panel.add(next);
  79 
  80                 JPanel root = new JPanel();
  81                 root.setLayout(new BorderLayout());
  82                 root.add(panel, BorderLayout.CENTER);
  83                 root.add(new JButton("OK"), BorderLayout.SOUTH);
  84 
  85                 frame.add(root);
  86                 frame.pack();

  87                 frame.setVisible(true);

  88             }
  89         });
  90 
  91         SwingUtilities.invokeAndWait(new Runnable() {
  92             @Override
  93             public void run() {
  94                 a.requestFocus();
  95             }
  96         });
  97 
  98         Robot robot = new Robot();





  99         robot.waitForIdle();







 100 
 101         robot.setAutoDelay(200);
 102 
 103         robot.keyPress(KeyEvent.VK_ENTER);
 104         robot.keyRelease(KeyEvent.VK_ENTER);
 105         robot.waitForIdle();












 106 







 107         SwingUtilities.invokeAndWait(new Runnable() {
 108             @Override
 109             public void run() {
 110                 Component focusOwner =
 111                         FocusManager.getCurrentManager().getFocusOwner();
 112                 if (focusOwner != next) {
 113                     throw new RuntimeException(
 114                             "Focus component is wrong after forward key " + focusOwner);
 115                 }
 116             }
 117         });

 118 
 119         robot.keyPress(KeyEvent.VK_SHIFT);
 120         robot.keyPress(KeyEvent.VK_TAB);
 121         robot.keyRelease(KeyEvent.VK_TAB);
 122         robot.keyRelease(KeyEvent.VK_SHIFT);
 123         robot.waitForIdle();
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125             @Override
 126             public void run() {
 127                 Component focusOwner =
 128                         FocusManager.getCurrentManager().getFocusOwner();
 129                 if (focusOwner != d) {
 130                     throw new RuntimeException(
 131                             "Focus component is wrong after backward key " + focusOwner);


 132                 }
 133             }
 134         });
 135         SwingUtilities.invokeLater(new Runnable() {



















 136             @Override
 137             public void run() {
 138                 frame.dispose();
 139             }
 140         });
 141         System.out.println("ok");
 142 
 143     }
 144 }


   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  @bug 8129940 8132770
  26  @summary JRadioButton should run custom FocusTraversalKeys for all LaFs
  27  @run main FocusTraversal
  28  */
  29 import java.awt.BorderLayout;
  30 import java.awt.Component;
  31 import java.awt.KeyboardFocusManager;
  32 import java.awt.Robot;
  33 import java.awt.event.KeyEvent;
  34 import java.util.HashSet;
  35 import java.util.Set;
  36 import javax.swing.ButtonGroup;
  37 import javax.swing.FocusManager;
  38 import javax.swing.JButton;
  39 import javax.swing.JFrame;
  40 import javax.swing.JPanel;
  41 import javax.swing.JRadioButton;
  42 import javax.swing.JTextField;
  43 import javax.swing.KeyStroke;
  44 import javax.swing.LookAndFeel;
  45 import javax.swing.SwingUtilities;
  46 import javax.swing.UIManager;
  47 import javax.swing.UnsupportedLookAndFeelException;
  48 
  49 public class FocusTraversal {
  50 
  51     private static JFrame frame;
  52     private static JRadioButton a;
  53     private static JRadioButton b;
  54     private static JRadioButton c;
  55     private static JRadioButton d;
  56     private static JTextField next;
  57     private static JTextField prev;
  58     private static Robot robot;
  59 
  60     public static void main(String[] args) throws Exception {
  61 
  62         robot = new Robot();
  63         robot.delay(2000);
  64         UIManager.LookAndFeelInfo[] lookAndFeelArray
  65                 = UIManager.getInstalledLookAndFeels();
  66         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  67             executeCase(lookAndFeelItem.getClassName());
  68         }
  69     }
  70 
  71     private static void executeCase(String lookAndFeelString)
  72             throws Exception {
  73         if (tryLookAndFeel(lookAndFeelString)) {
  74             createUI(lookAndFeelString);
  75             robot.delay(2000);
  76             runTestCase();
  77             robot.delay(2000);
  78             cleanUp();
  79             robot.delay(2000);
  80         }
  81     }
  82 
  83     private static void createUI(final String lookAndFeelString)
  84             throws Exception {
  85         SwingUtilities.invokeAndWait(new Runnable() {
  86             @Override
  87             public void run() {




  88                 Set<KeyStroke> keystrokes = new HashSet<KeyStroke>();
  89                 keystrokes.add(KeyStroke.getKeyStroke("TAB"));
  90                 keystrokes.add(KeyStroke.getKeyStroke("ENTER"));
  91                 frame = new JFrame("FocusTraversalTest " + lookAndFeelString);
  92                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  93                 frame.setUndecorated(true);
  94                 frame.setFocusTraversalKeys(
  95                         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
  96                         keystrokes);
  97 
  98                 a = new JRadioButton("a");
  99                 b = new JRadioButton("b");
 100                 c = new JRadioButton("c");
 101                 d = new JRadioButton("d");
 102 
 103                 ButtonGroup radioButtonGroup = new ButtonGroup();
 104                 radioButtonGroup.add(a);
 105                 radioButtonGroup.add(b);
 106                 radioButtonGroup.add(c);
 107                 radioButtonGroup.add(d);
 108 
 109                 JPanel panel = new JPanel();
 110                 prev = new JTextField("text");
 111                 panel.add(prev);
 112                 panel.add(a);
 113                 panel.add(b);
 114                 panel.add(c);
 115                 panel.add(d);
 116                 next = new JTextField("text");
 117                 panel.add(next);
 118 
 119                 JPanel root = new JPanel();
 120                 root.setLayout(new BorderLayout());
 121                 root.add(panel, BorderLayout.CENTER);
 122                 root.add(new JButton("OK"), BorderLayout.SOUTH);
 123 
 124                 frame.add(root);
 125                 frame.pack();
 126                 frame.setLocationRelativeTo(null);
 127                 frame.setVisible(true);
 128                 frame.toFront();
 129             }
 130         });





 131     }

 132 
 133     private static void runTestCase() throws Exception {
 134         LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
 135         focusOn(a);
 136         if (isExcludedLookAndFeel(lookAndFeel)) {
 137             robot.keyPress(KeyEvent.VK_ENTER);
 138             robot.keyRelease(KeyEvent.VK_ENTER);
 139             robot.waitForIdle();
 140             isFocusOwner(b, "forward");
 141             robot.keyPress(KeyEvent.VK_SHIFT);
 142             robot.keyPress(KeyEvent.VK_TAB);
 143             robot.keyRelease(KeyEvent.VK_TAB);
 144             robot.keyRelease(KeyEvent.VK_SHIFT);
 145             robot.waitForIdle();
 146             isFocusOwner(a, "backward");
 147 
 148         } else {
 149 
 150             robot.keyPress(KeyEvent.VK_ENTER);
 151             robot.keyRelease(KeyEvent.VK_ENTER);
 152             robot.waitForIdle();
 153             isFocusOwner(next, "forward");
 154             robot.keyPress(KeyEvent.VK_SHIFT);
 155             robot.keyPress(KeyEvent.VK_TAB);
 156             robot.keyRelease(KeyEvent.VK_TAB);
 157             robot.keyRelease(KeyEvent.VK_SHIFT);
 158             robot.waitForIdle();
 159             isFocusOwner(d, "backward");
 160         }
 161 
 162     }
 163 
 164     private static boolean isExcludedLookAndFeel(LookAndFeel lookAndFeel) {
 165 
 166         return lookAndFeel.toString().toLowerCase().contains("aqua")
 167                 || lookAndFeel.toString().toLowerCase().contains("nimbus")
 168                 || lookAndFeel.toString().toLowerCase().contains("gtk");
 169     }
 170 
 171     private static void focusOn(Component component)
 172             throws Exception {
 173         SwingUtilities.invokeAndWait(new Runnable() {
 174             @Override
 175             public void run() {
 176                 component.requestFocusInWindow();





 177             }
 178         });
 179     }
 180 
 181     private static void isFocusOwner(Component queriedFocusOwner,
 182             String direction)
 183             throws Exception {


 184         SwingUtilities.invokeAndWait(new Runnable() {
 185             @Override
 186             public void run() {
 187                 Component actualFocusOwner
 188                         = FocusManager.getCurrentManager().getFocusOwner();
 189                 if (actualFocusOwner != queriedFocusOwner) {
 190                     throw new RuntimeException(
 191                             "Focus component is wrong after " + direction
 192                             + " direction ");
 193 
 194                 }
 195             }
 196         });
 197     }
 198 
 199     private static boolean tryLookAndFeel(String lookAndFeelString)
 200             throws Exception {
 201 
 202         try {
 203             UIManager.setLookAndFeel(
 204                     lookAndFeelString);
 205 
 206         } catch (UnsupportedLookAndFeelException 
 207                 | ClassNotFoundException 
 208                 | InstantiationException 
 209                 | IllegalAccessException e) {
 210             return false;
 211         }
 212         return true;
 213     }
 214 
 215     private static void cleanUp() throws Exception {
 216         SwingUtilities.invokeAndWait(new Runnable() {
 217             @Override
 218             public void run() {
 219                 frame.dispose();
 220             }
 221         });


 222     }
 223 }
< prev index next >