Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/4633594/JTreeFocusTest.java
38853 views
/*1* Copyright (c) 2002, 2017, 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*/22/* @test23@bug 4633594 817201224@summary No way to pass focus from a JTree to a GUI placed inside the tree node25@run main JTreeFocusTest26*/27import java.awt.Component;28import java.awt.GridLayout;29import java.awt.Point;30import java.awt.Robot;31import java.awt.event.FocusAdapter;32import java.awt.event.FocusEvent;33import java.awt.event.KeyEvent;34import java.util.EventObject;35import javax.swing.JComponent;36import javax.swing.JFrame;37import javax.swing.JLabel;38import javax.swing.JPanel;39import javax.swing.JTextField;40import javax.swing.JTree;41import javax.swing.SwingUtilities;42import javax.swing.UIManager;43import javax.swing.border.BevelBorder;44import javax.swing.border.CompoundBorder;45import javax.swing.border.LineBorder;46import javax.swing.event.TreeSelectionEvent;47import javax.swing.event.TreeSelectionListener;48import javax.swing.tree.DefaultMutableTreeNode;49import javax.swing.tree.DefaultTreeCellEditor;50import javax.swing.tree.DefaultTreeCellRenderer;51import javax.swing.tree.DefaultTreeModel;5253public class JTreeFocusTest {5455private static DefaultMutableTreeNode root;56Robot robot;57static boolean passed = false;58boolean rootSelected = false;59boolean keysTyped = false;60private volatile Point p = null;61private static JFrame fr;62private static volatile JTree tree = null;6364public static void main(String[] args) throws Exception{65new JTreeFocusTest();66}6768void blockTillDisplayed(JComponent comp) throws Exception {69while (p == null) {70try {71SwingUtilities.invokeAndWait(() -> {72p = comp.getLocationOnScreen();73});74} catch (IllegalStateException e) {75try {76Thread.sleep(1000);77} catch (InterruptedException ie) {78}79}80}81}8283public JTreeFocusTest() throws Exception {84SwingUtilities.invokeAndWait(() -> {85fr = new JFrame("Test");8687root = new DefaultMutableTreeNode("root");88JPanel p = new JPanel();89p.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED),90new LineBorder(UIManager.getColor("control"), 7)));91p.setLayout(new GridLayout(2,2));92p.add(new JLabel("one"));93JTextField tf1 = new JTextField(10);94p.add(tf1);95p.add(new JLabel("two"));96p.add(new JTextField(10));97root.add(new DefaultMutableTreeNode(p));9899tf1.addFocusListener(new FocusAdapter() {100public void focusGained(FocusEvent e) {101setPassed(true);102}103});104105DefaultTreeModel model = new DefaultTreeModel(root);106tree = new JTree(model) {107public void processKeyEvent(KeyEvent e) {108super.processKeyEvent(e);109if (e.getKeyCode()==KeyEvent.VK_F2) {110synchronized (JTreeFocusTest.this) {111keysTyped = true;112JTreeFocusTest.this.notifyAll();113}114}115}116};117118tree.addTreeSelectionListener(new TreeSelectionListener() {119public void valueChanged(TreeSelectionEvent e) {120if ( root.equals(e.getPath().getLastPathComponent()) ) {121synchronized (JTreeFocusTest.this) {122rootSelected = true;123JTreeFocusTest.this.notifyAll();124}125}126}127});128129tree.setEditable(true);130DefaultTreeCellRenderer renderer = new FormRenderer();131tree.setCellRenderer(renderer);132DefaultTreeCellEditor editor = new FormEditor(tree, renderer);133tree.setCellEditor(editor);134fr.getContentPane().add(tree);135136fr.setSize(300,400);137fr.setVisible(true);138});139blockTillDisplayed(tree);140SwingUtilities.invokeAndWait(() -> {141tree.requestFocus();142tree.setSelectionRow(0);143});144145try {146synchronized (this) {147while (!rootSelected) {148JTreeFocusTest.this.wait();149}150}151152robot = new Robot();153robot.setAutoDelay(50);154robot.delay(150);155robot.keyPress(KeyEvent.VK_DOWN);156robot.keyRelease(KeyEvent.VK_DOWN);157robot.keyPress(KeyEvent.VK_RIGHT);158robot.keyRelease(KeyEvent.VK_RIGHT);159robot.keyPress(KeyEvent.VK_F2);160robot.keyRelease(KeyEvent.VK_F2);161162synchronized (this) {163while (!keysTyped) {164JTreeFocusTest.this.wait();165}166}167Thread.sleep(3000);168} catch(Throwable t) {169t.printStackTrace();170}171destroy();172}173174public void destroy() throws Exception {175SwingUtilities.invokeAndWait(()->fr.dispose());176if ( !isPassed() ) {177throw new RuntimeException("Focus wasn't transferred to the proper component");178}179}180181synchronized void setPassed(boolean passed) {182this.passed = passed;183}184185synchronized boolean isPassed() {186return passed;187}188189static JTree createTree() {190return tree;191}192193class FormRenderer extends DefaultTreeCellRenderer {194public Component getTreeCellRendererComponent(JTree tree, Object value,195boolean sel,196boolean expanded,197boolean leaf, int row,198boolean hasFocus) {199Object obj = ((DefaultMutableTreeNode)value).getUserObject();200if (obj instanceof Component){201return (Component)((DefaultMutableTreeNode)value).getUserObject();202}203return super.getTreeCellRendererComponent(tree, value, sel,204expanded, leaf, row,205hasFocus);206}207}208209class FormEditor extends DefaultTreeCellEditor {210public FormEditor(JTree tree, DefaultTreeCellRenderer renderer) {211super(tree, renderer);212}213214public Component getTreeCellEditorComponent(JTree tree, Object value,215boolean sel,216boolean expanded,217boolean leaf, int row) {218Object obj = ((DefaultMutableTreeNode)value).getUserObject();219if (obj instanceof Component){220return (Component)((DefaultMutableTreeNode)value).getUserObject();221}222return super.getTreeCellEditorComponent(tree, value, sel,223expanded, leaf, row);224}225226public boolean shouldSelectCell(EventObject anEvent) {227//return super.shouldSelectCell(anEvent);228return true;229}230}231}232233234