rev 17435 : 8183286: Some java/awt and javax/swing tests miss headful jtreg keyword

   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    @key headful
  25    @bug 4633594 8172012
  26    @summary No way to pass focus from a JTree to a GUI placed inside the tree node
  27    @run main JTreeFocusTest
  28 */
  29 import java.awt.Component;
  30 import java.awt.GridLayout;
  31 import java.awt.Point;
  32 import java.awt.Robot;
  33 import java.awt.event.FocusAdapter;
  34 import java.awt.event.FocusEvent;
  35 import java.awt.event.KeyEvent;
  36 import java.util.EventObject;
  37 import javax.swing.JComponent;
  38 import javax.swing.JFrame;
  39 import javax.swing.JLabel;
  40 import javax.swing.JPanel;
  41 import javax.swing.JTextField;
  42 import javax.swing.JTree;
  43 import javax.swing.SwingUtilities;
  44 import javax.swing.UIManager;
  45 import javax.swing.border.BevelBorder;
  46 import javax.swing.border.CompoundBorder;
  47 import javax.swing.border.LineBorder;
  48 import javax.swing.event.TreeSelectionEvent;
  49 import javax.swing.event.TreeSelectionListener;
  50 import javax.swing.tree.DefaultMutableTreeNode;
  51 import javax.swing.tree.DefaultTreeCellEditor;
  52 import javax.swing.tree.DefaultTreeCellRenderer;
  53 import javax.swing.tree.DefaultTreeModel;
  54 
  55 public class JTreeFocusTest {
  56 
  57     private static DefaultMutableTreeNode root;
  58     Robot robot;
  59     static boolean passed = false;
  60     boolean rootSelected = false;
  61     boolean keysTyped = false;
  62     private volatile Point p = null;
  63     private static JFrame fr;
  64     private static volatile JTree tree = null;
  65 
  66     public static void main(String[] args) throws Exception{
  67          new JTreeFocusTest();
  68     }
  69 
  70     void blockTillDisplayed(JComponent comp) throws Exception {
  71         while (p == null) {
  72             try {
  73                 SwingUtilities.invokeAndWait(() -> {
  74                     p = comp.getLocationOnScreen();
  75                 });
  76             } catch (IllegalStateException e) {
  77                 try {
  78                     Thread.sleep(1000);
  79                 } catch (InterruptedException ie) {
  80                 }
  81             }
  82         }
  83     }
  84 
  85     public JTreeFocusTest() throws Exception {
  86         SwingUtilities.invokeAndWait(() -> {
  87             fr = new JFrame("Test");
  88 
  89             root = new DefaultMutableTreeNode("root");
  90             JPanel p = new JPanel();
  91             p.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED),
  92                     new LineBorder(UIManager.getColor("control"), 7)));
  93             p.setLayout(new GridLayout(2,2));
  94             p.add(new JLabel("one"));
  95             JTextField tf1  = new JTextField(10);
  96             p.add(tf1);
  97             p.add(new JLabel("two"));
  98             p.add(new JTextField(10));
  99             root.add(new DefaultMutableTreeNode(p));
 100 
 101             tf1.addFocusListener(new FocusAdapter() {
 102                 public void focusGained(FocusEvent e) {
 103                     setPassed(true);
 104                 }
 105             });
 106 
 107             DefaultTreeModel model = new DefaultTreeModel(root);
 108             tree = new JTree(model) {
 109                 public void processKeyEvent(KeyEvent e) {
 110                     super.processKeyEvent(e);
 111                     if (e.getKeyCode()==KeyEvent.VK_F2) {
 112                         synchronized (JTreeFocusTest.this) {
 113                             keysTyped = true;
 114                             JTreeFocusTest.this.notifyAll();
 115                         }
 116                     }
 117                 }
 118             };
 119 
 120             tree.addTreeSelectionListener(new TreeSelectionListener() {
 121                 public void valueChanged(TreeSelectionEvent e) {
 122                     if ( root.equals(e.getPath().getLastPathComponent()) ) {
 123                         synchronized (JTreeFocusTest.this) {
 124                             rootSelected = true;
 125                             JTreeFocusTest.this.notifyAll();
 126                         }
 127                     }
 128                 }
 129             });
 130 
 131             tree.setEditable(true);
 132             DefaultTreeCellRenderer renderer = new FormRenderer();
 133             tree.setCellRenderer(renderer);
 134             DefaultTreeCellEditor editor = new FormEditor(tree, renderer);
 135             tree.setCellEditor(editor);
 136             fr.getContentPane().add(tree);
 137 
 138             fr.setSize(300,400);
 139             fr.setVisible(true);
 140         });
 141         blockTillDisplayed(tree);
 142         SwingUtilities.invokeAndWait(() -> {
 143             tree.requestFocus();
 144             tree.setSelectionRow(0);
 145         });
 146 
 147         try {
 148             synchronized (this) {
 149                 while (!rootSelected) {
 150                     JTreeFocusTest.this.wait();
 151                 }
 152             }
 153 
 154             robot = new Robot();
 155             robot.setAutoDelay(50);
 156             robot.delay(150);
 157             robot.keyPress(KeyEvent.VK_DOWN);
 158             robot.keyRelease(KeyEvent.VK_DOWN);
 159             robot.keyPress(KeyEvent.VK_RIGHT);
 160             robot.keyRelease(KeyEvent.VK_RIGHT);
 161             robot.keyPress(KeyEvent.VK_F2);
 162             robot.keyRelease(KeyEvent.VK_F2);
 163 
 164             synchronized (this) {
 165                 while (!keysTyped) {
 166                     JTreeFocusTest.this.wait();
 167                 }
 168             }
 169             Thread.sleep(3000);
 170         } catch(Throwable t) {
 171             t.printStackTrace();
 172         }
 173         destroy();
 174     }
 175 
 176     public void destroy() throws Exception {
 177         SwingUtilities.invokeAndWait(()->fr.dispose());
 178         if ( !isPassed() ) {
 179             throw new RuntimeException("Focus wasn't transferred to the proper component");
 180         }
 181     }
 182 
 183     synchronized void setPassed(boolean passed) {
 184         this.passed = passed;
 185     }
 186 
 187     synchronized boolean isPassed() {
 188         return passed;
 189     }
 190 
 191     static JTree createTree() {
 192         return tree;
 193     }
 194 
 195     class FormRenderer extends DefaultTreeCellRenderer {
 196         public Component getTreeCellRendererComponent(JTree tree, Object value,
 197                                                       boolean sel,
 198                                                       boolean expanded,
 199                                                       boolean leaf, int row,
 200                                                       boolean hasFocus) {
 201             Object obj = ((DefaultMutableTreeNode)value).getUserObject();
 202             if (obj instanceof Component){
 203                 return (Component)((DefaultMutableTreeNode)value).getUserObject();
 204             }
 205             return super.getTreeCellRendererComponent(tree, value, sel,
 206                                                       expanded, leaf, row,
 207                                                       hasFocus);
 208         }
 209     }
 210 
 211     class FormEditor extends DefaultTreeCellEditor {
 212         public FormEditor(JTree tree, DefaultTreeCellRenderer renderer) {
 213             super(tree, renderer);
 214         }
 215 
 216         public Component getTreeCellEditorComponent(JTree tree, Object value,
 217                                                       boolean sel,
 218                                                       boolean expanded,
 219                                                       boolean leaf, int row) {
 220             Object obj = ((DefaultMutableTreeNode)value).getUserObject();
 221             if (obj instanceof Component){
 222                 return (Component)((DefaultMutableTreeNode)value).getUserObject();
 223             }
 224             return super.getTreeCellEditorComponent(tree, value, sel,
 225                                                     expanded, leaf, row);
 226         }
 227 
 228         public boolean shouldSelectCell(EventObject anEvent) {
 229             //return super.shouldSelectCell(anEvent);
 230             return true;
 231         }
 232     }
 233 }
--- EOF ---