Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/ButtonGroup/TestButtonGroupFocusTraversal.java
41152 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
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
/*
25
* @test
26
* @key headful
27
* @bug 8249548 8259237
28
* @summary Test focus traversal in button group containing JToggleButton
29
* and JRadioButton
30
* @run main TestButtonGroupFocusTraversal
31
*/
32
33
import javax.swing.AbstractAction;
34
import javax.swing.ButtonGroup;
35
import javax.swing.JCheckBox;
36
import javax.swing.JFrame;
37
import javax.swing.JRadioButton;
38
import javax.swing.JTextField;
39
import javax.swing.JToggleButton;
40
import javax.swing.SwingUtilities;
41
import javax.swing.UIManager;
42
import java.awt.Component;
43
import java.awt.Container;
44
import java.awt.FlowLayout;
45
import java.awt.KeyboardFocusManager;
46
import java.awt.Point;
47
import java.awt.Robot;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.KeyEvent;
50
51
public class TestButtonGroupFocusTraversal {
52
private static JFrame frame;
53
private static JTextField textFieldFirst, textFieldLast;
54
private static JToggleButton toggleButton1, toggleButton2;
55
private static JCheckBox checkBox1, checkBox2;
56
private static boolean toggleButtonActionPerformed;
57
private static boolean checkboxActionPerformed;
58
private static JRadioButton radioButton1, radioButton2;
59
private static Robot robot;
60
61
private static void blockTillDisplayed(Component comp) {
62
Point p = null;
63
while (p == null) {
64
try {
65
p = comp.getLocationOnScreen();
66
} catch (IllegalStateException e) {
67
try {
68
Thread.sleep(500);
69
} catch (InterruptedException ie) {
70
}
71
}
72
}
73
}
74
75
private static void createUI() throws Exception {
76
SwingUtilities.invokeAndWait(new Runnable() {
77
public void run() {
78
textFieldFirst = new JTextField("First");
79
textFieldLast = new JTextField("Last");
80
toggleButton1 = new JToggleButton("1");
81
toggleButton2 = new JToggleButton("2");
82
radioButton1 = new JRadioButton("1");
83
radioButton2 = new JRadioButton("2");
84
checkBox1 = new JCheckBox("1");
85
checkBox2 = new JCheckBox("2");
86
87
toggleButton1.setAction(new AbstractAction() {
88
@Override
89
public void actionPerformed(ActionEvent e) {
90
toggleButtonActionPerformed = true;
91
}
92
});
93
94
toggleButton2.setAction(new AbstractAction() {
95
@Override
96
public void actionPerformed(ActionEvent e) {
97
toggleButtonActionPerformed = true;
98
}
99
});
100
101
checkBox1.setAction(new AbstractAction() {
102
@Override
103
public void actionPerformed(ActionEvent e) {
104
checkboxActionPerformed = true;
105
}
106
});
107
108
checkBox2.setAction(new AbstractAction() {
109
@Override
110
public void actionPerformed(ActionEvent e) {
111
checkboxActionPerformed = true;
112
}
113
});
114
115
ButtonGroup toggleGroup = new ButtonGroup();
116
toggleGroup.add(toggleButton1);
117
toggleGroup.add(toggleButton2);
118
119
ButtonGroup radioGroup = new ButtonGroup();
120
radioGroup.add(radioButton1);
121
radioGroup.add(radioButton2);
122
123
ButtonGroup checkboxButtonGroup = new ButtonGroup();
124
checkboxButtonGroup.add(checkBox1);
125
checkboxButtonGroup.add(checkBox2);
126
127
toggleButton2.setSelected(true);
128
radioButton2.setSelected(true);
129
checkBox2.setSelected(true);
130
131
frame = new JFrame("Test");
132
frame.setLayout(new FlowLayout());
133
134
Container pane = frame.getContentPane();
135
pane.add(textFieldFirst);
136
pane.add(toggleButton1);
137
pane.add(toggleButton2);
138
pane.add(radioButton1);
139
pane.add(radioButton2);
140
pane.add(checkBox1);
141
pane.add(checkBox2);
142
pane.add(textFieldLast);
143
144
frame.pack();
145
frame.setAlwaysOnTop(true);
146
frame.setLocationRelativeTo(null);
147
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
148
frame.setVisible(true);
149
}
150
});
151
}
152
153
private static void pressKey(int ...keys) {
154
int num = keys.length;
155
for (int i=0; i<num; i++)
156
robot.keyPress(keys[i]);
157
for (int i=num; i>0; i--)
158
robot.keyRelease(keys[i-1]);
159
160
robot.waitForIdle();
161
robot.delay(200);
162
}
163
164
private static void checkFocusedComponent (Component component) {
165
Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
166
if (!focusedComponent.equals(component)) {
167
System.out.println(component);
168
System.out.println(focusedComponent);
169
throw new RuntimeException("Wrong Component Selected");
170
}
171
}
172
173
private static void checkToggleButtonActionPerformed() {
174
if (toggleButtonActionPerformed) {
175
throw new RuntimeException("Toggle Button Action should not be" +
176
"performed");
177
}
178
}
179
180
private static void checkCheckboxActionPerformed() {
181
if (toggleButtonActionPerformed) {
182
throw new RuntimeException("Checkbox Action should not be" +
183
"performed");
184
}
185
}
186
187
public static void main(String[] args) throws Exception {
188
robot = new Robot();
189
robot.setAutoDelay(100);
190
191
UIManager.LookAndFeelInfo infos[] = UIManager.getInstalledLookAndFeels();
192
for (UIManager.LookAndFeelInfo info : infos) {
193
UIManager.setLookAndFeel(info.getClassName());
194
System.out.println(info.getClassName());
195
try {
196
createUI();
197
198
robot.waitForIdle();
199
robot.delay(200);
200
201
blockTillDisplayed(frame);
202
203
SwingUtilities.invokeAndWait(textFieldFirst::requestFocus);
204
205
if (!textFieldFirst.equals(KeyboardFocusManager.getCurrentKeyboardFocusManager()
206
.getFocusOwner())) {
207
try {
208
Thread.sleep(100);
209
} catch (InterruptedException e) {
210
e.printStackTrace();
211
}
212
SwingUtilities.invokeAndWait(textFieldFirst::requestFocus);
213
}
214
215
robot.waitForIdle();
216
robot.delay(200);
217
218
pressKey(KeyEvent.VK_TAB);
219
checkFocusedComponent(toggleButton2);
220
221
pressKey(KeyEvent.VK_TAB);
222
checkFocusedComponent(radioButton2);
223
224
pressKey(KeyEvent.VK_TAB);
225
checkFocusedComponent(checkBox2);
226
227
pressKey(KeyEvent.VK_TAB);
228
checkFocusedComponent(textFieldLast);
229
230
pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
231
checkFocusedComponent(checkBox2);
232
233
pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
234
checkFocusedComponent(radioButton2);
235
236
pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
237
checkFocusedComponent(toggleButton2);
238
239
pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
240
checkFocusedComponent(textFieldFirst);
241
242
pressKey(KeyEvent.VK_TAB);
243
checkFocusedComponent(toggleButton2);
244
245
pressKey(KeyEvent.VK_LEFT);
246
checkFocusedComponent(toggleButton1);
247
checkToggleButtonActionPerformed();
248
249
pressKey(KeyEvent.VK_RIGHT);
250
checkFocusedComponent(toggleButton2);
251
checkToggleButtonActionPerformed();
252
253
pressKey(KeyEvent.VK_UP);
254
checkFocusedComponent(toggleButton1);
255
checkToggleButtonActionPerformed();
256
257
pressKey(KeyEvent.VK_DOWN);
258
checkFocusedComponent(toggleButton2);
259
checkToggleButtonActionPerformed();
260
261
pressKey(KeyEvent.VK_TAB);
262
checkFocusedComponent(radioButton2);
263
264
pressKey(KeyEvent.VK_LEFT);
265
checkFocusedComponent(radioButton1);
266
267
pressKey(KeyEvent.VK_RIGHT);
268
checkFocusedComponent(radioButton2);
269
270
pressKey(KeyEvent.VK_UP);
271
checkFocusedComponent(radioButton1);
272
273
pressKey(KeyEvent.VK_DOWN);
274
checkFocusedComponent(radioButton2);
275
276
pressKey(KeyEvent.VK_TAB);
277
checkFocusedComponent(checkBox2);
278
279
pressKey(KeyEvent.VK_LEFT);
280
checkCheckboxActionPerformed();
281
checkFocusedComponent(checkBox1);
282
283
pressKey(KeyEvent.VK_RIGHT);
284
checkCheckboxActionPerformed();
285
checkFocusedComponent(checkBox2);
286
287
pressKey(KeyEvent.VK_UP);
288
checkCheckboxActionPerformed();
289
checkFocusedComponent(checkBox1);
290
291
pressKey(KeyEvent.VK_DOWN);
292
checkCheckboxActionPerformed();
293
checkFocusedComponent(checkBox2);
294
} finally {
295
if (frame != null) {
296
SwingUtilities.invokeAndWait(frame::dispose);
297
}
298
}
299
}
300
}
301
}
302
303