bug6263446.java revision 9330:8b1f1c2a400f
1231990Smp/* 259243Sobrien * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 359243Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 459243Sobrien * 559243Sobrien * This code is free software; you can redistribute it and/or modify it 659243Sobrien * under the terms of the GNU General Public License version 2 only, as 759243Sobrien * published by the Free Software Foundation. 859243Sobrien * 959243Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT 1059243Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1159243Sobrien * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1259243Sobrien * version 2 for more details (a copy is included in the LICENSE file that 1359243Sobrien * accompanied this code). 1459243Sobrien * 1559243Sobrien * You should have received a copy of the GNU General Public License version 1659243Sobrien * 2 along with this work; if not, write to the Free Software Foundation, 17100616Smp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1859243Sobrien * 1959243Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2059243Sobrien * or visit www.oracle.com if you need additional information or have any 2159243Sobrien * questions. 2259243Sobrien */ 2359243Sobrien 2459243Sobrien/* 2559243Sobrien * @test 2659243Sobrien * @bug 6263446 2759243Sobrien * @summary Tests that double-clicking to edit a cell doesn't select the content. 2859243Sobrien * @author Shannon Hickey 2959243Sobrien * @run main bug6263446 3059243Sobrien */ 3159243Sobrienimport java.awt.*; 3259243Sobrienimport java.awt.event.InputEvent; 3359243Sobrienimport java.lang.reflect.Field; 3459243Sobrienimport javax.swing.*; 35167465Smpimport javax.swing.tree.*; 3659243Sobrienimport sun.awt.SunToolkit; 37231990Smp 3859243Sobrienpublic class bug6263446 { 3959243Sobrien 4059243Sobrien private static final String FIRST = "AAAAAAAAAAA"; 4159243Sobrien private static final String SECOND = "BB"; 4259243Sobrien private static final String ALL = FIRST + " " + SECOND; 4359243Sobrien private static JTree tree; 4459243Sobrien private static Robot robot; 4559243Sobrien private static SunToolkit toolkit; 4659243Sobrien 4759243Sobrien public static void main(String[] args) throws Exception { 4859243Sobrien toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 4959243Sobrien robot = new Robot(); 5059243Sobrien robot.setAutoDelay(50); 5159243Sobrien 5259243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 5359243Sobrien 54231990Smp public void run() { 5559243Sobrien createAndShowGUI(); 5659243Sobrien } 5759243Sobrien }); 5859243Sobrien 5959243Sobrien toolkit.realSync(); 6059243Sobrien 6159243Sobrien Point point = getClickPoint(); 6259243Sobrien robot.mouseMove(point.x, point.y); 6359243Sobrien 6459243Sobrien // click count 3 6559243Sobrien click(1); 6659243Sobrien assertNotEditing(); 6759243Sobrien 6859243Sobrien click(2); 6959243Sobrien assertNotEditing(); 7059243Sobrien 7159243Sobrien click(3); 7259243Sobrien assertEditing(); 7359243Sobrien cancelCellEditing(); 7459243Sobrien assertNotEditing(); 7559243Sobrien 7659243Sobrien click(4); 7759243Sobrien checkSelectedText(FIRST); 7859243Sobrien 7959243Sobrien click(5); 8059243Sobrien checkSelectedText(ALL); 8159243Sobrien 8259243Sobrien // click count 4 8359243Sobrien setClickCountToStart(4); 8459243Sobrien 8559243Sobrien click(1); 8659243Sobrien assertNotEditing(); 8759243Sobrien 8859243Sobrien click(2); 8959243Sobrien assertNotEditing(); 9059243Sobrien 9159243Sobrien click(3); 9259243Sobrien assertNotEditing(); 9359243Sobrien 9459243Sobrien click(4); 9559243Sobrien assertEditing(); 9659243Sobrien cancelCellEditing(); 9759243Sobrien assertNotEditing(); 9859243Sobrien 9959243Sobrien click(5); 10059243Sobrien checkSelectedText(FIRST); 10159243Sobrien 10259243Sobrien click(6); 10359243Sobrien checkSelectedText(ALL); 10459243Sobrien 10559243Sobrien // start path editing 10659243Sobrien startPathEditing(); 10759243Sobrien assertEditing(); 10859243Sobrien 10959243Sobrien click(1); 11059243Sobrien checkSelection(null); 11159243Sobrien 11259243Sobrien click(2); 11359243Sobrien checkSelection(FIRST); 11459243Sobrien 11559243Sobrien click(3); 11659243Sobrien checkSelection(ALL); 11759243Sobrien } 11859243Sobrien 11959243Sobrien private static void click(int times) { 12059243Sobrien robot.delay(500); 12159243Sobrien for (int i = 0; i < times; i++) { 12259243Sobrien robot.mousePress(InputEvent.BUTTON1_MASK); 12359243Sobrien robot.mouseRelease(InputEvent.BUTTON1_MASK); 12459243Sobrien } 12559243Sobrien } 12659243Sobrien 12759243Sobrien private static Point getClickPoint() throws Exception { 12859243Sobrien final Point[] result = new Point[1]; 12959243Sobrien 13059243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 13159243Sobrien 13259243Sobrien @Override 13359243Sobrien public void run() { 13459243Sobrien Rectangle rect = tree.getRowBounds(0); 13559243Sobrien // UPDATE !!! 13659243Sobrien Point p = new Point(rect.x + rect.width / 2, rect.y + 2); 13759243Sobrien SwingUtilities.convertPointToScreen(p, tree); 13859243Sobrien result[0] = p; 13959243Sobrien 14059243Sobrien } 14159243Sobrien }); 14259243Sobrien 14359243Sobrien return result[0]; 14459243Sobrien } 14559243Sobrien 14659243Sobrien private static TreeModel createTreeModel() { 14759243Sobrien return new DefaultTreeModel(new DefaultMutableTreeNode(ALL)); 14859243Sobrien } 14959243Sobrien 15059243Sobrien private static void createAndShowGUI() { 15159243Sobrien 15259243Sobrien JFrame frame = new JFrame(); 15359243Sobrien frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15459243Sobrien 15559243Sobrien tree = new JTree(createTreeModel()); 15659243Sobrien tree.setRootVisible(true); 15759243Sobrien tree.setEditable(true); 15859243Sobrien 15959243Sobrien 16059243Sobrien frame.getContentPane().add(tree); 16159243Sobrien frame.pack(); 16259243Sobrien frame.setVisible(true); 16359243Sobrien } 16459243Sobrien 16559243Sobrien private static void setClickCountToStart(final int clicks) throws Exception { 16659243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 16759243Sobrien 16859243Sobrien @Override 16959243Sobrien public void run() { 17059243Sobrien try { 17159243Sobrien DefaultTreeCellEditor editor = 17259243Sobrien (DefaultTreeCellEditor) tree.getCellEditor(); 17359243Sobrien Field field = DefaultTreeCellEditor.class.getDeclaredField("realEditor"); 17459243Sobrien field.setAccessible(true); 17559243Sobrien DefaultCellEditor ce = (DefaultCellEditor) field.get(editor); 17659243Sobrien ce.setClickCountToStart(clicks); 17759243Sobrien } catch (IllegalAccessException e) { 17859243Sobrien throw new RuntimeException(e); 17959243Sobrien } catch (NoSuchFieldException e) { 18059243Sobrien throw new RuntimeException(e); 18159243Sobrien } 18259243Sobrien } 18359243Sobrien }); 18459243Sobrien 18559243Sobrien toolkit.realSync(); 18659243Sobrien 18759243Sobrien } 18859243Sobrien 18959243Sobrien private static void startPathEditing() throws Exception { 19059243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 191231990Smp 192231990Smp @Override 19359243Sobrien public void run() { 194145479Smp tree.startEditingAtPath(tree.getPathForRow(0)); 19559243Sobrien } 19659243Sobrien }); 19759243Sobrien } 19859243Sobrien 19959243Sobrien private static void cancelCellEditing() throws Exception { 200167465Smp SwingUtilities.invokeAndWait(new Runnable() { 20159243Sobrien 20259243Sobrien @Override 203167465Smp public void run() { 20459243Sobrien tree.getCellEditor().cancelCellEditing(); 20559243Sobrien } 206167465Smp }); 207195609Smp } 20859243Sobrien 20959243Sobrien private static void checkSelection(final String sel) throws Exception { 21059243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 21159243Sobrien 21259243Sobrien @Override 21359243Sobrien public void run() { 21459243Sobrien try { 21559243Sobrien DefaultTreeCellEditor editor = 21659243Sobrien (DefaultTreeCellEditor) tree.getCellEditor(); 21759243Sobrien Field field = DefaultTreeCellEditor.class.getDeclaredField("realEditor"); 21859243Sobrien field.setAccessible(true); 21959243Sobrien DefaultCellEditor ce = (DefaultCellEditor) field.get(editor); 22059243Sobrien JTextField tf = (JTextField) ce.getComponent(); 22159243Sobrien String text = tf.getSelectedText(); 22259243Sobrien 22359243Sobrien if (sel == null) { 22459243Sobrien if (text != null && text.length() != 0) { 22559243Sobrien throw new RuntimeException("Nothing should be selected, but \"" + text + "\" is selected."); 22659243Sobrien } 22759243Sobrien } else if (!sel.equals(text)) { 22859243Sobrien throw new RuntimeException("\"" + sel + "\" should be selected, but \"" + text + "\" is selected."); 22959243Sobrien } 23059243Sobrien } catch (IllegalAccessException e) { 23159243Sobrien throw new RuntimeException(e); 23259243Sobrien } catch (NoSuchFieldException e) { 23359243Sobrien throw new RuntimeException(e); 23459243Sobrien } 23559243Sobrien } 23659243Sobrien }); 23759243Sobrien } 23859243Sobrien 23959243Sobrien private static void checkSelectedText(String sel) throws Exception { 24059243Sobrien assertEditing(); 24159243Sobrien checkSelection(sel); 24259243Sobrien cancelCellEditing(); 24359243Sobrien assertNotEditing(); 24459243Sobrien } 24559243Sobrien 24659243Sobrien private static void assertEditing() throws Exception { 24759243Sobrien assertEditingNoTreeLock(true); 24859243Sobrien } 24959243Sobrien 25059243Sobrien private static void assertNotEditing() throws Exception { 25159243Sobrien assertEditingNoTreeLock(false); 25259243Sobrien } 25359243Sobrien 25459243Sobrien private static void assertEditingNoTreeLock(final boolean editing) throws Exception { 25559243Sobrien toolkit.realSync(); 25659243Sobrien 25759243Sobrien SwingUtilities.invokeAndWait(new Runnable() { 25859243Sobrien 25959243Sobrien @Override 26059243Sobrien public void run() { 26159243Sobrien if (editing && !tree.isEditing()) { 26259243Sobrien throw new RuntimeException("Tree should be editing"); 26359243Sobrien } 26459243Sobrien if (!editing && tree.isEditing()) { 26559243Sobrien throw new RuntimeException("Tree should not be editing"); 26659243Sobrien } 26759243Sobrien } 26859243Sobrien }); 26959243Sobrien 27059243Sobrien } 27159243Sobrien 27259243Sobrien} 27359243Sobrien