4903N/A/*
4903N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4903N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4903N/A *
4903N/A * This code is free software; you can redistribute it and/or modify it
4903N/A * under the terms of the GNU General Public License version 2 only, as
4903N/A * published by the Free Software Foundation.
4903N/A *
4903N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4903N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4903N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4903N/A * version 2 for more details (a copy is included in the LICENSE file that
4903N/A * accompanied this code).
4903N/A *
4903N/A * You should have received a copy of the GNU General Public License version
4903N/A * 2 along with this work; if not, write to the Free Software Foundation,
4903N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4903N/A *
4903N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4903N/A * or visit www.oracle.com if you need additional information or have any
4903N/A * questions.
4903N/A */
4903N/A
4903N/A/*
4903N/A * @test
4903N/A * @bug 4330357
4903N/A * @summary Tests that real editor in JTree cleans up after editing was stopped
4903N/A * @library ../../regtesthelpers
4903N/A * @build Util
4903N/A * @author Peter Zhelezniakov
4903N/A * @run main bug4330357
4903N/A */
4903N/Aimport java.awt.*;
4903N/Aimport java.awt.event.*;
4903N/Aimport javax.swing.*;
4903N/Aimport javax.swing.tree.*;
4903N/Aimport sun.awt.SunToolkit;
4903N/A
4903N/Apublic class bug4330357 {
4903N/A
4903N/A private static JTree tree;
4903N/A private static JButton button;
4903N/A private static Robot robot;
4903N/A
4903N/A public static void main(String[] args) throws Exception {
4903N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
4903N/A robot = new Robot();
4903N/A robot.setAutoDelay(50);
4903N/A
4903N/A UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
4903N/A
4903N/A javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
4903N/A
4903N/A public void run() {
4903N/A createAndShowGUI();
4903N/A }
4903N/A });
4903N/A
4903N/A toolkit.realSync();
4903N/A
4903N/A clickMouse(getTreeRowClickPoint(1));
4903N/A Util.hitKeys(robot, KeyEvent.VK_F2);
4903N/A Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C);
4903N/A toolkit.realSync();
4903N/A
4903N/A if (!hasComponent(JTextField.class)) {
4903N/A throw new RuntimeException("Cell editor is missed for path: color");
4903N/A }
4903N/A
4903N/A
4903N/A clickMouse(getButtonClickPoint());
4903N/A toolkit.realSync();
4903N/A
4903N/A clickMouse(getTreeRowClickPoint(2));
4903N/A Util.hitKeys(robot, KeyEvent.VK_F2);
4903N/A toolkit.realSync();
4903N/A
4903N/A if (!hasComponent(JComboBox.class)) {
4903N/A throw new RuntimeException("Cell editor is missed for path: sports");
4903N/A }
4903N/A
4903N/A if (hasComponent(JTextField.class)) {
4903N/A throw new RuntimeException("Cell editor is wrongly shown for path: color");
4903N/A }
4903N/A }
4903N/A
4903N/A static void clickMouse(Point point) {
4903N/A robot.mouseMove(point.x, point.y);
4903N/A robot.mousePress(InputEvent.BUTTON1_MASK);
4903N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
4903N/A }
4903N/A
4903N/A private static Point getTreeRowClickPoint(final int row) throws Exception {
4903N/A final Point[] result = new Point[1];
4903N/A
4903N/A SwingUtilities.invokeAndWait(new Runnable() {
4903N/A
4903N/A @Override
4903N/A public void run() {
4903N/A
4903N/A Rectangle rect = tree.getRowBounds(row);
4903N/A Point p = new Point(rect.x + rect.width / 2, rect.y + 2);
4903N/A SwingUtilities.convertPointToScreen(p, tree);
4903N/A result[0] = p;
4903N/A }
4903N/A });
4903N/A
4903N/A return result[0];
4903N/A }
4903N/A
4903N/A private static Point getButtonClickPoint() throws Exception {
4903N/A final Point[] result = new Point[1];
4903N/A
4903N/A SwingUtilities.invokeAndWait(new Runnable() {
4903N/A
4903N/A @Override
4903N/A public void run() {
4903N/A Point p = button.getLocationOnScreen();
4903N/A Dimension size = button.getSize();
4903N/A result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
4903N/A }
4903N/A });
4903N/A return result[0];
4903N/A }
4903N/A
4903N/A static boolean hasComponent(final Class cls) throws Exception {
4903N/A final boolean[] result = new boolean[1];
4903N/A
4903N/A SwingUtilities.invokeAndWait(new Runnable() {
4903N/A
4903N/A @Override
4903N/A public void run() {
4903N/A result[0] = Util.findSubComponent(tree, cls.getName()) != null;
4903N/A }
4903N/A });
4903N/A
4903N/A return result[0];
4903N/A }
4903N/A
4903N/A private static void createAndShowGUI() {
4903N/A JFrame frame = new JFrame("Test");
4903N/A frame.setSize(200, 200);
4903N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4903N/A
4903N/A tree = new JTree();
4903N/A tree.setEditable(true);
4903N/A
4903N/A final TestEditor testEditor = new TestEditor();
4903N/A tree.setCellEditor(new DefaultTreeCellEditor(tree,
4903N/A (DefaultTreeCellRenderer) tree.getCellRenderer(),
4903N/A testEditor));
4903N/A
4903N/A button = new JButton("stop");
4903N/A
4903N/A button.addActionListener(new ActionListener() {
4903N/A
4903N/A public void actionPerformed(ActionEvent ae) {
4903N/A testEditor.stopCellEditing();
4903N/A }
4903N/A });
4903N/A
4903N/A frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
4903N/A frame.getContentPane().add(button, BorderLayout.SOUTH);
4903N/A frame.setVisible(true);
4903N/A }
4903N/A
4903N/A static class TestEditor extends AbstractCellEditor implements TreeCellEditor {
4903N/A
4903N/A private JComboBox comboBox;
4903N/A private JTextField textField;
4903N/A private boolean comboBoxActive;
4903N/A
4903N/A TestEditor() {
4903N/A comboBox = new JComboBox(new String[]{"one", "two"});
4903N/A textField = new JTextField();
4903N/A }
4903N/A
4903N/A public Component getTreeCellEditorComponent(JTree tree, Object value,
4903N/A boolean isSelected,
4903N/A boolean expanded,
4903N/A boolean leaf, int row) {
4903N/A if (row % 2 == 0) {
4903N/A comboBoxActive = true;
4903N/A return comboBox;
4903N/A }
4903N/A comboBoxActive = false;
4903N/A return textField;
4903N/A }
4903N/A
4903N/A public Object getCellEditorValue() {
4903N/A if (comboBoxActive) {
4903N/A return comboBox.getSelectedItem();
4903N/A }
4903N/A return textField.getText();
4903N/A }
4903N/A }
4903N/A}