1 /*
   2  * Copyright (c) 2013, 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 
  24 /*
  25  * @test
  26  * @key headful
  27  * @bug 8004298
  28  * @requires (os.family == "windows")
  29  * @summary NPE in WindowsTreeUI.ensureRowsAreVisible
  30  * @author Alexander Scherbatiy
  31  * @library ../../regtesthelpers
  32  * @modules java.desktop/com.sun.java.swing.plaf.windows
  33  * @build Util
  34  * @run main bug8004298
  35  */
  36 
  37 import java.awt.*;
  38 import java.awt.event.InputEvent;
  39 import javax.swing.*;
  40 import javax.swing.tree.*;
  41 import java.util.concurrent.Callable;
  42 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  43 import com.sun.java.swing.plaf.windows.WindowsTreeUI;
  44 
  45 public class bug8004298 {
  46 
  47     private static JTree tree;
  48 
  49     public static void main(String[] args) throws Exception {
  50         Robot robot = new Robot();
  51         robot.setAutoDelay(50);
  52         try {
  53             UIManager.setLookAndFeel(new WindowsLookAndFeel());
  54         } catch (javax.swing.UnsupportedLookAndFeelException ulafe) {
  55             System.out.println(ulafe.getMessage());
  56             System.out.println("The test is considered PASSED");
  57             return;
  58         }
  59         SwingUtilities.invokeAndWait(new Runnable() {
  60 
  61             @Override
  62             public void run() {
  63                 createAndShowGUI();
  64             }
  65         });
  66 
  67         robot.waitForIdle();
  68 
  69         Point point = Util.invokeOnEDT(new Callable<Point>() {
  70 
  71             @Override
  72             public Point call() throws Exception {
  73                 Rectangle rect = tree.getRowBounds(2);
  74                 Point p = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
  75                 SwingUtilities.convertPointToScreen(p, tree);
  76                 return p;
  77             }
  78         });
  79 
  80         robot.mouseMove(point.x, point.y);
  81         robot.mousePress(InputEvent.BUTTON1_MASK);
  82         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  83         robot.mousePress(InputEvent.BUTTON1_MASK);
  84         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  85         robot.waitForIdle();
  86 
  87     }
  88 
  89     private static void createAndShowGUI() {
  90         JFrame frame = new JFrame();
  91         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  92 
  93         DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
  94         root.add(new DefaultMutableTreeNode("colors"));
  95         DefaultMutableTreeNode sports = new DefaultMutableTreeNode("sports");
  96         sports.add(new DefaultMutableTreeNode("basketball"));
  97         sports.add(new DefaultMutableTreeNode("football"));
  98         root.add(sports);
  99 
 100         tree = new JTree(root);
 101         tree.setUI(new NullReturningTreeUI());
 102 
 103         frame.getContentPane().add(tree);
 104         frame.pack();
 105         frame.setVisible(true);
 106 
 107     }
 108 
 109     private static final class NullReturningTreeUI extends WindowsTreeUI {
 110 
 111         @Override
 112         public Rectangle getPathBounds(JTree tree, TreePath path) {
 113             // the method can return null and callers have to be ready for
 114             // that. Simulate the case by returning null for unknown reason.
 115             if (path != null && path.toString().contains("football")) {
 116                 return null;
 117             }
 118 
 119             return super.getPathBounds(tree, path);
 120         }
 121     }
 122 }