1 /*
2  * Copyright (c) 2013, 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 8003400
28  * @summary Tests that JTree shows the last row
29  * @author Sergey Malenkov
30  * @library /lib/client
31  * @build ExtendedRobot
32  * @run main/othervm Test8003400
33  * @run main/othervm Test8003400 reverse
34  * @run main/othervm Test8003400 system
35  * @run main/othervm Test8003400 system reverse
36  */
37 
38 import java.awt.Rectangle;
39 import java.awt.Robot;
40 import java.awt.event.KeyEvent;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44 import javax.swing.JFrame;
45 import javax.swing.JScrollPane;
46 import javax.swing.JTree;
47 import javax.swing.SwingUtilities;
48 import javax.swing.UIManager;
49 import javax.swing.tree.DefaultMutableTreeNode;
50 
51 public class Test8003400 {
52 
53     private static final String TITLE = "Test JTree with a large model";
54     private static List<String> OBJECTS = Arrays.asList(TITLE, "x", "y", "z");
55     private static JScrollPane pane;
56 
main(String[] args)57     public static void main(String[] args) throws Exception {
58         for (String arg : args) {
59             if (arg.equals("reverse")) {
60                 Collections.reverse(OBJECTS);
61             }
62             if (arg.equals("system")) {
63                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
64             }
65         }
66         SwingUtilities.invokeAndWait(new Runnable() {
67             public void run() {
68                 DefaultMutableTreeNode root = new DefaultMutableTreeNode();
69 
70                 JTree tree = new JTree(root);
71                 tree.setLargeModel(true);
72                 tree.setRowHeight(16);
73 
74                 JFrame frame = new JFrame(TITLE);
75                 frame.add(pane = new JScrollPane(tree));
76                 frame.setSize(200, 100);
77                 frame.setLocationRelativeTo(null);
78                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
79                 frame.setVisible(true);
80 
81                 for (String object : OBJECTS) {
82                     root.add(new DefaultMutableTreeNode(object));
83                 }
84                 tree.expandRow(0);
85             }
86         });
87 
88         ExtendedRobot robot = new ExtendedRobot();
89         robot.waitForIdle(500);
90         robot.keyPress(KeyEvent.VK_END);
91         robot.waitForIdle(500);
92         robot.keyRelease(KeyEvent.VK_END);
93         robot.waitForIdle();
94 
95         SwingUtilities.invokeAndWait(new Runnable() {
96             public void run() {
97                 JTree tree = (JTree) pane.getViewport().getView();
98                 Rectangle inner = tree.getRowBounds(tree.getRowCount() - 1);
99                 Rectangle outer = SwingUtilities.convertRectangle(tree, inner, pane);
100                 outer.y += tree.getRowHeight() - 1 - pane.getVerticalScrollBar().getHeight();
101                 // error reporting only for automatic testing
102                 if (null != System.getProperty("test.src", null)) {
103                     SwingUtilities.getWindowAncestor(pane).dispose();
104                     if (outer.y != 0) {
105                         throw new Error("TEST FAILED: " + outer.y);
106                     }
107                 }
108             }
109         });
110     }
111 }
112