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