Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JSpinner/TestJSpinnerFocusLost.java
41149 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 4840869
28
* @summary JSpinner keeps spinning while JOptionPane is shown on ChangeListener
29
* @run main TestJSpinnerFocusLost
30
*/
31
32
import java.awt.Component;
33
import java.awt.Point;
34
import java.awt.Rectangle;
35
import java.awt.Robot;
36
import java.awt.event.InputEvent;
37
import java.awt.event.FocusEvent;
38
import java.awt.event.FocusListener;
39
import javax.swing.JFrame;
40
import javax.swing.JSpinner;
41
import javax.swing.JOptionPane;
42
import javax.swing.SpinnerNumberModel;
43
import javax.swing.SwingUtilities;
44
import javax.swing.event.ChangeEvent;
45
import javax.swing.event.ChangeListener;
46
import javax.swing.UIManager;
47
import javax.swing.UnsupportedLookAndFeelException;
48
49
public class TestJSpinnerFocusLost extends JFrame implements ChangeListener, FocusListener {
50
51
JSpinner spinner;
52
53
boolean spinnerGainedFocus = false;
54
boolean spinnerLostFocus = false;
55
56
static TestJSpinnerFocusLost b;
57
Point p;
58
Rectangle rect;
59
static Robot robot;
60
61
public 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(1000);
69
} catch (InterruptedException ie) {
70
}
71
}
72
}
73
}
74
75
public TestJSpinnerFocusLost() {
76
spinner = new JSpinner(new SpinnerNumberModel(10, 1, 100, 1));
77
spinner.addChangeListener(this);
78
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addFocusListener(this);
79
getContentPane().add(spinner);
80
}
81
82
public void doTest() throws Exception {
83
blockTillDisplayed(spinner);
84
SwingUtilities.invokeAndWait(() -> {
85
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().requestFocus();
86
});
87
88
try {
89
synchronized (TestJSpinnerFocusLost.this) {
90
if (!spinnerGainedFocus) {
91
TestJSpinnerFocusLost.this.wait(2000);
92
}
93
}
94
95
96
SwingUtilities.invokeAndWait(() -> {
97
p = spinner.getLocationOnScreen();
98
rect = spinner.getBounds();
99
});
100
robot.delay(1000);
101
robot.mouseMove(p.x+rect.width-5, p.y+3);
102
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
103
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
104
105
synchronized (TestJSpinnerFocusLost.this) {
106
while (!spinnerLostFocus) {
107
TestJSpinnerFocusLost.this.wait(2000);
108
}
109
}
110
111
} catch(Exception ex) {
112
ex.printStackTrace();
113
}
114
115
if ( ((Integer) spinner.getValue()).intValue() != 11 ) {
116
System.out.println("spinner value " + ((Integer) spinner.getValue()).intValue());
117
throw new RuntimeException("Spinner value shouldn't be other than 11");
118
}
119
}
120
121
122
private boolean changing = false;
123
124
public void stateChanged(ChangeEvent e) {
125
if (changing) {
126
return;
127
}
128
JSpinner spinner = (JSpinner)e.getSource();
129
int value = ((Integer) spinner.getValue()).intValue();
130
if (value > 10) {
131
changing = true;
132
JOptionPane.showMessageDialog(spinner, "10 exceeded");
133
}
134
}
135
136
public void focusGained(FocusEvent e) {
137
synchronized (TestJSpinnerFocusLost.this) {
138
spinnerGainedFocus = true;
139
TestJSpinnerFocusLost.this.notifyAll();
140
}
141
}
142
143
public void focusLost(FocusEvent e) {
144
synchronized (TestJSpinnerFocusLost.this) {
145
spinnerLostFocus = true;
146
TestJSpinnerFocusLost.this.notifyAll();
147
}
148
}
149
150
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
151
try {
152
UIManager.setLookAndFeel(laf.getClassName());
153
} catch (UnsupportedLookAndFeelException ignored) {
154
System.out.println("Unsupported L&F: " + laf.getClassName());
155
} catch (ClassNotFoundException | InstantiationException
156
| IllegalAccessException e) {
157
throw new RuntimeException(e);
158
}
159
}
160
161
public static void main(String[] argv) throws Exception {
162
robot = new Robot();
163
robot.setAutoWaitForIdle(true);
164
robot.setAutoDelay(250);
165
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
166
System.out.println("Testing L&F: " + laf.getClassName());
167
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
168
try {
169
SwingUtilities.invokeAndWait(() -> {
170
b = new TestJSpinnerFocusLost();
171
b.pack();
172
b.setLocationRelativeTo(null);
173
b.setVisible(true);
174
});
175
robot.waitForIdle();
176
b.doTest();
177
robot.delay(500);
178
} finally {
179
SwingUtilities.invokeAndWait(() -> {
180
if (b != null) b.dispose();
181
});
182
}
183
robot.delay(1000);
184
}
185
}
186
}
187
188