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