1 /* 2 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* @test 24 @bug 4633594 8172012 25 @summary No way to pass focus from a JTree to a GUI placed inside the tree node 26 @run main JTreeFocusTest 27 */ 28 import java.awt.Component; 29 import java.awt.GridLayout; 30 import java.awt.Point; 31 import java.awt.Robot; 32 import java.awt.event.FocusAdapter; 33 import java.awt.event.FocusEvent; 34 import java.awt.event.KeyEvent; 35 import java.util.EventObject; 36 import javax.swing.JComponent; 37 import javax.swing.JFrame; 38 import javax.swing.JLabel; 39 import javax.swing.JPanel; 40 import javax.swing.JTextField; 41 import javax.swing.JTree; 42 import javax.swing.SwingUtilities; 43 import javax.swing.UIManager; 44 import javax.swing.border.BevelBorder; 45 import javax.swing.border.CompoundBorder; 46 import javax.swing.border.LineBorder; 47 import javax.swing.event.TreeSelectionEvent; 48 import javax.swing.event.TreeSelectionListener; 49 import javax.swing.tree.DefaultMutableTreeNode; 50 import javax.swing.tree.DefaultTreeCellEditor; 51 import javax.swing.tree.DefaultTreeCellRenderer; 52 import javax.swing.tree.DefaultTreeModel; 53 54 public class JTreeFocusTest { 55 56 private static DefaultMutableTreeNode root; 57 Robot robot; 58 static boolean passed = false; 59 boolean rootSelected = false; 60 boolean keysTyped = false; 61 private volatile Point p = null; 62 private static JFrame fr; 63 private static volatile JTree tree = null; 64 65 public static void main(String[] args) throws Exception{ 66 new JTreeFocusTest(); 67 } 68 69 void blockTillDisplayed(JComponent comp) throws Exception { 70 while (p == null) { 71 try { 72 SwingUtilities.invokeAndWait(() -> { 73 p = comp.getLocationOnScreen(); 74 }); 75 } catch (IllegalStateException e) { 76 try { 77 Thread.sleep(1000); 78 } catch (InterruptedException ie) { 79 } 80 } 81 } 82 } 83 84 public JTreeFocusTest() throws Exception { 85 SwingUtilities.invokeAndWait(() -> { 86 fr = new JFrame("Test"); 87 88 root = new DefaultMutableTreeNode("root"); 89 JPanel p = new JPanel(); 90 p.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED), 91 new LineBorder(UIManager.getColor("control"), 7))); 92 p.setLayout(new GridLayout(2,2)); 93 p.add(new JLabel("one")); 94 JTextField tf1 = new JTextField(10); 95 p.add(tf1); 96 p.add(new JLabel("two")); 97 p.add(new JTextField(10)); 98 root.add(new DefaultMutableTreeNode(p)); 99 100 tf1.addFocusListener(new FocusAdapter() { 101 public void focusGained(FocusEvent e) { 102 setPassed(true); 103 } 104 }); 105 106 DefaultTreeModel model = new DefaultTreeModel(root); 107 tree = new JTree(model) { 108 public void processKeyEvent(KeyEvent e) { 109 super.processKeyEvent(e); 110 if (e.getKeyCode()==KeyEvent.VK_F2) { 111 synchronized (JTreeFocusTest.this) { 112 keysTyped = true; 113 JTreeFocusTest.this.notifyAll(); 114 } 115 } 116 } 117 }; 118 119 tree.addTreeSelectionListener(new TreeSelectionListener() { 120 public void valueChanged(TreeSelectionEvent e) { 121 if ( root.equals(e.getPath().getLastPathComponent()) ) { 122 synchronized (JTreeFocusTest.this) { 123 rootSelected = true; 124 JTreeFocusTest.this.notifyAll(); 125 } 126 } 127 } 128 }); 129 130 tree.setEditable(true); 131 DefaultTreeCellRenderer renderer = new FormRenderer(); 132 tree.setCellRenderer(renderer); 133 DefaultTreeCellEditor editor = new FormEditor(tree, renderer); 134 tree.setCellEditor(editor); 135 fr.getContentPane().add(tree); 136 137 fr.setSize(300,400); 138 fr.setVisible(true); 139 }); 140 blockTillDisplayed(tree); 141 SwingUtilities.invokeAndWait(() -> { 142 tree.requestFocus(); 143 tree.setSelectionRow(0); 144 }); 145 146 try { 147 synchronized (this) { 148 while (!rootSelected) { 149 JTreeFocusTest.this.wait(); 150 } 151 } 152 153 robot = new Robot(); 154 robot.setAutoDelay(50); 155 robot.delay(150); 156 robot.keyPress(KeyEvent.VK_DOWN); 157 robot.keyRelease(KeyEvent.VK_DOWN); 158 robot.keyPress(KeyEvent.VK_RIGHT); 159 robot.keyRelease(KeyEvent.VK_RIGHT); 160 robot.keyPress(KeyEvent.VK_F2); 161 robot.keyRelease(KeyEvent.VK_F2); 162 163 synchronized (this) { 164 while (!keysTyped) { 165 JTreeFocusTest.this.wait(); 166 } 167 } 168 Thread.sleep(3000); 169 } catch(Throwable t) { 170 t.printStackTrace(); 171 } 172 destroy(); 173 } 174 175 public void destroy() throws Exception { 176 SwingUtilities.invokeAndWait(()->fr.dispose()); 177 if ( !isPassed() ) { 178 throw new RuntimeException("Focus wasn't transferred to the proper component"); 179 } 180 } 181 182 synchronized void setPassed(boolean passed) { 183 this.passed = passed; 184 } 185 186 synchronized boolean isPassed() { 187 return passed; 188 } 189 190 static JTree createTree() { 191 return tree; 192 } 193 194 class FormRenderer extends DefaultTreeCellRenderer { 195 public Component getTreeCellRendererComponent(JTree tree, Object value, 196 boolean sel, 197 boolean expanded, 198 boolean leaf, int row, 199 boolean hasFocus) { 200 Object obj = ((DefaultMutableTreeNode)value).getUserObject(); 201 if (obj instanceof Component){ 202 return (Component)((DefaultMutableTreeNode)value).getUserObject(); 203 } 204 return super.getTreeCellRendererComponent(tree, value, sel, 205 expanded, leaf, row, 206 hasFocus); 207 } 208 } 209 210 class FormEditor extends DefaultTreeCellEditor { 211 public FormEditor(JTree tree, DefaultTreeCellRenderer renderer) { 212 super(tree, renderer); 213 } 214 215 public Component getTreeCellEditorComponent(JTree tree, Object value, 216 boolean sel, 217 boolean expanded, 218 boolean leaf, int row) { 219 Object obj = ((DefaultMutableTreeNode)value).getUserObject(); 220 if (obj instanceof Component){ 221 return (Component)((DefaultMutableTreeNode)value).getUserObject(); 222 } 223 return super.getTreeCellEditorComponent(tree, value, sel, 224 expanded, leaf, row); 225 } 226 227 public boolean shouldSelectCell(EventObject anEvent) { 228 //return super.shouldSelectCell(anEvent); 229 return true; 230 } 231 } 232 }