|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2023, 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 | +/* @test |
| 25 | + * @bug 4210432 |
| 26 | + * @summary Tests if JTree allows nodes not visible to be selected |
| 27 | + * @run main bug4210432 |
| 28 | + */ |
| 29 | + |
| 30 | +import javax.swing.JPanel; |
| 31 | +import javax.swing.JTree; |
| 32 | +import javax.swing.SwingUtilities; |
| 33 | +import javax.swing.tree.DefaultMutableTreeNode; |
| 34 | +import javax.swing.tree.DefaultTreeModel; |
| 35 | +import javax.swing.tree.TreePath; |
| 36 | + |
| 37 | +public class bug4210432 { |
| 38 | + public static void main(String[] args) throws Exception { |
| 39 | + SwingUtilities.invokeAndWait(() -> { |
| 40 | + JPanel p = new JPanel(); |
| 41 | + DefaultMutableTreeNode expansible = |
| 42 | + new DefaultMutableTreeNode("expansible"); |
| 43 | + DefaultMutableTreeNode unexpansible = |
| 44 | + new DefaultMutableTreeNode("unexpansible"); |
| 45 | + DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); |
| 46 | + DefaultMutableTreeNode subexpansible1 = |
| 47 | + new DefaultMutableTreeNode("sub-expansible 1"); |
| 48 | + DefaultMutableTreeNode subexpansible2 = |
| 49 | + new DefaultMutableTreeNode("sub-expansible 2"); |
| 50 | + DefaultMutableTreeNode subsubexpansible1 = |
| 51 | + new DefaultMutableTreeNode("sub-sub-expansible 1"); |
| 52 | + DefaultMutableTreeNode subsubexpansible2 = |
| 53 | + new DefaultMutableTreeNode("sub-sub-expansible 2"); |
| 54 | + expansible.add(subexpansible1); |
| 55 | + expansible.add(subexpansible2); |
| 56 | + subexpansible1.add(subsubexpansible1); |
| 57 | + subexpansible1.add(subsubexpansible2); |
| 58 | + root.add(expansible); |
| 59 | + root.add(unexpansible); |
| 60 | + DefaultTreeModel model = new DefaultTreeModel(root); |
| 61 | + JTree t = new JTree(model); |
| 62 | + Object[] tpa = {root, expansible, subexpansible1}; |
| 63 | + Object[] tpa2 = {root, expansible}; |
| 64 | + t.setExpandsSelectedPaths(false); |
| 65 | + t.setSelectionPath(new TreePath(tpa)); |
| 66 | + p.add(t); |
| 67 | + if (t.isExpanded(new TreePath(tpa2))) { |
| 68 | + throw new RuntimeException("Test failed: JTree should not have " + |
| 69 | + "expanded path"); |
| 70 | + } |
| 71 | + t.clearSelection(); |
| 72 | + t.setExpandsSelectedPaths(true); |
| 73 | + t.setSelectionPath(new TreePath(tpa)); |
| 74 | + if (!t.isExpanded(new TreePath(tpa2))) { |
| 75 | + throw new RuntimeException("Test failed: JTree should have " + |
| 76 | + "expanded path"); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
0 commit comments