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