Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTable/6263446/bug6263446.java
38853 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* @bug 626344626* @summary Tests that double-clicking to edit a cell doesn't select the content.27* @author Shannon Hickey28* @run main bug626344629*/30import java.awt.*;31import java.awt.event.*;32import javax.swing.*;33import javax.swing.table.*;3435public class bug6263446 {3637private static JTable table;38private static final String FIRST = "AAAAA";39private static final String SECOND = "BB";40private static final String ALL = FIRST + " " + SECOND;41private static Robot robot;4243public static void main(String[] args) throws Exception {44robot = new Robot();45robot.setAutoDelay(50);4647SwingUtilities.invokeAndWait(new Runnable() {4849public void run() {50createAndShowGUI();51}52});535455robot.waitForIdle();5657Point point = getClickPoint();58robot.mouseMove(point.x, point.y);59robot.waitForIdle();6061click(1);62robot.waitForIdle();63assertEditing(false);6465click(2);66robot.waitForIdle();67checkSelectedText(null);6869click(3);70robot.waitForIdle();71checkSelectedText(FIRST);727374click(4);75robot.waitForIdle();76checkSelectedText(ALL);7778setClickCountToStart(1);7980click(1);81robot.waitForIdle();82checkSelectedText(null);8384click(2);85robot.waitForIdle();86checkSelectedText(FIRST);8788click(3);89robot.waitForIdle();90checkSelectedText(ALL);9192setClickCountToStart(3);9394click(1);95robot.waitForIdle();96assertEditing(false);9798click(2);99robot.waitForIdle();100assertEditing(false);101102click(3);103robot.waitForIdle();104checkSelectedText(null);105106click(4);107robot.waitForIdle();108checkSelectedText(FIRST);109110click(5);111robot.waitForIdle();112checkSelectedText(ALL);113114115SwingUtilities.invokeAndWait(new Runnable() {116117@Override118public void run() {119table.editCellAt(0, 0);120}121});122123robot.waitForIdle();124assertEditing(true);125126click(2);127robot.waitForIdle();128checkSelectedText(FIRST);129130}131132private static void checkSelectedText(String sel) throws Exception {133assertEditing(true);134checkSelection(sel);135cancelCellEditing();136assertEditing(false);137}138139private static void setClickCountToStart(final int clicks) throws Exception {140SwingUtilities.invokeAndWait(new Runnable() {141142@Override143public void run() {144DefaultCellEditor editor =145(DefaultCellEditor) table.getDefaultEditor(String.class);146editor.setClickCountToStart(clicks);147}148});149150}151152private static void cancelCellEditing() throws Exception {153SwingUtilities.invokeAndWait(new Runnable() {154155@Override156public void run() {157table.getCellEditor().cancelCellEditing();158}159});160}161162private static void checkSelection(final String sel) throws Exception {163SwingUtilities.invokeAndWait(new Runnable() {164165@Override166public void run() {167DefaultCellEditor editor =168(DefaultCellEditor) table.getDefaultEditor(String.class);169JTextField field = (JTextField) editor.getComponent();170String text = field.getSelectedText();171if (sel == null) {172if (text != null && text.length() != 0) {173throw new RuntimeException("Nothing should be selected,"174+ " but \"" + text + "\" is selected.");175}176} else if (!sel.equals(text)) {177throw new RuntimeException("\"" + sel + "\" should be "178+ "selected, but \"" + text + "\" is selected.");179}180}181});182}183184private static void assertEditing(final boolean editing) throws Exception {185SwingUtilities.invokeAndWait(new Runnable() {186187@Override188public void run() {189if (editing && !table.isEditing()) {190throw new RuntimeException("Table should be editing");191}192if (!editing && table.isEditing()) {193throw new RuntimeException("Table should not be editing");194}195}196});197}198199private static Point getClickPoint() throws Exception {200final Point[] result = new Point[1];201SwingUtilities.invokeAndWait(new Runnable() {202203@Override204public void run() {205Rectangle rect = table.getCellRect(0, 0, false);206Point point = new Point(rect.x + rect.width / 5,207rect.y + rect.height / 2);208SwingUtilities.convertPointToScreen(point, table);209result[0] = point;210}211});212213return result[0];214}215216private static void click(int times) {217robot.delay(500);218for (int i = 0; i < times; i++) {219robot.mousePress(InputEvent.BUTTON1_MASK);220robot.mouseRelease(InputEvent.BUTTON1_MASK);221}222}223224private static TableModel createTableModel() {225String[] columnNames = {"Column 0"};226String[][] data = {{ALL}};227228return new DefaultTableModel(data, columnNames);229}230231private static void createAndShowGUI() {232JFrame frame = new JFrame("bug6263446");233frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);234table = new JTable(createTableModel());235frame.add(table);236frame.pack();237frame.setVisible(true);238}239}240241242