Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JTable/7124218/SelectEditTableCell.java
41153 views
1
/*
2
* Copyright (c) 2015, 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 7124218
28
* @summary verifies different behaviour of SPACE and ENTER in JTable
29
* @library ../../regtesthelpers
30
* @build Util
31
* @run main SelectEditTableCell
32
*/
33
import java.awt.Point;
34
import java.awt.Robot;
35
import java.awt.event.InputEvent;
36
import java.awt.event.KeyEvent;
37
import javax.swing.DefaultListSelectionModel;
38
import javax.swing.JFrame;
39
import javax.swing.JTable;
40
import javax.swing.LookAndFeel;
41
import javax.swing.SwingUtilities;
42
import javax.swing.UIManager;
43
import javax.swing.UnsupportedLookAndFeelException;
44
45
public class SelectEditTableCell {
46
47
private static JFrame frame;
48
private static JTable table;
49
private static Robot robot;
50
51
public static void main(String[] args) throws Exception {
52
robot = new Robot();
53
robot.setAutoDelay(100);
54
UIManager.LookAndFeelInfo[] lookAndFeelArray
55
= UIManager.getInstalledLookAndFeels();
56
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
57
executeCase(lookAndFeelItem.getClassName());
58
}
59
}
60
61
private static void executeCase(String lookAndFeelString) throws Exception {
62
try {
63
if (tryLookAndFeel(lookAndFeelString)) {
64
createUI(lookAndFeelString);
65
robot.delay(2000);
66
runTestCase();
67
robot.delay(2000);
68
cleanUp();
69
robot.delay(2000);
70
}
71
} finally {
72
if (frame != null) {
73
SwingUtilities.invokeAndWait(frame::dispose);
74
}
75
}
76
77
}
78
79
private static void createUI(final String lookAndFeelString)
80
throws Exception {
81
SwingUtilities.invokeAndWait(new Runnable() {
82
@Override
83
public void run() {
84
String[][] data = {{"Foo"}};
85
String[] cols = {"One"};
86
table = new JTable(data, cols);
87
table.setSelectionMode(
88
DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
89
frame = new JFrame(lookAndFeelString);
90
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
91
frame.getContentPane().add(table);
92
frame.pack();
93
frame.setSize(500, frame.getSize().height);
94
frame.setLocationRelativeTo(null);
95
frame.setVisible(true);
96
frame.toFront();
97
}
98
});
99
}
100
101
private static void runTestCase() throws Exception {
102
Point centerPoint;
103
centerPoint = Util.getCenterPoint(table);
104
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
105
robot.mouseMove(centerPoint.x, centerPoint.y);
106
robot.mousePress(InputEvent.BUTTON1_MASK);
107
robot.mouseRelease(InputEvent.BUTTON1_MASK);
108
robot.waitForIdle();
109
SwingUtilities.invokeAndWait(new Runnable() {
110
@Override
111
public void run() {
112
table.clearSelection();
113
if (table.isEditing() || table.isCellSelected(0, 0)) {
114
// assumption is bad, bail
115
frame.dispose();
116
throw new AssertionError("Failed assumption: assumed no"
117
+ "editing and no selection.");
118
}
119
}
120
});
121
122
int fetchKeyCode;
123
keyTap(fetchKeyCode = isMac(lookAndFeel)
124
? KeyEvent.VK_ENTER : KeyEvent.VK_SPACE);
125
final int keyCode = fetchKeyCode;
126
robot.waitForIdle();
127
SwingUtilities.invokeAndWait(new Runnable() {
128
@Override
129
public void run() {
130
if (!table.isCellSelected(0, 0)) {
131
frame.dispose();
132
throw new RuntimeException(((keyCode == KeyEvent.VK_ENTER)
133
? "Enter" : "Space")
134
+ " should select cell");
135
}
136
}
137
});
138
139
keyTap(KeyEvent.VK_SPACE);
140
robot.waitForIdle();
141
SwingUtilities.invokeAndWait(new Runnable() {
142
@Override
143
public void run() {
144
if (!table.isEditing()) {
145
frame.dispose();
146
throw new RuntimeException("Space should start editing");
147
}
148
table.getCellEditor().cancelCellEditing();
149
table.clearSelection();
150
if (table.isEditing() || table.isCellSelected(0, 0)) {
151
// assumption is bad, bail
152
frame.dispose();
153
throw new AssertionError("Failed assumption: assumed no "
154
+ "editing and no selection.");
155
}
156
}
157
});
158
159
// hitting a letter key will start editing
160
keyTap(KeyEvent.VK_A);
161
robot.waitForIdle();
162
keyTap(KeyEvent.VK_SPACE);
163
robot.waitForIdle();
164
keyTap(KeyEvent.VK_A);
165
robot.waitForIdle();
166
SwingUtilities.invokeAndWait(new Runnable() {
167
@Override
168
public void run() {
169
if (table.isCellSelected(0, 0)) {
170
frame.dispose();
171
throw new RuntimeException("Space should not select when "
172
+ "already editing.");
173
}
174
}
175
});
176
}
177
178
private static void cleanUp() throws Exception {
179
SwingUtilities.invokeAndWait(new Runnable() {
180
@Override
181
public void run() {
182
frame.dispose();
183
}
184
});
185
}
186
187
private static boolean isMac(LookAndFeel lookAndFeel) {
188
189
return lookAndFeel.toString().toLowerCase().contains("mac");
190
}
191
192
private static void keyTap(int keyCode) {
193
robot.keyPress(keyCode);
194
robot.keyRelease(keyCode);
195
}
196
197
private static boolean tryLookAndFeel(String lookAndFeelString)
198
throws Exception {
199
try {
200
UIManager.setLookAndFeel(
201
lookAndFeelString);
202
203
} catch (UnsupportedLookAndFeelException
204
| ClassNotFoundException
205
| InstantiationException
206
| IllegalAccessException e) {
207
return false;
208
}
209
return true;
210
}
211
}
212
213