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