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