1 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 ; 26 import java.util.Arrays ; 27 import java.awt.BorderLayout ; 28 import org.openide.explorer.view.BeanTreeView; 29 import javax.swing.JLabel ; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.util.HelpCtx; 32 33 37 public class FindHelpTest extends NbTestCase { 38 39 public FindHelpTest(String name) { 40 super(name); 41 } 42 43 private static Node[] nodes; 44 private static Node root; 45 46 protected void setUp() throws Exception { 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 { 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 { 90 ExplorerPanel p = new ExplorerPanel(); 91 p.setLayout(new BorderLayout ()); 92 BeanTreeView b = new BeanTreeView(); 93 p.add(b, BorderLayout.CENTER); 94 JLabel l1 = new JLabel ("test1"); 95 HelpCtx.setHelpIDString(l1, "test"); 96 p.add(l1, BorderLayout.NORTH); 97 JLabel l2 = new JLabel ("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 id; 126 public WithHelpNode(String 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 |