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