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