37
37
import java .awt .event .FocusAdapter ;
38
38
import java .awt .event .FocusEvent ;
39
39
import java .awt .event .KeyEvent ;
40
+ import java .util .Arrays ;
40
41
import javax .swing .JFrame ;
41
42
import javax .swing .SwingUtilities ;
42
43
import javax .swing .UIManager ;
50
51
51
52
public class ButtonGroupLayoutTraversalTest {
52
53
53
- static int nx = 3 ;
54
- static int ny = 3 ;
54
+ private static final int NX = 3 ;
55
+ private static final int NY = 3 ;
56
+
57
+ private static final int [] focusCnt = new int [NX * NY ];
55
58
56
- static int focusCnt [] = new int [nx * ny ];
57
59
private static JFrame window ;
60
+ private static Robot robot ;
58
61
59
62
public static void main (String [] args ) throws Exception {
63
+ robot = new Robot ();
64
+ robot .setAutoDelay (100 );
60
65
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 ();
66
+ for (UIManager .LookAndFeelInfo laf : UIManager .getInstalledLookAndFeels ()) {
67
+ try {
68
+ SwingUtilities .invokeAndWait (() -> setLookAndFeel (laf ));
69
+ SwingUtilities .invokeAndWait (() -> initLayout (NX , NY ));
70
+ test ();
71
+ } finally {
72
+ SwingUtilities .invokeAndWait (() -> {
73
+ if (window != null ) {
74
+ window .dispose ();
75
+ }
76
+ window = null ;
77
+ synchronized (focusCnt ) {
78
+ Arrays .fill (focusCnt , 0 );
79
+ }
80
+ });
73
81
}
82
+ }
83
+ }
74
84
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
85
81
- robot .delay (200 );
86
+ private static void test () {
87
+ robot .waitForIdle ();
88
+ robot .delay (1000 );
89
+
90
+ for (int i = 0 ; i < NX * NY - NX * NY / 2 - 1 ; i ++) {
91
+ robot .keyPress (KeyEvent .VK_RIGHT );
92
+ robot .keyRelease (KeyEvent .VK_RIGHT );
93
+ robot .waitForIdle ();
94
+ }
95
+
96
+ for (int i = 0 ; i < NX * NY / 2 ; i ++) {
97
+ robot .keyPress (KeyEvent .VK_TAB );
98
+ robot .keyRelease (KeyEvent .VK_TAB );
99
+ robot .waitForIdle ();
100
+ }
82
101
83
- for (int i = 0 ; i < nx * ny ; i ++) {
102
+ robot .delay (200 );
103
+
104
+ synchronized (focusCnt ) {
105
+ for (int i = 0 ; i < NX * NY ; i ++) {
84
106
if (focusCnt [i ] < 1 ) {
85
107
throw new RuntimeException ("Component " + i
86
- + " is not reachable in the forward focus cycle" );
108
+ + " is not reachable in the forward focus cycle" );
87
109
} else if (focusCnt [i ] > 1 ) {
88
110
throw new RuntimeException ("Component " + i
89
- + " got focus more than once in the forward focus cycle" );
111
+ + " got focus more than once in the forward focus cycle" );
90
112
}
91
113
}
114
+ }
92
115
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
-
116
+ for (int i = 0 ; i < NX * NY / 2 ; i ++) {
107
117
robot .keyPress (KeyEvent .VK_SHIFT );
108
118
robot .keyPress (KeyEvent .VK_TAB );
109
119
robot .keyRelease (KeyEvent .VK_TAB );
110
120
robot .keyRelease (KeyEvent .VK_SHIFT );
111
121
robot .waitForIdle ();
122
+ }
112
123
113
- robot .delay (200 );
124
+ for (int i = 0 ; i < NX * NY - NX * NY / 2 - 1 ; i ++) {
125
+ robot .keyPress (KeyEvent .VK_LEFT );
126
+ robot .keyRelease (KeyEvent .VK_LEFT );
127
+ robot .waitForIdle ();
128
+ }
114
129
115
- for (int i = 0 ; i < nx * ny ; i ++) {
130
+ robot .keyPress (KeyEvent .VK_SHIFT );
131
+ robot .keyPress (KeyEvent .VK_TAB );
132
+ robot .keyRelease (KeyEvent .VK_TAB );
133
+ robot .keyRelease (KeyEvent .VK_SHIFT );
134
+ robot .waitForIdle ();
135
+
136
+ robot .delay (200 );
137
+
138
+ synchronized (focusCnt ) {
139
+ for (int i = 0 ; i < NX * NY ; i ++) {
116
140
if (focusCnt [i ] < 2 ) {
117
141
throw new RuntimeException ("Component " + i
118
- + " is not reachable in the backward focus cycle" );
142
+ + " is not reachable in the backward focus cycle" );
119
143
} else if (focusCnt [i ] > 2 ) {
120
144
throw new RuntimeException ("Component " + i
121
- + " got focus more than once in the backward focus cycle" );
145
+ + " got focus more than once in the backward focus cycle" );
122
146
}
123
147
}
124
- } finally {
125
- SwingUtilities .invokeAndWait (() -> {
126
- if (window != null ) {
127
- window .dispose ();
128
- }
129
- });
130
148
}
149
+
131
150
}
132
151
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
- }
152
+ private static void setLookAndFeel (UIManager .LookAndFeelInfo laf ) {
153
+ try {
154
+ UIManager .setLookAndFeel (laf .getClassName ());
155
+ System .out .println (laf .getName ());
156
+ } catch (UnsupportedLookAndFeelException ignored ){
157
+ System .out .println ("Unsupported LookAndFeel: " + laf .getClassName ());
158
+ } catch (ClassNotFoundException | InstantiationException |
159
+ IllegalAccessException e ) {
160
+ throw new RuntimeException (e );
145
161
}
146
162
}
147
163
@@ -168,16 +184,16 @@ public static void initLayout(int nx, int ny) {
168
184
comp .addFocusListener (new FocusAdapter () {
169
185
@ Override
170
186
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 );
187
+ synchronized ( focusCnt ) {
188
+ focusCnt [fi ]++;
189
+ JComponent btn = ( JComponent ) e .getSource ();
190
+ if ( focusCnt [ fi ] == 1 ) {
191
+ btn . setBackground ( Color . yellow );
192
+ } else if ( focusCnt [ fi ] == 2 ) {
193
+ btn .setBackground (Color .green );
194
+ } else {
195
+ btn . setBackground ( Color . red );
196
+ }
181
197
}
182
198
}
183
199
});
0 commit comments