Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/4330357/bug4330357.java
38854 views
1
/*
2
* Copyright (c) 2012, 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
* @bug 4330357
27
* @summary Tests that real editor in JTree cleans up after editing was stopped
28
* @library ../../regtesthelpers
29
* @build Util
30
* @author Peter Zhelezniakov
31
* @run main bug4330357
32
*/
33
import java.awt.*;
34
import java.awt.event.*;
35
import javax.swing.*;
36
import javax.swing.tree.*;
37
38
public class bug4330357 {
39
40
private static JTree tree;
41
private static JButton button;
42
private static Robot robot;
43
44
public static void main(String[] args) throws Exception {
45
robot = new Robot();
46
robot.setAutoDelay(50);
47
48
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
49
50
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
51
52
public void run() {
53
createAndShowGUI();
54
}
55
});
56
57
robot.waitForIdle();
58
59
clickMouse(getTreeRowClickPoint(1));
60
Util.hitKeys(robot, KeyEvent.VK_F2);
61
Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C);
62
robot.waitForIdle();
63
64
if (!hasComponent(JTextField.class)) {
65
throw new RuntimeException("Cell editor is missed for path: color");
66
}
67
68
69
clickMouse(getButtonClickPoint());
70
robot.waitForIdle();
71
72
clickMouse(getTreeRowClickPoint(2));
73
Util.hitKeys(robot, KeyEvent.VK_F2);
74
robot.waitForIdle();
75
76
if (!hasComponent(JComboBox.class)) {
77
throw new RuntimeException("Cell editor is missed for path: sports");
78
}
79
80
if (hasComponent(JTextField.class)) {
81
throw new RuntimeException("Cell editor is wrongly shown for path: color");
82
}
83
}
84
85
static void clickMouse(Point point) {
86
robot.mouseMove(point.x, point.y);
87
robot.mousePress(InputEvent.BUTTON1_MASK);
88
robot.mouseRelease(InputEvent.BUTTON1_MASK);
89
}
90
91
private static Point getTreeRowClickPoint(final int row) throws Exception {
92
final Point[] result = new Point[1];
93
94
SwingUtilities.invokeAndWait(new Runnable() {
95
96
@Override
97
public void run() {
98
99
Rectangle rect = tree.getRowBounds(row);
100
Point p = new Point(rect.x + rect.width / 2, rect.y + 2);
101
SwingUtilities.convertPointToScreen(p, tree);
102
result[0] = p;
103
}
104
});
105
106
return result[0];
107
}
108
109
private static Point getButtonClickPoint() throws Exception {
110
final Point[] result = new Point[1];
111
112
SwingUtilities.invokeAndWait(new Runnable() {
113
114
@Override
115
public void run() {
116
Point p = button.getLocationOnScreen();
117
Dimension size = button.getSize();
118
result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
119
}
120
});
121
return result[0];
122
}
123
124
static boolean hasComponent(final Class cls) throws Exception {
125
final boolean[] result = new boolean[1];
126
127
SwingUtilities.invokeAndWait(new Runnable() {
128
129
@Override
130
public void run() {
131
result[0] = Util.findSubComponent(tree, cls.getName()) != null;
132
}
133
});
134
135
return result[0];
136
}
137
138
private static void createAndShowGUI() {
139
JFrame frame = new JFrame("Test");
140
frame.setSize(200, 200);
141
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
142
143
tree = new JTree();
144
tree.setEditable(true);
145
146
final TestEditor testEditor = new TestEditor();
147
tree.setCellEditor(new DefaultTreeCellEditor(tree,
148
(DefaultTreeCellRenderer) tree.getCellRenderer(),
149
testEditor));
150
151
button = new JButton("stop");
152
153
button.addActionListener(new ActionListener() {
154
155
public void actionPerformed(ActionEvent ae) {
156
testEditor.stopCellEditing();
157
}
158
});
159
160
frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
161
frame.getContentPane().add(button, BorderLayout.SOUTH);
162
frame.setVisible(true);
163
}
164
165
static class TestEditor extends AbstractCellEditor implements TreeCellEditor {
166
167
private JComboBox comboBox;
168
private JTextField textField;
169
private boolean comboBoxActive;
170
171
TestEditor() {
172
comboBox = new JComboBox(new String[]{"one", "two"});
173
textField = new JTextField();
174
}
175
176
public Component getTreeCellEditorComponent(JTree tree, Object value,
177
boolean isSelected,
178
boolean expanded,
179
boolean leaf, int row) {
180
if (row % 2 == 0) {
181
comboBoxActive = true;
182
return comboBox;
183
}
184
comboBoxActive = false;
185
return textField;
186
}
187
188
public Object getCellEditorValue() {
189
if (comboBoxActive) {
190
return comboBox.getSelectedItem();
191
}
192
return textField.getText();
193
}
194
}
195
}
196
197