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