Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/4330357/bug4330357.java
38854 views
/*1* Copyright (c) 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 433035726* @summary Tests that real editor in JTree cleans up after editing was stopped27* @library ../../regtesthelpers28* @build Util29* @author Peter Zhelezniakov30* @run main bug433035731*/32import java.awt.*;33import java.awt.event.*;34import javax.swing.*;35import javax.swing.tree.*;3637public class bug4330357 {3839private static JTree tree;40private static JButton button;41private static Robot robot;4243public static void main(String[] args) throws Exception {44robot = new Robot();45robot.setAutoDelay(50);4647UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");4849javax.swing.SwingUtilities.invokeAndWait(new Runnable() {5051public void run() {52createAndShowGUI();53}54});5556robot.waitForIdle();5758clickMouse(getTreeRowClickPoint(1));59Util.hitKeys(robot, KeyEvent.VK_F2);60Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C);61robot.waitForIdle();6263if (!hasComponent(JTextField.class)) {64throw new RuntimeException("Cell editor is missed for path: color");65}666768clickMouse(getButtonClickPoint());69robot.waitForIdle();7071clickMouse(getTreeRowClickPoint(2));72Util.hitKeys(robot, KeyEvent.VK_F2);73robot.waitForIdle();7475if (!hasComponent(JComboBox.class)) {76throw new RuntimeException("Cell editor is missed for path: sports");77}7879if (hasComponent(JTextField.class)) {80throw new RuntimeException("Cell editor is wrongly shown for path: color");81}82}8384static void clickMouse(Point point) {85robot.mouseMove(point.x, point.y);86robot.mousePress(InputEvent.BUTTON1_MASK);87robot.mouseRelease(InputEvent.BUTTON1_MASK);88}8990private static Point getTreeRowClickPoint(final int row) throws Exception {91final Point[] result = new Point[1];9293SwingUtilities.invokeAndWait(new Runnable() {9495@Override96public void run() {9798Rectangle rect = tree.getRowBounds(row);99Point p = new Point(rect.x + rect.width / 2, rect.y + 2);100SwingUtilities.convertPointToScreen(p, tree);101result[0] = p;102}103});104105return result[0];106}107108private static Point getButtonClickPoint() throws Exception {109final Point[] result = new Point[1];110111SwingUtilities.invokeAndWait(new Runnable() {112113@Override114public void run() {115Point p = button.getLocationOnScreen();116Dimension size = button.getSize();117result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);118}119});120return result[0];121}122123static boolean hasComponent(final Class cls) throws Exception {124final boolean[] result = new boolean[1];125126SwingUtilities.invokeAndWait(new Runnable() {127128@Override129public void run() {130result[0] = Util.findSubComponent(tree, cls.getName()) != null;131}132});133134return result[0];135}136137private static void createAndShowGUI() {138JFrame frame = new JFrame("Test");139frame.setSize(200, 200);140frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);141142tree = new JTree();143tree.setEditable(true);144145final TestEditor testEditor = new TestEditor();146tree.setCellEditor(new DefaultTreeCellEditor(tree,147(DefaultTreeCellRenderer) tree.getCellRenderer(),148testEditor));149150button = new JButton("stop");151152button.addActionListener(new ActionListener() {153154public void actionPerformed(ActionEvent ae) {155testEditor.stopCellEditing();156}157});158159frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);160frame.getContentPane().add(button, BorderLayout.SOUTH);161frame.setVisible(true);162}163164static class TestEditor extends AbstractCellEditor implements TreeCellEditor {165166private JComboBox comboBox;167private JTextField textField;168private boolean comboBoxActive;169170TestEditor() {171comboBox = new JComboBox(new String[]{"one", "two"});172textField = new JTextField();173}174175public Component getTreeCellEditorComponent(JTree tree, Object value,176boolean isSelected,177boolean expanded,178boolean leaf, int row) {179if (row % 2 == 0) {180comboBoxActive = true;181return comboBox;182}183comboBoxActive = false;184return textField;185}186187public Object getCellEditorValue() {188if (comboBoxActive) {189return comboBox.getSelectedItem();190}191return textField.getText();192}193}194}195196197