Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/FocusTraversalPolicy/ButtonGroupLayoutTraversal/ButtonGroupLayoutTraversalTest.java
41154 views
1
/*
2
* Copyright (c) 2016, 2021, 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 8154043 8172509
28
* @summary Fields not reachable anymore by tab-key, because of new tabbing
29
* behaviour of radio button groups.
30
* @run main ButtonGroupLayoutTraversalTest
31
*/
32
33
import java.awt.BorderLayout;
34
import java.awt.Color;
35
import java.awt.GridLayout;
36
import java.awt.Robot;
37
import java.awt.event.FocusAdapter;
38
import java.awt.event.FocusEvent;
39
import java.awt.event.KeyEvent;
40
import javax.swing.JFrame;
41
import javax.swing.SwingUtilities;
42
import javax.swing.UIManager;
43
import javax.swing.JPanel;
44
import javax.swing.ButtonGroup;
45
import javax.swing.JComponent;
46
import javax.swing.JRadioButton;
47
import javax.swing.JToggleButton;
48
import javax.swing.LayoutFocusTraversalPolicy;
49
import javax.swing.UnsupportedLookAndFeelException;
50
51
public class ButtonGroupLayoutTraversalTest {
52
53
static int nx = 3;
54
static int ny = 3;
55
56
static int focusCnt[] = new int[nx * ny];
57
private static JFrame window;
58
59
public static void main(String[] args) throws Exception {
60
61
try {
62
SwingUtilities.invokeAndWait(() -> changeLAF());
63
SwingUtilities.invokeAndWait(() -> initLayout(nx, ny));
64
Robot robot = new Robot();
65
robot.setAutoDelay(100);
66
robot.waitForIdle();
67
robot.delay(1000);
68
69
for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
70
robot.keyPress(KeyEvent.VK_RIGHT);
71
robot.keyRelease(KeyEvent.VK_RIGHT);
72
robot.waitForIdle();
73
}
74
75
for (int i = 0; i < nx * ny / 2; i++) {
76
robot.keyPress(KeyEvent.VK_TAB);
77
robot.keyRelease(KeyEvent.VK_TAB);
78
robot.waitForIdle();
79
}
80
81
robot.delay(200);
82
83
for (int i = 0; i < nx * ny; i++) {
84
if (focusCnt[i] < 1) {
85
throw new RuntimeException("Component " + i
86
+ " is not reachable in the forward focus cycle");
87
} else if (focusCnt[i] > 1) {
88
throw new RuntimeException("Component " + i
89
+ " got focus more than once in the forward focus cycle");
90
}
91
}
92
93
for (int i = 0; i < nx * ny / 2; i++) {
94
robot.keyPress(KeyEvent.VK_SHIFT);
95
robot.keyPress(KeyEvent.VK_TAB);
96
robot.keyRelease(KeyEvent.VK_TAB);
97
robot.keyRelease(KeyEvent.VK_SHIFT);
98
robot.waitForIdle();
99
}
100
101
for (int i = 0; i < nx * ny - nx * ny / 2 - 1; i++) {
102
robot.keyPress(KeyEvent.VK_LEFT);
103
robot.keyRelease(KeyEvent.VK_LEFT);
104
robot.waitForIdle();
105
}
106
107
robot.keyPress(KeyEvent.VK_SHIFT);
108
robot.keyPress(KeyEvent.VK_TAB);
109
robot.keyRelease(KeyEvent.VK_TAB);
110
robot.keyRelease(KeyEvent.VK_SHIFT);
111
robot.waitForIdle();
112
113
robot.delay(200);
114
115
for (int i = 0; i < nx * ny; i++) {
116
if (focusCnt[i] < 2) {
117
throw new RuntimeException("Component " + i
118
+ " is not reachable in the backward focus cycle");
119
} else if (focusCnt[i] > 2) {
120
throw new RuntimeException("Component " + i
121
+ " got focus more than once in the backward focus cycle");
122
}
123
}
124
} finally {
125
SwingUtilities.invokeAndWait(() -> {
126
if (window != null) {
127
window.dispose();
128
}
129
});
130
}
131
}
132
133
private static void changeLAF() {
134
String currentLAF = UIManager.getLookAndFeel().toString();
135
currentLAF = currentLAF.toLowerCase();
136
if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) {
137
try {
138
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
139
} catch (ClassNotFoundException
140
| IllegalAccessException
141
| InstantiationException
142
| UnsupportedLookAndFeelException ex) {
143
ex.printStackTrace();
144
}
145
}
146
}
147
148
public static void initLayout(int nx, int ny) {
149
window = new JFrame("Test");
150
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
151
JPanel rootPanel = new JPanel();
152
rootPanel.setLayout(new BorderLayout());
153
JPanel formPanel = new JPanel(new GridLayout(nx, ny));
154
formPanel.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
155
formPanel.setFocusCycleRoot(true);
156
ButtonGroup radioButtonGroup = new ButtonGroup();
157
for (int i = 0; i < nx * ny; i++) {
158
JToggleButton comp;
159
if (i % 2 == 0) {
160
comp = new JRadioButton("Grouped component");
161
radioButtonGroup.add(comp);
162
} else {
163
comp = new JRadioButton("Single component");
164
}
165
formPanel.add(comp);
166
int fi = i;
167
comp.setBackground(Color.red);
168
comp.addFocusListener(new FocusAdapter() {
169
@Override
170
public void focusGained(FocusEvent e) {
171
focusCnt[fi]++;
172
if (focusCnt[fi] == 1) {
173
((JComponent) e.getSource())
174
.setBackground(Color.yellow);
175
} else if (focusCnt[fi] == 2) {
176
((JComponent) e.getSource())
177
.setBackground(Color.green);
178
} else {
179
((JComponent) e.getSource())
180
.setBackground(Color.red);
181
}
182
}
183
});
184
}
185
rootPanel.add(formPanel, BorderLayout.CENTER);
186
window.add(rootPanel);
187
window.setLocationRelativeTo(null);
188
window.pack();
189
window.setVisible(true);
190
}
191
}
192
193