1 /*
2  * Copyright (c) 2012, 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 6505523
28  * @summary NullPointerException in BasicTreeUI when a node is removed by expansion listener
29  * @author Alexandr Scherbatiy
30  * @run main bug6505523
31  */
32 import java.awt.Point;
33 import java.awt.Rectangle;
34 import java.awt.Robot;
35 import java.awt.event.InputEvent;
36 import javax.swing.JFrame;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTree;
39 import javax.swing.SwingUtilities;
40 import javax.swing.event.TreeExpansionEvent;
41 import javax.swing.event.TreeExpansionListener;
42 import javax.swing.tree.DefaultMutableTreeNode;
43 import javax.swing.tree.DefaultTreeModel;
44 import javax.swing.tree.TreeNode;
45 
46 public class bug6505523 {
47 
48     private static JTree tree;
49 
main(String[] args)50     public static void main(String[] args) throws Exception {
51         Robot robot = new Robot();
52         robot.setAutoDelay(50);
53 
54         SwingUtilities.invokeAndWait(new Runnable() {
55 
56             @Override
57             public void run() {
58                 createAndShowGUI();
59             }
60         });
61 
62         robot.waitForIdle();
63 
64         Point point = getRowPointToClick(2);
65         robot.mouseMove(point.x, point.y);
66         robot.mousePress(InputEvent.BUTTON1_MASK);
67         robot.mouseRelease(InputEvent.BUTTON1_MASK);
68 
69         robot.waitForIdle();
70 
71     }
72 
getRowPointToClick(final int row)73     private static Point getRowPointToClick(final int row) throws Exception {
74 
75         final Point[] result = new Point[1];
76 
77         SwingUtilities.invokeAndWait(new Runnable() {
78 
79             @Override
80             public void run() {
81                 Rectangle rect = tree.getRowBounds(row);
82                 Point point = new Point(rect.x - 5, rect.y + rect.height / 2);
83                 SwingUtilities.convertPointToScreen(point, tree);
84                 result[0] = point;
85             }
86         });
87 
88         return result[0];
89     }
90 
createAndShowGUI()91     private static void createAndShowGUI() {
92         final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6");
93         final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr");
94         problematic.add(new DefaultMutableTreeNode("some content"));
95         root.add(new DefaultMutableTreeNode("irrelevant..."));
96         root.add(problematic);
97 
98         final DefaultTreeModel model = new DefaultTreeModel(root);
99         tree = new JTree(model);
100         tree.setRootVisible(true);
101         tree.setShowsRootHandles(true);
102         tree.expandRow(0);
103         tree.collapseRow(2);
104 
105         // this is critical - without dragEnabled everything works
106         tree.setDragEnabled(true);
107 
108         tree.addTreeExpansionListener(new TreeExpansionListener() {
109 
110             @Override
111             public void treeExpanded(TreeExpansionEvent event) {
112                 TreeNode parent = problematic.getParent();
113                 if (parent instanceof DefaultMutableTreeNode) {
114                     model.removeNodeFromParent(problematic);
115                 }
116             }
117 
118             @Override
119             public void treeCollapsed(TreeExpansionEvent event) {
120             }
121         });
122 
123         JFrame frame = new JFrame("JTree Problem");
124         frame.add(new JScrollPane(tree));
125         frame.setSize(500, 300);
126         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
127         frame.setLocationRelativeTo(null);
128         frame.setVisible(true);
129     }
130 }
131