Index: openide/src/org/openide/explorer/ExplorerUtils.java =================================================================== RCS file: /cvs/openide/src/org/openide/explorer/ExplorerUtils.java,v --- openide/src/org/openide/explorer/ExplorerUtils.java 20 Jan 2004 10:10:16 -0000 1.4 +++ openide/src/org/openide/explorer/ExplorerUtils.java 10 Jun 2004 12:12:17 -0000 @@ -186,4 +186,29 @@ return new DefaultEMLookup (em, map); } + /** Utility method to get context help from a node selection. + * Tries to find context helps for selected nodes. + * If there are some, and they all agree, uses that. + * In all other cases, uses the supplied generic help. + * + * @param sel a list of nodes to search for help in + * @param def the default help to use if they have none or do not agree + * @return a help context + */ + public static HelpCtx getHelpCtx (Node[] sel, HelpCtx def) { + HelpCtx result = null; + for (int i = 0; i < sel.length; i++) { + HelpCtx attempt = sel[i].getHelpCtx (); + if (attempt != null && ! attempt.equals (HelpCtx.DEFAULT_HELP)) { + if (result == null || result.equals (attempt)) { + result = attempt; + } else { + // More than one found, and they conflict. Get general help on the Explorer instead. + result = null; + break; + } + } + } + return result != null ? result : def; + } } Index: projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java =================================================================== RCS file: /cvs/projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java,v --- projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java 1 Jun 2004 12:15:20 -0000 1.7 +++ projects/projectui/src/org/netbeans/modules/project/ui/ProjectTab.java 10 Jun 2004 12:12:17 -0000 @@ -83,7 +83,7 @@ public ProjectTab() { - // This should not be here byt for some reason it does not work + // This should not be here but for some reason it does not work // In the module installer Hacks.hackFolderActions(); @@ -194,7 +194,9 @@ } public HelpCtx getHelpCtx() { - return new HelpCtx(ProjectTab.class); + return ExplorerUtils.getHelpCtx( + manager.getSelectedNodes(), + new HelpCtx( ProjectTab.class ) ); } public int getPersistenceType() { Index: openide/test/unit/src/org/openide/explorer/ExplorerUtilsTest.java =================================================================== RCS file: openide/test/unit/src/org/openide/explorer/ExplorerUtilsTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/test/unit/src/org/openide/explorer/ExplorerUtilsTest.java 10 Jun 2004 12:12:17 -0000 @@ -0,0 +1,87 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.explorer; + +import junit.textui.TestRunner; + +import org.netbeans.junit.*; +import org.openide.nodes.*; +import org.openide.util.HelpCtx; + +/** + * Check the behaviour of the ExplorerUtils + * + * @author Petr Nejedly + */ +public class ExplorerUtilsTest extends NbTestCase { + public ExplorerUtilsTest(String testName) { + super(testName); + } + + public static void main(String[] args) { + TestRunner.run(new NbTestSuite(ExplorerUtilsTest.class)); + System.exit(0); + } + + public void testGetHelpCtx () throws Exception { + HelpCtx DEF = new HelpCtx("default"); + + assertEquals("Use default help for no nodes", + DEF, + ExplorerUtils.getHelpCtx(new Node[0], DEF)); + + assertEquals("Use default help for single node w/o help", + DEF, + ExplorerUtils.getHelpCtx(new Node[] {new NoHelpNode()}, DEF)); + + assertEquals("Use provided help for single node with help", + new HelpCtx("foo"), + ExplorerUtils.getHelpCtx(new Node[] {new WithHelpNode("foo")}, DEF)); + + assertEquals("Use default help for more nodes w/o help", + DEF, + ExplorerUtils.getHelpCtx(new Node[] {new NoHelpNode(), new NoHelpNode()}, DEF)); + + assertEquals("Use provided help if only one node has help", + new HelpCtx("foo"), + ExplorerUtils.getHelpCtx(new Node[] {new NoHelpNode(), new WithHelpNode("foo")}, DEF)); + + assertEquals("Use provided help if more nodes have the same help", + new HelpCtx("foo"), + ExplorerUtils.getHelpCtx(new Node[] {new WithHelpNode("foo"), new WithHelpNode("foo")}, DEF)); + + assertEquals("Use default help if nodes have different helps", + DEF, + ExplorerUtils.getHelpCtx(new Node[] {new WithHelpNode("foo"), new WithHelpNode("bar")}, DEF)); + } + + + private static final class NoHelpNode extends AbstractNode { + public NoHelpNode() { + super(Children.LEAF); + } + } + + private static final class WithHelpNode extends AbstractNode { + private final String id; + public WithHelpNode(String id) { + super(Children.LEAF); + this.id = id; + } + public HelpCtx getHelpCtx() { + return new HelpCtx(id); + } + } + +}