2

当我使用 时HelpBroker.setCurrentID("[some help id]"),JavaHelp 会正确显示我想要的页面,但关联的主题不会在目录中自动选择/突出显示。这使得用户很难知道当前页面在主题树中的什么位置。

当我使用 HelpBroker 导航到特定的帮助页面时,有什么方法可以以编程方式突出显示 TOC 中的当前主题?

请注意,当用户点击 Java 帮助系统中的链接时,会在目录中正确选择新主题。

4

2 回答 2

1

您应该可以只调用该navigate()函数,其余的都是助手。id当然是 id,并且nodeLabel是您要选择的节点上的可见文本。请注意,此代码不会检查错误,即防止尝试去不存在的地方。

import java.awt.Component;
import java.util.Enumeration;
import javax.help.*;
import javax.help.plaf.basic.BasicTOCNavigatorUI;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.DefaultMutableTreeNode;

public class Nav
{
    private static DefaultMutableTreeNode match = null;

    private Nav()
    {
    }

    public static void navigate(HelpSet set, HelpBroker broker, String id, String nodeLabel)
    {
        broker.setCurrentID(id);
        JHelp jhelp = new JHelp(set);
        JHelpNavigator nav = getNavigator(jhelp);
        JTree tree = getTree(nav);
        TreeModel model = tree.getModel();
        Object root = model.getRoot();
        match = null;
        findNode(model, root, nodeLabel);
        TreePath path = new TreePath(match.getPath());
        tree.setSelectionPath(path);
        tree.scrollPathToVisible(path);
    }

    //http://www.google.com/codesearch/p?hl=en#WiboLAWeTd0/xena/ext/src/javahelp/jhMaster/JavaHelp/src/new/javax/help/WindowPresentation.java&t=0&d=30&l=272
    private static JHelpNavigator getNavigator(JHelp jhelp)
    {
        JHelpNavigator nav = null;
        for (Enumeration e = jhelp.getHelpNavigators(); e.hasMoreElements(); ) 
        {
            nav = (JHelpNavigator) e.nextElement();
            if (nav.getNavigatorName().equals("TOC"))
            {
                return nav;
            }
        }
        return null;
    }

    //http://forums.sun.com/thread.jspa?threadID=350180#1459484
    private static JTree getTree(JHelpNavigator nav)
    {
        JTree tree = null;
        Component[] components = nav.getComponents();
        int count = components.length;
        int i = 0;
        while(i < count && !(components[i] instanceof JScrollPane))
        {
            i++;
        }
        if(i < count)
        {
            JScrollPane sp = (JScrollPane) components[i];
            components = sp.getViewport().getComponents();
            count = components.length;
            i = 0;
            while(i < count && !(components[i] instanceof JTree))
            {
                i++;
            }
            tree = (JTree) components[i];
        }
        return tree;
    }

    //http://www.rkcole.com/articles/swing/TraverseJtree-2000-11-17.html
    private static void findNode(TreeModel model, Object o, String search)
    {
        if(match != null)
            return;

        int count = model.getChildCount(o);
        for( int i=0;  i < count;  i++ )
        {
            Object child = model.getChild(o, i );
            if (  model.isLeaf( child ) )
            {
                if(search.equals(((TOCItem)((DefaultMutableTreeNode)child).getUserObject()).getName()))
                {
                    match = (DefaultMutableTreeNode)child;
                    return;
                }
            }
            else
                findNode( model, child, search );
        }
    }
}
于 2010-03-27T03:57:05.123 回答
1

您是否使用合并的帮助集?如果我使用 setCurrentID("topic") 并且主题不是主帮助集而是在子帮助集中,我会得到你看到的行为。你想做的是

setCurrentID(javax.help.Map.ID.create("topic",subhelpset))

对我来说,这使 TOC 视图选择正确发生。当然,这可能意味着您需要使用动态合并的帮助集,因为如果在主帮助集的 .hs 文件中指定了子帮助集,我看不到(直接)找到子帮助集的简单方法。

Similarly, for context sensitive help, you need to set both the ID and the helpset on each Component.

于 2011-06-01T17:46:31.137 回答