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