1 /*
   2  * Copyright (c) 2011, 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 6263446
  27  * @summary Tests that double-clicking to edit a cell doesn't select the content.
  28  * @author Shannon Hickey
  29  * @run main bug6263446
  30  */
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import javax.swing.*;
  34 import javax.swing.table.*;
  35 
  36 public class bug6263446 {
  37 
  38     private static JTable table;
  39     private static final String FIRST = "AAAAA";
  40     private static final String SECOND = "BB";
  41     private static final String ALL = FIRST + " " + SECOND;
  42     private static Robot robot;
  43 
  44     public static void main(String[] args) throws Exception {
  45         robot = new Robot();
  46         robot.setAutoDelay(50);
  47 
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49 
  50             public void run() {
  51                 createAndShowGUI();
  52             }
  53         });
  54 
  55 
  56         robot.waitForIdle();
  57 
  58         Point point = getClickPoint();
  59         robot.mouseMove(point.x, point.y);
  60         robot.waitForIdle();
  61 
  62         click(1);
  63         robot.waitForIdle();
  64         assertEditing(false);
  65 
  66         click(2);
  67         robot.waitForIdle();
  68         checkSelectedText(null);
  69 
  70         click(3);
  71         robot.waitForIdle();
  72         checkSelectedText(FIRST);
  73 
  74 
  75         click(4);
  76         robot.waitForIdle();
  77         checkSelectedText(ALL);
  78 
  79         setClickCountToStart(1);
  80 
  81         click(1);
  82         robot.waitForIdle();
  83         checkSelectedText(null);
  84 
  85         click(2);
  86         robot.waitForIdle();
  87         checkSelectedText(FIRST);
  88 
  89         click(3);
  90         robot.waitForIdle();
  91         checkSelectedText(ALL);
  92 
  93         setClickCountToStart(3);
  94 
  95         click(1);
  96         robot.waitForIdle();
  97         assertEditing(false);
  98 
  99         click(2);
 100         robot.waitForIdle();
 101         assertEditing(false);
 102 
 103         click(3);
 104         robot.waitForIdle();
 105         checkSelectedText(null);
 106 
 107         click(4);
 108         robot.waitForIdle();
 109         checkSelectedText(FIRST);
 110 
 111         click(5);
 112         robot.waitForIdle();
 113         checkSelectedText(ALL);
 114 
 115 
 116         SwingUtilities.invokeAndWait(new Runnable() {
 117 
 118             @Override
 119             public void run() {
 120                 table.editCellAt(0, 0);
 121             }
 122         });
 123 
 124         robot.waitForIdle();
 125         assertEditing(true);
 126 
 127         click(2);
 128         robot.waitForIdle();
 129         checkSelectedText(FIRST);
 130 
 131     }
 132 
 133     private static void checkSelectedText(String sel) throws Exception {
 134         assertEditing(true);
 135         checkSelection(sel);
 136         cancelCellEditing();
 137         assertEditing(false);
 138     }
 139 
 140     private static void setClickCountToStart(final int clicks) throws Exception {
 141         SwingUtilities.invokeAndWait(new Runnable() {
 142 
 143             @Override
 144             public void run() {
 145                 DefaultCellEditor editor =
 146                         (DefaultCellEditor) table.getDefaultEditor(String.class);
 147                 editor.setClickCountToStart(clicks);
 148             }
 149         });
 150 
 151     }
 152 
 153     private static void cancelCellEditing() throws Exception {
 154         SwingUtilities.invokeAndWait(new Runnable() {
 155 
 156             @Override
 157             public void run() {
 158                 table.getCellEditor().cancelCellEditing();
 159             }
 160         });
 161     }
 162 
 163     private static void checkSelection(final String sel) throws Exception {
 164         SwingUtilities.invokeAndWait(new Runnable() {
 165 
 166             @Override
 167             public void run() {
 168                 DefaultCellEditor editor =
 169                         (DefaultCellEditor) table.getDefaultEditor(String.class);
 170                 JTextField field = (JTextField) editor.getComponent();
 171                 String text = field.getSelectedText();
 172                 if (sel == null) {
 173                     if (text != null && text.length() != 0) {
 174                         throw new RuntimeException("Nothing should be selected,"
 175                                 + " but \"" + text + "\" is selected.");
 176                     }
 177                 } else if (!sel.equals(text)) {
 178                     throw new RuntimeException("\"" + sel + "\" should be "
 179                             + "selected, but \"" + text + "\" is selected.");
 180                 }
 181             }
 182         });
 183     }
 184 
 185     private static void assertEditing(final boolean editing) throws Exception {
 186         SwingUtilities.invokeAndWait(new Runnable() {
 187 
 188             @Override
 189             public void run() {
 190                 if (editing && !table.isEditing()) {
 191                     throw new RuntimeException("Table should be editing");
 192                 }
 193                 if (!editing && table.isEditing()) {
 194                     throw new RuntimeException("Table should not be editing");
 195                 }
 196             }
 197         });
 198     }
 199 
 200     private static Point getClickPoint() throws Exception {
 201         final Point[] result = new Point[1];
 202         SwingUtilities.invokeAndWait(new Runnable() {
 203 
 204             @Override
 205             public void run() {
 206                 Rectangle rect = table.getCellRect(0, 0, false);
 207                 Point point = new Point(rect.x + rect.width / 5,
 208                         rect.y + rect.height / 2);
 209                 SwingUtilities.convertPointToScreen(point, table);
 210                 result[0] = point;
 211             }
 212         });
 213 
 214         return result[0];
 215     }
 216 
 217     private static void click(int times) {
 218         robot.delay(500);
 219         for (int i = 0; i < times; i++) {
 220             robot.mousePress(InputEvent.BUTTON1_MASK);
 221             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 222         }
 223     }
 224 
 225     private static TableModel createTableModel() {
 226         String[] columnNames = {"Column 0"};
 227         String[][] data = {{ALL}};
 228 
 229         return new DefaultTableModel(data, columnNames);
 230     }
 231 
 232     private static void createAndShowGUI() {
 233         JFrame frame = new JFrame("bug6263446");
 234         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 235         table = new JTable(createTableModel());
 236         frame.add(table);
 237         frame.pack();
 238         frame.setVisible(true);
 239     }
 240 }