Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/6263446/bug6263446.java
38853 views
/*1* Copyright (c) 2011, 2012, 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.InputEvent;32import java.lang.reflect.Field;33import javax.swing.*;34import javax.swing.tree.*;3536public class bug6263446 {3738private static final String FIRST = "AAAAAAAAAAA";39private static final String SECOND = "BB";40private static final String ALL = FIRST + " " + SECOND;41private static JTree tree;42private static Robot robot;4344public static void main(String[] args) throws Exception {45robot = new Robot();46robot.setAutoDelay(50);4748SwingUtilities.invokeAndWait(new Runnable() {4950public void run() {51createAndShowGUI();52}53});5455robot.waitForIdle();5657Point point = getClickPoint();58robot.mouseMove(point.x, point.y);5960// click count 361click(1);62assertNotEditing();6364click(2);65assertNotEditing();6667click(3);68assertEditing();69cancelCellEditing();70assertNotEditing();7172click(4);73checkSelectedText(FIRST);7475click(5);76checkSelectedText(ALL);7778// click count 479setClickCountToStart(4);8081click(1);82assertNotEditing();8384click(2);85assertNotEditing();8687click(3);88assertNotEditing();8990click(4);91assertEditing();92cancelCellEditing();93assertNotEditing();9495click(5);96checkSelectedText(FIRST);9798click(6);99checkSelectedText(ALL);100101// start path editing102startPathEditing();103assertEditing();104105click(1);106checkSelection(null);107108click(2);109checkSelection(FIRST);110111click(3);112checkSelection(ALL);113}114115private static void click(int times) {116robot.delay(500);117for (int i = 0; i < times; i++) {118robot.mousePress(InputEvent.BUTTON1_MASK);119robot.mouseRelease(InputEvent.BUTTON1_MASK);120}121}122123private static Point getClickPoint() throws Exception {124final Point[] result = new Point[1];125126SwingUtilities.invokeAndWait(new Runnable() {127128@Override129public void run() {130Rectangle rect = tree.getRowBounds(0);131// UPDATE !!!132Point p = new Point(rect.x + rect.width / 2, rect.y + 2);133SwingUtilities.convertPointToScreen(p, tree);134result[0] = p;135136}137});138139return result[0];140}141142private static TreeModel createTreeModel() {143return new DefaultTreeModel(new DefaultMutableTreeNode(ALL));144}145146private static void createAndShowGUI() {147148JFrame frame = new JFrame();149frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);150151tree = new JTree(createTreeModel());152tree.setRootVisible(true);153tree.setEditable(true);154155156frame.getContentPane().add(tree);157frame.pack();158frame.setVisible(true);159}160161private static void setClickCountToStart(final int clicks) throws Exception {162SwingUtilities.invokeAndWait(new Runnable() {163164@Override165public void run() {166try {167DefaultTreeCellEditor editor =168(DefaultTreeCellEditor) tree.getCellEditor();169Field field = DefaultTreeCellEditor.class.getDeclaredField("realEditor");170field.setAccessible(true);171DefaultCellEditor ce = (DefaultCellEditor) field.get(editor);172ce.setClickCountToStart(clicks);173} catch (IllegalAccessException e) {174throw new RuntimeException(e);175} catch (NoSuchFieldException e) {176throw new RuntimeException(e);177}178}179});180181robot.waitForIdle();182183}184185private static void startPathEditing() throws Exception {186SwingUtilities.invokeAndWait(new Runnable() {187188@Override189public void run() {190tree.startEditingAtPath(tree.getPathForRow(0));191}192});193}194195private static void cancelCellEditing() throws Exception {196SwingUtilities.invokeAndWait(new Runnable() {197198@Override199public void run() {200tree.getCellEditor().cancelCellEditing();201}202});203}204205private static void checkSelection(final String sel) throws Exception {206SwingUtilities.invokeAndWait(new Runnable() {207208@Override209public void run() {210try {211DefaultTreeCellEditor editor =212(DefaultTreeCellEditor) tree.getCellEditor();213Field field = DefaultTreeCellEditor.class.getDeclaredField("realEditor");214field.setAccessible(true);215DefaultCellEditor ce = (DefaultCellEditor) field.get(editor);216JTextField tf = (JTextField) ce.getComponent();217String text = tf.getSelectedText();218219if (sel == null) {220if (text != null && text.length() != 0) {221throw new RuntimeException("Nothing should be selected, but \"" + text + "\" is selected.");222}223} else if (!sel.equals(text)) {224throw new RuntimeException("\"" + sel + "\" should be selected, but \"" + text + "\" is selected.");225}226} catch (IllegalAccessException e) {227throw new RuntimeException(e);228} catch (NoSuchFieldException e) {229throw new RuntimeException(e);230}231}232});233}234235private static void checkSelectedText(String sel) throws Exception {236assertEditing();237checkSelection(sel);238cancelCellEditing();239assertNotEditing();240}241242private static void assertEditing() throws Exception {243assertEditingNoTreeLock(true);244}245246private static void assertNotEditing() throws Exception {247assertEditingNoTreeLock(false);248}249250private static void assertEditingNoTreeLock(final boolean editing) throws Exception {251robot.waitForIdle();252253SwingUtilities.invokeAndWait(new Runnable() {254255@Override256public void run() {257if (editing && !tree.isEditing()) {258throw new RuntimeException("Tree should be editing");259}260if (!editing && tree.isEditing()) {261throw new RuntimeException("Tree should not be editing");262}263}264});265266}267268}269270271