1 /* 2 * Copyright (c) 2013, 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 8023474 28 * @summary Tests that the first mouse press starts editing in JTree 29 * @author Dmitry Markov 30 * @run main bug8023474 31 */ 32 33 import sun.awt.SunToolkit; 34 35 import javax.swing.*; 36 import javax.swing.event.CellEditorListener; 37 import javax.swing.tree.DefaultMutableTreeNode; 38 import javax.swing.tree.DefaultTreeModel; 39 import javax.swing.tree.TreeCellEditor; 40 import javax.swing.tree.TreeCellRenderer; 41 import java.awt.*; 42 import java.awt.event.InputEvent; 43 import java.util.EventObject; 44 45 public class bug8023474 { 46 private static JTree tree; 47 48 public static void main(String[] args) throws Exception { 49 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 50 Robot robot = new Robot(); 51 robot.setAutoDelay(50); 52 53 SwingUtilities.invokeAndWait(new Runnable() { 54 public void run() { 55 createAndShowGUI(); 56 } 57 }); 58 59 toolkit.realSync(); 60 61 Point point = getRowPointToClick(1); 62 robot.mouseMove(point.x, point.y); 63 robot.mousePress(InputEvent.BUTTON1_MASK); 64 robot.mouseRelease(InputEvent.BUTTON1_MASK); 65 66 toolkit.realSync(); 67 68 Boolean result = (Boolean)tree.getCellEditor().getCellEditorValue(); 69 if (!result) { 70 throw new RuntimeException("Test Failed!"); 71 } 72 } 73 74 private static void createAndShowGUI() { 75 try { 76 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 77 } catch (Exception e) { 78 throw new RuntimeException(e); 79 } 80 81 DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); 82 DefaultMutableTreeNode item = new DefaultMutableTreeNode("item"); 83 DefaultMutableTreeNode subItem = new DefaultMutableTreeNode("subItem"); 84 85 root.add(item); 86 item.add(subItem); 87 88 DefaultTreeModel model = new DefaultTreeModel(root); 89 tree = new JTree(model); 90 91 tree.setCellEditor(new Editor()); 92 tree.setEditable(true); 93 tree.setRowHeight(30); 94 tree.setCellRenderer(new CheckboxCellRenderer()); 95 96 JFrame frame = new JFrame("bug8023474"); 97 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 98 frame.add(new JScrollPane(tree)); 99 frame.setSize(400, 300); 100 frame.setVisible(true); 101 } 102 103 private static Point getRowPointToClick(final int row) throws Exception { 104 final Point[] result = new Point[1]; 105 106 SwingUtilities.invokeAndWait(new Runnable() { 107 public void run() { 108 Rectangle rect = tree.getRowBounds(row); 109 Point point = new Point(rect.x + 10, rect.y + rect.height / 2); 110 SwingUtilities.convertPointToScreen(point, tree); 111 result[0] = point; 112 } 113 }); 114 return result[0]; 115 } 116 117 private static class Editor extends JPanel implements TreeCellEditor { 118 private JCheckBox checkbox; 119 120 public Editor() { 121 setOpaque(false); 122 checkbox = new JCheckBox(); 123 add(checkbox); 124 } 125 126 public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, 127 boolean expanded, boolean leaf, int row) { 128 checkbox.setText(value.toString()); 129 checkbox.setSelected(false); 130 return this; 131 } 132 133 public Object getCellEditorValue() { 134 return checkbox.isSelected(); 135 } 136 137 public boolean isCellEditable(EventObject anEvent) { 138 return true; 139 } 140 141 public boolean shouldSelectCell(EventObject anEvent) { 142 return true; 143 } 144 145 public boolean stopCellEditing() { 146 return true; 147 } 148 149 public void cancelCellEditing() { 150 } 151 152 public void addCellEditorListener(CellEditorListener l) { 153 } 154 155 public void removeCellEditorListener(CellEditorListener l) { 156 } 157 } 158 159 private static class CheckboxCellRenderer extends JPanel implements TreeCellRenderer { 160 private JCheckBox checkbox; 161 162 public CheckboxCellRenderer() { 163 setOpaque(false); 164 checkbox = new JCheckBox(); 165 add(checkbox); 166 } 167 168 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 169 boolean leaf, int row, boolean hasFocus) { 170 checkbox.setText(value.toString()); 171 checkbox.setSelected(false); 172 return this; 173 } 174 } 175 }