Path: blob/master/test/jdk/javax/swing/JTable/6263446/bug6263446.java
41155 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 626344627* @summary Tests that double-clicking to edit a cell doesn't select the content.28* @author Shannon Hickey29* @run main bug626344630*/31import java.awt.Point;32import java.awt.Robot;33import java.awt.Rectangle;34import java.awt.event.InputEvent;35import javax.swing.DefaultCellEditor;36import javax.swing.JFrame;37import javax.swing.JTable;38import javax.swing.JTextField;39import javax.swing.SwingUtilities;40import javax.swing.table.TableModel;41import javax.swing.table.DefaultTableModel;4243public class bug6263446 {4445private static JFrame frame;46private static JTable table;47private static final String FIRST = "AAAAA";48private static final String SECOND = "BB";49private static final String ALL = FIRST + " " + SECOND;50private static Robot robot;5152public static void main(String[] args) throws Exception {53try {54robot = new Robot();55robot.setAutoDelay(50);5657SwingUtilities.invokeAndWait(() -> createAndShowGUI());58robot.waitForIdle();59robot.delay(1000);6061Point point = getClickPoint();62robot.mouseMove(point.x, point.y);63robot.waitForIdle();6465click(1);66robot.waitForIdle();67assertEditing(false);6869click(2);70robot.waitForIdle();71checkSelectedText(null);7273click(3);74robot.waitForIdle();75checkSelectedText(FIRST);7677click(4);78robot.waitForIdle();79checkSelectedText(ALL);8081setClickCountToStart(1);82robot.waitForIdle();8384click(1);85robot.waitForIdle();86checkSelectedText(null);8788click(2);89robot.waitForIdle();90checkSelectedText(FIRST);9192click(3);93robot.waitForIdle();94checkSelectedText(ALL);9596setClickCountToStart(3);97robot.waitForIdle();9899click(1);100robot.waitForIdle();101assertEditing(false);102103click(2);104robot.waitForIdle();105assertEditing(false);106107click(3);108robot.waitForIdle();109checkSelectedText(null);110111click(4);112robot.waitForIdle();113checkSelectedText(FIRST);114115click(5);116robot.waitForIdle();117checkSelectedText(ALL);118119SwingUtilities.invokeAndWait(() -> table.editCellAt(0, 0));120121robot.waitForIdle();122assertEditing(true);123124click(2);125robot.waitForIdle();126checkSelectedText(FIRST);127} finally {128if (frame != null) {129SwingUtilities.invokeAndWait(frame::dispose);130}131}132}133134private static void checkSelectedText(String sel) throws Exception {135assertEditing(true);136checkSelection(sel);137robot.waitForIdle();138cancelCellEditing();139robot.waitForIdle();140assertEditing(false);141}142143private static void setClickCountToStart(final int clicks) throws Exception {144SwingUtilities.invokeAndWait(() -> {145DefaultCellEditor editor =146(DefaultCellEditor) table.getDefaultEditor(String.class);147editor.setClickCountToStart(clicks);148});149}150151private static void cancelCellEditing() throws Exception {152SwingUtilities.invokeAndWait(() -> {153table.getCellEditor().cancelCellEditing();154});155}156157private static void checkSelection(final String sel) throws Exception {158SwingUtilities.invokeAndWait(() -> {159DefaultCellEditor editor =160(DefaultCellEditor) table.getDefaultEditor(String.class);161JTextField field = (JTextField) editor.getComponent();162String text = field.getSelectedText();163if (sel == null) {164if (text != null && text.length() != 0) {165throw new RuntimeException("Nothing should be selected,"166+ " but \"" + text + "\" is selected.");167}168} else if (!sel.equals(text)) {169throw new RuntimeException("\"" + sel + "\" should be "170+ "selected, but \"" + text + "\" is selected.");171}172});173}174175private static void assertEditing(final boolean editing) throws Exception {176SwingUtilities.invokeAndWait(() -> {177if (editing && !table.isEditing()) {178throw new RuntimeException("Table should be editing");179}180if (!editing && table.isEditing()) {181throw new RuntimeException("Table should not be editing");182}183});184}185186private static Point getClickPoint() throws Exception {187final Point[] result = new Point[1];188SwingUtilities.invokeAndWait(() -> {189Rectangle rect = table.getCellRect(0, 0, false);190Point point = new Point(rect.x + rect.width / 5,191rect.y + rect.height / 2);192SwingUtilities.convertPointToScreen(point, table);193result[0] = point;194});195return result[0];196}197198private static void click(int times) {199robot.delay(500);200for (int i = 0; i < times; i++) {201robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);202robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);203}204}205206private static TableModel createTableModel() {207String[] columnNames = {"Column 0"};208String[][] data = {{ALL}};209210return new DefaultTableModel(data, columnNames);211}212213private static void createAndShowGUI() {214frame = new JFrame("bug6263446");215frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);216table = new JTable(createTableModel());217frame.add(table);218frame.setAlwaysOnTop(true);219frame.setLocationRelativeTo(null);220frame.pack();221frame.setVisible(true);222frame.toFront();223}224}225226227