Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JTable/6263446/bug6263446.java
41155 views
1
/*
2
* Copyright (c) 2011, 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 6263446
28
* @summary Tests that double-clicking to edit a cell doesn't select the content.
29
* @author Shannon Hickey
30
* @run main bug6263446
31
*/
32
import java.awt.Point;
33
import java.awt.Robot;
34
import java.awt.Rectangle;
35
import java.awt.event.InputEvent;
36
import javax.swing.DefaultCellEditor;
37
import javax.swing.JFrame;
38
import javax.swing.JTable;
39
import javax.swing.JTextField;
40
import javax.swing.SwingUtilities;
41
import javax.swing.table.TableModel;
42
import javax.swing.table.DefaultTableModel;
43
44
public class bug6263446 {
45
46
private static JFrame frame;
47
private static JTable table;
48
private static final String FIRST = "AAAAA";
49
private static final String SECOND = "BB";
50
private static final String ALL = FIRST + " " + SECOND;
51
private static Robot robot;
52
53
public static void main(String[] args) throws Exception {
54
try {
55
robot = new Robot();
56
robot.setAutoDelay(50);
57
58
SwingUtilities.invokeAndWait(() -> createAndShowGUI());
59
robot.waitForIdle();
60
robot.delay(1000);
61
62
Point point = getClickPoint();
63
robot.mouseMove(point.x, point.y);
64
robot.waitForIdle();
65
66
click(1);
67
robot.waitForIdle();
68
assertEditing(false);
69
70
click(2);
71
robot.waitForIdle();
72
checkSelectedText(null);
73
74
click(3);
75
robot.waitForIdle();
76
checkSelectedText(FIRST);
77
78
click(4);
79
robot.waitForIdle();
80
checkSelectedText(ALL);
81
82
setClickCountToStart(1);
83
robot.waitForIdle();
84
85
click(1);
86
robot.waitForIdle();
87
checkSelectedText(null);
88
89
click(2);
90
robot.waitForIdle();
91
checkSelectedText(FIRST);
92
93
click(3);
94
robot.waitForIdle();
95
checkSelectedText(ALL);
96
97
setClickCountToStart(3);
98
robot.waitForIdle();
99
100
click(1);
101
robot.waitForIdle();
102
assertEditing(false);
103
104
click(2);
105
robot.waitForIdle();
106
assertEditing(false);
107
108
click(3);
109
robot.waitForIdle();
110
checkSelectedText(null);
111
112
click(4);
113
robot.waitForIdle();
114
checkSelectedText(FIRST);
115
116
click(5);
117
robot.waitForIdle();
118
checkSelectedText(ALL);
119
120
SwingUtilities.invokeAndWait(() -> table.editCellAt(0, 0));
121
122
robot.waitForIdle();
123
assertEditing(true);
124
125
click(2);
126
robot.waitForIdle();
127
checkSelectedText(FIRST);
128
} finally {
129
if (frame != null) {
130
SwingUtilities.invokeAndWait(frame::dispose);
131
}
132
}
133
}
134
135
private static void checkSelectedText(String sel) throws Exception {
136
assertEditing(true);
137
checkSelection(sel);
138
robot.waitForIdle();
139
cancelCellEditing();
140
robot.waitForIdle();
141
assertEditing(false);
142
}
143
144
private static void setClickCountToStart(final int clicks) throws Exception {
145
SwingUtilities.invokeAndWait(() -> {
146
DefaultCellEditor editor =
147
(DefaultCellEditor) table.getDefaultEditor(String.class);
148
editor.setClickCountToStart(clicks);
149
});
150
}
151
152
private static void cancelCellEditing() throws Exception {
153
SwingUtilities.invokeAndWait(() -> {
154
table.getCellEditor().cancelCellEditing();
155
});
156
}
157
158
private static void checkSelection(final String sel) throws Exception {
159
SwingUtilities.invokeAndWait(() -> {
160
DefaultCellEditor editor =
161
(DefaultCellEditor) table.getDefaultEditor(String.class);
162
JTextField field = (JTextField) editor.getComponent();
163
String text = field.getSelectedText();
164
if (sel == null) {
165
if (text != null && text.length() != 0) {
166
throw new RuntimeException("Nothing should be selected,"
167
+ " but \"" + text + "\" is selected.");
168
}
169
} else if (!sel.equals(text)) {
170
throw new RuntimeException("\"" + sel + "\" should be "
171
+ "selected, but \"" + text + "\" is selected.");
172
}
173
});
174
}
175
176
private static void assertEditing(final boolean editing) throws Exception {
177
SwingUtilities.invokeAndWait(() -> {
178
if (editing && !table.isEditing()) {
179
throw new RuntimeException("Table should be editing");
180
}
181
if (!editing && table.isEditing()) {
182
throw new RuntimeException("Table should not be editing");
183
}
184
});
185
}
186
187
private static Point getClickPoint() throws Exception {
188
final Point[] result = new Point[1];
189
SwingUtilities.invokeAndWait(() -> {
190
Rectangle rect = table.getCellRect(0, 0, false);
191
Point point = new Point(rect.x + rect.width / 5,
192
rect.y + rect.height / 2);
193
SwingUtilities.convertPointToScreen(point, table);
194
result[0] = point;
195
});
196
return result[0];
197
}
198
199
private static void click(int times) {
200
robot.delay(500);
201
for (int i = 0; i < times; i++) {
202
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
203
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
204
}
205
}
206
207
private static TableModel createTableModel() {
208
String[] columnNames = {"Column 0"};
209
String[][] data = {{ALL}};
210
211
return new DefaultTableModel(data, columnNames);
212
}
213
214
private static void createAndShowGUI() {
215
frame = new JFrame("bug6263446");
216
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
217
table = new JTable(createTableModel());
218
frame.add(table);
219
frame.setAlwaysOnTop(true);
220
frame.setLocationRelativeTo(null);
221
frame.pack();
222
frame.setVisible(true);
223
frame.toFront();
224
}
225
}
226
227