KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > FindHelpTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer;
21
22 import org.openide.nodes.Node;
23 import org.openide.nodes.Children;
24 import org.openide.nodes.AbstractNode;
25 import java.util.Collections JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.awt.BorderLayout JavaDoc;
28 import org.openide.explorer.view.BeanTreeView;
29 import javax.swing.JLabel JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.util.HelpCtx;
32
33 /** Test finding help IDs on explorers.
34  * @author Jesse Glick
35  * @see "#14701"
36  */

37 public class FindHelpTest extends NbTestCase {
38
39     public FindHelpTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     private static Node[] nodes;
44     private static Node root;
45
46     protected void setUp() throws Exception JavaDoc {
47         Children kids = new Children.Array();
48         nodes = new Node[] {
49             new NoHelpNode(),
50             new WithHelpNode("foo"),
51             new WithHelpNode("bar"),
52             new WithHelpNode("foo"),
53         };
54         kids.add(nodes);
55         root = new AbstractNode(kids);
56     }
57     
58     public void testFindHelpOnExplorer() throws Exception JavaDoc {
59         ExplorerPanel p = new ExplorerPanel();
60         ExplorerManager m = p.getExplorerManager();
61         m.setRootContext(root);
62         assertEquals(Collections.EMPTY_LIST, Arrays.asList(m.getSelectedNodes()));
63         HelpCtx base = new HelpCtx(ExplorerPanel.class);
64         assertEquals(base, p.getHelpCtx());
65         m.setSelectedNodes(new Node[] {nodes[0]});
66         assertEquals(base, p.getHelpCtx());
67         m.setSelectedNodes(new Node[] {nodes[1]});
68         assertEquals(new HelpCtx("foo"), p.getHelpCtx());
69         m.setSelectedNodes(new Node[] {nodes[1], nodes[2]});
70         assertEquals(base, p.getHelpCtx());
71         m.setSelectedNodes(new Node[] {nodes[1], nodes[3]});
72         assertEquals(new HelpCtx("foo"), p.getHelpCtx());
73         p = new WithHelpExplorer();
74         m = p.getExplorerManager();
75         m.setRootContext(root);
76         assertEquals(Collections.EMPTY_LIST, Arrays.asList(m.getSelectedNodes()));
77         base = new HelpCtx("base");
78         assertEquals(base, p.getHelpCtx());
79         m.setSelectedNodes(new Node[] {nodes[0]});
80         assertEquals(base, p.getHelpCtx());
81         m.setSelectedNodes(new Node[] {nodes[1]});
82         assertEquals(new HelpCtx("foo"), p.getHelpCtx());
83         m.setSelectedNodes(new Node[] {nodes[1], nodes[2]});
84         assertEquals(base, p.getHelpCtx());
85         m.setSelectedNodes(new Node[] {nodes[1], nodes[3]});
86         assertEquals(new HelpCtx("foo"), p.getHelpCtx());
87     }
88     
89     public void testFindHelpHierarchically() throws Exception JavaDoc {
90         ExplorerPanel p = new ExplorerPanel();
91         p.setLayout(new BorderLayout JavaDoc());
92         BeanTreeView b = new BeanTreeView();
93         p.add(b, BorderLayout.CENTER);
94         JLabel JavaDoc l1 = new JLabel JavaDoc("test1");
95         HelpCtx.setHelpIDString(l1, "test");
96         p.add(l1, BorderLayout.NORTH);
97         JLabel JavaDoc l2 = new JLabel JavaDoc("test2");
98         assertEquals(HelpCtx.DEFAULT_HELP, HelpCtx.findHelp(l2));
99         p.add(l2, BorderLayout.SOUTH);
100         ExplorerManager m = p.getExplorerManager();
101         m.setRootContext(root);
102         assertEquals(Collections.EMPTY_LIST, Arrays.asList(m.getSelectedNodes()));
103         HelpCtx base = new HelpCtx(ExplorerPanel.class);
104         assertEquals(base, HelpCtx.findHelp(b));
105         m.setSelectedNodes(new Node[] {nodes[0]});
106         assertEquals(base, HelpCtx.findHelp(b));
107         m.setSelectedNodes(new Node[] {nodes[1]});
108         assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(b));
109         m.setSelectedNodes(new Node[] {nodes[1], nodes[2]});
110         assertEquals(base, HelpCtx.findHelp(b));
111         m.setSelectedNodes(new Node[] {nodes[1], nodes[3]});
112         assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(b));
113         assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(p));
114         assertEquals(new HelpCtx("test"), HelpCtx.findHelp(l1));
115         assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(l2));
116     }
117     
118     private static final class NoHelpNode extends AbstractNode {
119         public NoHelpNode() {
120             super(Children.LEAF);
121         }
122     }
123     
124     private static final class WithHelpNode extends AbstractNode {
125         private final String JavaDoc id;
126         public WithHelpNode(String JavaDoc id) {
127             super(Children.LEAF);
128             this.id = id;
129         }
130         public HelpCtx getHelpCtx() {
131             return new HelpCtx(id);
132         }
133     }
134     
135     private static final class WithHelpExplorer extends ExplorerPanel {
136         public HelpCtx getHelpCtx() {
137             return getHelpCtx(getExplorerManager().getSelectedNodes(), new HelpCtx("base"));
138         }
139     }
140     
141 }
142
Popular Tags