首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JavaHelp 2.0 打造專業級 Java 應用程式線上說明系統

     編輯:Java 相關知識

前言:為什麼需要 JavaHelp?

說明系統往往是開發最後才考慮的項目,但對使用者而言卻是學習軟體的關鍵資源。Java 平台內建 JavaHelp 2.0 API,提供標準化、全功能、可擴展的說明系統框架,支援目錄導航、全文搜尋、上下文敏感說明、嵌入式說明、多幫助集合併等企業級功能,讓開發者能以最小成本構建專業級線上說明。

核心架構:HelpSet 三大檔案類型

每個 JavaHelp 系統由一個 HelpSet 組成,包含三類檔案:

  • HelpSet 資料檔:.hs 主控制檔、.jhm 對應檔
  • 導航檔:TOC(目錄)、Index(索引)、Glossary(詞彙表)、Favorites(我的最愛)
  • 主題檔:HTML 格式內容頁面

關鍵檔案結構解析

1. helpset 檔(主控制檔)

<helpset version="2.0">
  <maps>
    <homeID>overview</homeID>
    <mapref location="TaxCalculatorMap.jhm"/>
  </maps>
  <view>
    <name>TOC</name> <label>目錄</label>
    <type>javax.help.TOCView</type>
    <data>TaxCalculatorTOC.xml</data>
    <image>toc_icon</image>
  </view>
  <view>... IndexView ...</view>
  <view>... GlossaryView ...</view>
  <view>... SearchView ...</view>
  <presentation default="true">
    <name>Main_Window</name> <title>Tax Calculator</title>
    <size width="640" height="480"/> <location x="0" y="0"/>
    <image>icon</image>
    <toolbar>
      <helpaction>javax.help.BackAction</helpaction>
      <helpaction image="addfav_icon">javax.help.FavoritesAction</helpaction>
    </toolbar>
  </presentation>
  <impl>
    <helpsetregistry helpbrokerclass="javax.help.DefaultHelpBroker"/>
    <viewerregistry viewertype="text/html" viewerclass="com.sun.java.help.impl.CustomKit"/>
    <viewerregistry viewertype="application/pdf" viewerclass="Your PDF Editor Kit"/>
  </impl>
</helpset>

四大區塊說明:

  • maps:指定對應檔與首頁 ID
  • view:定義導航視圖類型、資料來源、標籤圖示
  • presentation:視窗尺寸、標題、工具列按鈕(支援 8 種標準動作)
  • impl:註冊內容檢視器支援的檔案類型

2. map 檔(ID-URL 對應)

<map version="2.0">
  <mapID target="overview" url="TaxCalculator/overview.htm"/>
  <mapID target="icon" url="images/icon.gif"/>
  <mapID target="glossary_icon" url="images/g.gif"/>
  <mapID target="addfav_icon" url="images/addfav_icon.gif"/>
</map>

3. 導航檔範例

TOC(目錄):<tocitem text="Pages" image="topLevel"><tocitem text="About Page" target="about" image="secondLevel"/></tocitem>

Index(索引):<indexitem text="Color"><indexitem text="Changing Color" target="changeColor"/></indexitem>

Glossary(詞彙表):結構同 Index,提供術語定義導航。

在 Java 應用程式中啟動說明系統

ClassLoader loader = MyApp.class.getClassLoader();
URL url = HelpSet.findHelpSet(loader, "TaxCalculatorHelpSet");
HelpSet hs = new HelpSet(loader, url);
HelpBroker hb = hs.createHelpBroker("Main_Window");

// 方式一:按鈕直接綁定主題
hb.enableHelpOnButton(helpButton, "overview", hs);

// 方式二:動態顯示(支援上下文敏感)
ActionListener listener = new CSH.DisplayHelpFromSource(hb);
CSH.setHelpIDString(helpButton, "overview");
helpButton.addActionListener(listener);

進階客製化功能

視窗類型與切換

  • 主視窗:預設含工具列、導航窗格、內容區
  • 次視窗:可獨立調整大小、移動,預設僅含內容區
  • 彈出視窗:專為上下文敏感設計,失焦即關閉,不可調整大小

切換方式:hb.enableHelpOnButton(btn, "id", hs, "javax.help.SecondaryWindow", "main") 或在 HTML 主題檔嵌入 <object CLASSID="java:com.sun.java.help.impl.JHSecondaryViewer">...</object>。

外觀客製化

  • 圖示:在 helpset、view、導航檔中指定 image 屬性
  • 文字/圖示標籤切換:<presentation displayviewimages="false">
  • 工具列顯示/隱藏:移除 <toolbar> 區塊
  • 字體設定:hb.setFont(new Font("SansSerif", Font.ITALIC, 10))

全文搜尋與停用詞

  1. 切換至主題目錄:cd src/TaxCalculator
  2. 建立索引:%JAVAHELP_HOME%/javahelp/bin/jhindexer TaxCalculator
  3. 驗證:%JAVAHELP_HOME%/javahelp/bin/jhsearch JavaHelpSearch

自訂停用詞:建立 stopWords.txt(每行一詞)與 config.txt(StopWordsFile stopWords.txt),索引時指定 jhindexer -c config.txt TaxCalculator。

嵌入式說明

JHelpContentViewer viewer = new JHelpContentViewer(hs);
JHelpTOCNavigator nav = (JHelpTOCNavigator) hs.getNavigatorView("TOC").createNavigator(viewer.getModel());
JPanel panel = new JPanel(new BorderLayout());
panel.add(nav, BorderLayout.WEST);
panel.add(viewer, BorderLayout.CENTER);

多幫助集合併

四種合併策略:SortMerge(字母排序)、UniteAppendMerge(保留階層合併同名節點再附加)、AppendMerge(直接附加)、NoMerge(不合併)。靜態合併在 .hs 加入 <subhelpset location="..."/>;動態合併用 hs.add(hs1) / hs.remove(hs1)。

輕量級元件擴展

實作 ViewAwareComponent 介面,透過 <object CLASSID="java:YourClass"> 在主題檔嵌入自訂元件(如播放音效的圖示按鈕)。

伺服器端說明系統

基於 Tomcat + JSP 的 ServerHelp 架構,提供 TOC、Index、Search 三種視圖(無次視窗、彈出視窗、輕量級元件支援)。核心元件:

  • ServletHelpBroker:Session 級 Bean 管理說明狀態
  • JSP 標籤庫:<jh:validate>、<jh:navigators>、<jh:tocItem> 等
  • JavaScript:tree.js 建立導航樹、searchList.js 搜尋結果列表

部署步驟:將 jhall.jar 放入 WEB-INF/lib,複製 demo 檔案至 Web 應用目錄,設定 helpset 路徑即可。

結語

JavaHelp 2.0 以 XML 驅動的架構將內容、導航、呈現徹底分離,支援從桌面應用到 Web 應用的完整說明系統生命週期。掌握其核心概念(HelpSet、Map、View、Presentation)與擴展點(自訂視窗、輕量級元件、伺服器端部署),即可為任何 Java 應用程式快速建置專業、可維護的線上說明系統。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved