4569N/A/*
4569N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4569N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4569N/A *
4569N/A * This code is free software; you can redistribute it and/or modify it
4569N/A * under the terms of the GNU General Public License version 2 only, as
4569N/A * published by the Free Software Foundation.
4569N/A *
4569N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4569N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4569N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4569N/A * version 2 for more details (a copy is included in the LICENSE file that
4569N/A * accompanied this code).
4569N/A *
4569N/A * You should have received a copy of the GNU General Public License version
4569N/A * 2 along with this work; if not, write to the Free Software Foundation,
4569N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4569N/A *
4569N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4569N/A * or visit www.oracle.com if you need additional information or have any
4569N/A * questions.
4569N/A */
4569N/A
4569N/A/*
4569N/A * @test
4569N/A * @bug 6263446
4569N/A * @summary Tests that double-clicking to edit a cell doesn't select the content.
4569N/A * @author Shannon Hickey
4569N/A * @run main bug6263446
4569N/A */
4569N/Aimport java.awt.*;
4569N/Aimport java.awt.event.*;
4569N/Aimport javax.swing.*;
4569N/Aimport javax.swing.table.*;
4569N/Aimport sun.awt.SunToolkit;
4569N/A
4569N/Apublic class bug6263446 {
4569N/A
4569N/A private static JTable table;
4569N/A private static final String FIRST = "AAAAA";
4569N/A private static final String SECOND = "BB";
4569N/A private static final String ALL = FIRST + " " + SECOND;
4569N/A private static Robot robot;
4569N/A
4569N/A public static void main(String[] args) throws Exception {
4569N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
4569N/A robot = new Robot();
4569N/A robot.setAutoDelay(50);
4569N/A
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A public void run() {
4569N/A createAndShowGUI();
4569N/A }
4569N/A });
4569N/A
4569N/A
4569N/A toolkit.realSync();
4569N/A
4569N/A Point point = getClickPoint();
4569N/A robot.mouseMove(point.x, point.y);
4569N/A toolkit.realSync();
4569N/A
4569N/A click(1);
4569N/A toolkit.realSync();
4569N/A assertEditing(false);
4569N/A
4569N/A click(2);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(null);
4569N/A
4569N/A click(3);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(FIRST);
4569N/A
4569N/A
4569N/A click(4);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(ALL);
4569N/A
4569N/A setClickCountToStart(1);
4569N/A
4569N/A click(1);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(null);
4569N/A
4569N/A click(2);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(FIRST);
4569N/A
4569N/A click(3);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(ALL);
4569N/A
4569N/A setClickCountToStart(3);
4569N/A
4569N/A click(1);
4569N/A toolkit.realSync();
4569N/A assertEditing(false);
4569N/A
4569N/A click(2);
4569N/A toolkit.realSync();
4569N/A assertEditing(false);
4569N/A
4569N/A click(3);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(null);
4569N/A
4569N/A click(4);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(FIRST);
4569N/A
4569N/A click(5);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(ALL);
4569N/A
4569N/A
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A table.editCellAt(0, 0);
4569N/A }
4569N/A });
4569N/A
4569N/A toolkit.realSync();
4569N/A assertEditing(true);
4569N/A
4569N/A click(2);
4569N/A toolkit.realSync();
4569N/A checkSelectedText(FIRST);
4569N/A
4569N/A }
4569N/A
4569N/A private static void checkSelectedText(String sel) throws Exception {
4569N/A assertEditing(true);
4569N/A checkSelection(sel);
4569N/A cancelCellEditing();
4569N/A assertEditing(false);
4569N/A }
4569N/A
4569N/A private static void setClickCountToStart(final int clicks) throws Exception {
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A DefaultCellEditor editor =
4569N/A (DefaultCellEditor) table.getDefaultEditor(String.class);
4569N/A editor.setClickCountToStart(clicks);
4569N/A }
4569N/A });
4569N/A
4569N/A }
4569N/A
4569N/A private static void cancelCellEditing() throws Exception {
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A table.getCellEditor().cancelCellEditing();
4569N/A }
4569N/A });
4569N/A }
4569N/A
4569N/A private static void checkSelection(final String sel) throws Exception {
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A DefaultCellEditor editor =
4569N/A (DefaultCellEditor) table.getDefaultEditor(String.class);
4569N/A JTextField field = (JTextField) editor.getComponent();
4569N/A String text = field.getSelectedText();
4569N/A if (sel == null) {
4569N/A if (text != null && text.length() != 0) {
4569N/A throw new RuntimeException("Nothing should be selected,"
4569N/A + " but \"" + text + "\" is selected.");
4569N/A }
4569N/A } else if (!sel.equals(text)) {
4569N/A throw new RuntimeException("\"" + sel + "\" should be "
4569N/A + "selected, but \"" + text + "\" is selected.");
4569N/A }
4569N/A }
4569N/A });
4569N/A }
4569N/A
4569N/A private static void assertEditing(final boolean editing) throws Exception {
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A if (editing && !table.isEditing()) {
4569N/A throw new RuntimeException("Table should be editing");
4569N/A }
4569N/A if (!editing && table.isEditing()) {
4569N/A throw new RuntimeException("Table should not be editing");
4569N/A }
4569N/A }
4569N/A });
4569N/A }
4569N/A
4569N/A private static Point getClickPoint() throws Exception {
4569N/A final Point[] result = new Point[1];
4569N/A SwingUtilities.invokeAndWait(new Runnable() {
4569N/A
4569N/A @Override
4569N/A public void run() {
4569N/A Rectangle rect = table.getCellRect(0, 0, false);
4569N/A Point point = new Point(rect.x + rect.width / 5,
4569N/A rect.y + rect.height / 2);
4569N/A SwingUtilities.convertPointToScreen(point, table);
4569N/A result[0] = point;
4569N/A }
4569N/A });
4569N/A
4569N/A return result[0];
4569N/A }
4569N/A
4569N/A private static void click(int times) {
4569N/A robot.delay(500);
4569N/A for (int i = 0; i < times; i++) {
4569N/A robot.mousePress(InputEvent.BUTTON1_MASK);
4569N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
4569N/A }
4569N/A }
4569N/A
4569N/A private static TableModel createTableModel() {
4569N/A String[] columnNames = {"Column 0"};
4569N/A String[][] data = {{ALL}};
4569N/A
4569N/A return new DefaultTableModel(data, columnNames);
4569N/A }
4569N/A
4569N/A private static void createAndShowGUI() {
4569N/A JFrame frame = new JFrame("bug6263446");
4569N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4569N/A table = new JTable(createTableModel());
4569N/A frame.add(table);
4569N/A frame.pack();
4569N/A frame.setVisible(true);
4569N/A }
4569N/A}