001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.explorer;
043:
044: import org.openide.nodes.Node;
045: import org.openide.nodes.Children;
046: import org.openide.nodes.AbstractNode;
047: import java.util.Collections;
048: import java.util.Arrays;
049: import java.awt.BorderLayout;
050: import org.openide.explorer.view.BeanTreeView;
051: import javax.swing.JLabel;
052: import org.netbeans.junit.NbTestCase;
053: import org.openide.util.HelpCtx;
054:
055: /** Test finding help IDs on explorers.
056: * @author Jesse Glick
057: * @see "#14701"
058: */
059: public class FindHelpTest extends NbTestCase {
060:
061: public FindHelpTest(String name) {
062: super (name);
063: }
064:
065: private static Node[] nodes;
066: private static Node root;
067:
068: protected void setUp() throws Exception {
069: Children kids = new Children.Array();
070: nodes = new Node[] { new NoHelpNode(), new WithHelpNode("foo"),
071: new WithHelpNode("bar"), new WithHelpNode("foo"), };
072: kids.add(nodes);
073: root = new AbstractNode(kids);
074: }
075:
076: public void testFindHelpOnExplorer() throws Exception {
077: ExplorerPanel p = new ExplorerPanel();
078: ExplorerManager m = p.getExplorerManager();
079: m.setRootContext(root);
080: assertEquals(Collections.EMPTY_LIST, Arrays.asList(m
081: .getSelectedNodes()));
082: HelpCtx base = new HelpCtx(ExplorerPanel.class);
083: assertEquals(base, p.getHelpCtx());
084: m.setSelectedNodes(new Node[] { nodes[0] });
085: assertEquals(base, p.getHelpCtx());
086: m.setSelectedNodes(new Node[] { nodes[1] });
087: assertEquals(new HelpCtx("foo"), p.getHelpCtx());
088: m.setSelectedNodes(new Node[] { nodes[1], nodes[2] });
089: assertEquals(base, p.getHelpCtx());
090: m.setSelectedNodes(new Node[] { nodes[1], nodes[3] });
091: assertEquals(new HelpCtx("foo"), p.getHelpCtx());
092: p = new WithHelpExplorer();
093: m = p.getExplorerManager();
094: m.setRootContext(root);
095: assertEquals(Collections.EMPTY_LIST, Arrays.asList(m
096: .getSelectedNodes()));
097: base = new HelpCtx("base");
098: assertEquals(base, p.getHelpCtx());
099: m.setSelectedNodes(new Node[] { nodes[0] });
100: assertEquals(base, p.getHelpCtx());
101: m.setSelectedNodes(new Node[] { nodes[1] });
102: assertEquals(new HelpCtx("foo"), p.getHelpCtx());
103: m.setSelectedNodes(new Node[] { nodes[1], nodes[2] });
104: assertEquals(base, p.getHelpCtx());
105: m.setSelectedNodes(new Node[] { nodes[1], nodes[3] });
106: assertEquals(new HelpCtx("foo"), p.getHelpCtx());
107: }
108:
109: public void testFindHelpHierarchically() throws Exception {
110: ExplorerPanel p = new ExplorerPanel();
111: p.setLayout(new BorderLayout());
112: BeanTreeView b = new BeanTreeView();
113: p.add(b, BorderLayout.CENTER);
114: JLabel l1 = new JLabel("test1");
115: HelpCtx.setHelpIDString(l1, "test");
116: p.add(l1, BorderLayout.NORTH);
117: JLabel l2 = new JLabel("test2");
118: assertEquals(HelpCtx.DEFAULT_HELP, HelpCtx.findHelp(l2));
119: p.add(l2, BorderLayout.SOUTH);
120: ExplorerManager m = p.getExplorerManager();
121: m.setRootContext(root);
122: assertEquals(Collections.EMPTY_LIST, Arrays.asList(m
123: .getSelectedNodes()));
124: HelpCtx base = new HelpCtx(ExplorerPanel.class);
125: assertEquals(base, HelpCtx.findHelp(b));
126: m.setSelectedNodes(new Node[] { nodes[0] });
127: assertEquals(base, HelpCtx.findHelp(b));
128: m.setSelectedNodes(new Node[] { nodes[1] });
129: assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(b));
130: m.setSelectedNodes(new Node[] { nodes[1], nodes[2] });
131: assertEquals(base, HelpCtx.findHelp(b));
132: m.setSelectedNodes(new Node[] { nodes[1], nodes[3] });
133: assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(b));
134: assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(p));
135: assertEquals(new HelpCtx("test"), HelpCtx.findHelp(l1));
136: assertEquals(new HelpCtx("foo"), HelpCtx.findHelp(l2));
137: }
138:
139: private static final class NoHelpNode extends AbstractNode {
140: public NoHelpNode() {
141: super (Children.LEAF);
142: }
143: }
144:
145: private static final class WithHelpNode extends AbstractNode {
146: private final String id;
147:
148: public WithHelpNode(String id) {
149: super (Children.LEAF);
150: this .id = id;
151: }
152:
153: public HelpCtx getHelpCtx() {
154: return new HelpCtx(id);
155: }
156: }
157:
158: private static final class WithHelpExplorer extends ExplorerPanel {
159: public HelpCtx getHelpCtx() {
160: return getHelpCtx(getExplorerManager().getSelectedNodes(),
161: new HelpCtx("base"));
162: }
163: }
164:
165: }
|