001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.window.quickhelp;
020:
021: import java.awt.*;
022: import java.io.*;
023: import java.net.*;
024: import java.util.*;
025: import java.util.List;
026:
027: import javax.help.*;
028: import javax.swing.*;
029: import javax.swing.event.*;
030: import javax.xml.parsers.*;
031:
032: import org.jaxen.*;
033: import org.jaxen.dom.*;
034: import org.w3c.dom.*;
035: import org.xml.sax.*;
036:
037: /**
038:
039: * @author Matthew Large
040: *
041: */
042: public class QuickHelpWindow extends JPanel implements Runnable,
043: ListSelectionListener {
044:
045: private JHelpContentViewer m_viewer = null;
046:
047: private JList m_tocList = null;
048:
049: private String m_sHelpRootPath = "/";
050:
051: public QuickHelpWindow() {
052: super ();
053: this .setup();
054: }
055:
056: private void setup() {
057: this .setBackground(Color.WHITE);
058: this .setBorder(BorderFactory.createLineBorder(Color.black));
059:
060: BorderLayout layout = new BorderLayout();
061: this .setLayout(layout);
062:
063: try {
064: JHelp jhelp = null;
065: HelpSet hs = null;
066: try {
067: String helpHS = "Harmonise Information Manager.hs";
068: ClassLoader cl = QuickHelpWindow.class.getClassLoader();
069: URL hsURL = HelpSet.findHelpSet(cl, helpHS);
070: hs = new HelpSet(null, hsURL);
071: hs.setHomeID("How_To___");
072: jhelp = new JHelp(hs);
073: } catch (Exception ee) {
074: }
075:
076: this .m_viewer = jhelp.getContentViewer();
077:
078: try {
079: InputStream is = QuickHelpWindow.class
080: .getResourceAsStream(this .m_sHelpRootPath
081: + "Harmonise Information ManagerTOC.xml");
082:
083: if (is == null) {
084: this .m_sHelpRootPath = "/helpfiles/";
085: is = QuickHelpWindow.class
086: .getResourceAsStream(this .m_sHelpRootPath
087: + "Harmonise Information ManagerTOC.xml");
088: }
089:
090: BufferedReader buff = new BufferedReader(
091: new InputStreamReader(is));
092: StringBuffer sBuff = new StringBuffer();
093: String sLine = null;
094:
095: while ((sLine = buff.readLine()) != null) {
096: sBuff.append(sLine);
097: }
098:
099: Document helpTOC = DocumentBuilderFactory.newInstance()
100: .newDocumentBuilder().parse(
101: new org.xml.sax.InputSource(
102: new StringReader(sBuff
103: .toString())));
104:
105: DOMXPath howToXPath = new DOMXPath(
106: "descendant::tocitem[@text='How To...']");
107: Element howToEl = (Element) howToXPath
108: .selectSingleNode(helpTOC.getDocumentElement());
109:
110: HelpListItem itemRoot = this .convert(howToEl, 0);
111:
112: List helpList = new ArrayList();
113: this .addTOCtoList(itemRoot, helpList);
114:
115: this .m_tocList = new JList(helpList.toArray());
116: this .m_tocList.addListSelectionListener(this );
117: this .m_tocList
118: .setCellRenderer(new HelpListCellRenderer());
119:
120: JScrollPane scroller = new JScrollPane(this .m_tocList);
121: JPanel panel = new JPanel();
122: panel.setLayout(new BorderLayout());
123: panel.add(scroller);
124:
125: JSplitPane split = new JSplitPane(
126: JSplitPane.HORIZONTAL_SPLIT, panel, jhelp
127: .getContentViewer());
128: this .add(split);
129: } catch (SAXException e) {
130: e.printStackTrace();
131: } catch (IOException e) {
132: e.printStackTrace();
133: } catch (ParserConfigurationException e) {
134: e.printStackTrace();
135: } catch (FactoryConfigurationError e) {
136: e.printStackTrace();
137: } catch (JaxenException e) {
138: e.printStackTrace();
139: } catch (NullPointerException e) {
140: e.printStackTrace();
141: }
142:
143: } catch (FactoryConfigurationError e) {
144: e.printStackTrace();
145: } catch (NullPointerException e) {
146: e.printStackTrace();
147: }
148: }
149:
150: private void addTOCtoList(HelpListItem item, List helpList) {
151: helpList.add(item);
152: Iterator itor = item.getChildren().iterator();
153: while (itor.hasNext()) {
154: HelpListItem element = (HelpListItem) itor.next();
155: this .addTOCtoList(element, helpList);
156: }
157: }
158:
159: private HelpListItem convert(Element el, int nDepth) {
160: String sTitle = el.getAttribute("text");
161: String sURL = el.getAttribute("target");
162: if (sURL.startsWith("gloss.")) {
163: sURL = sURL.replaceAll("gloss.", "");
164: }
165: sURL = this .m_sHelpRootPath + "HTML/" + sURL + ".htm";
166:
167: HelpListItem itemRetn = new HelpListItem(sTitle, sURL, nDepth);
168:
169: nDepth = nDepth + 1;
170: NodeList nodes = el.getChildNodes();
171: for (int i = 0; i < nodes.getLength(); i++) {
172: Node node = nodes.item(i);
173: if (node.getNodeType() == Node.ELEMENT_NODE) {
174: Element elChild = (Element) node;
175: itemRetn.addChild(this .convert(elChild, nDepth));
176: }
177: }
178:
179: return itemRetn;
180: }
181:
182: protected class HelpListItem {
183:
184: private String m_sTitle = null;
185: private String m_sURL = null;
186:
187: private int m_nDepth = 0;
188:
189: private List m_children = new ArrayList();
190:
191: public HelpListItem(String sTitle, String sURL, int nDepth) {
192: super ();
193: this .m_sTitle = sTitle;
194: this .m_sURL = sURL;
195: this .m_nDepth = nDepth;
196: }
197:
198: public int getDepth() {
199: return this .m_nDepth;
200: }
201:
202: public String getTitle() {
203: return this .m_sTitle;
204: }
205:
206: public String getURL() {
207: return this .m_sURL;
208: }
209:
210: public void addChild(HelpListItem child) {
211: this .m_children.add(child);
212: }
213:
214: public List getChildren() {
215: return this .m_children;
216: }
217:
218: public String toString() {
219: StringBuffer sBuff = new StringBuffer("Title: "
220: + this .m_sTitle + "\n");
221: sBuff.append("URL: " + this .m_sURL + "\n");
222: sBuff.append("------------------------\n");
223: Iterator itor = this .m_children.iterator();
224: while (itor.hasNext()) {
225: HelpListItem element = (HelpListItem) itor.next();
226: sBuff.append(element.toString());
227: }
228:
229: sBuff.append("------------------------\n");
230: sBuff.append("========================\n");
231:
232: return sBuff.toString();
233: }
234:
235: }
236:
237: /* (non-Javadoc)
238: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
239: */
240: public void valueChanged(ListSelectionEvent lse) {
241: try {
242: URL url = QuickHelpWindow.class
243: .getResource(((HelpListItem) this .m_tocList
244: .getSelectedValue()).getURL());
245: this .m_viewer.setCurrentURL(url);
246: } catch (NullPointerException e) {
247: System.out
248: .println("Name: "
249: + ((HelpListItem) this .m_tocList
250: .getSelectedValue()).getTitle()
251: + " URL:"
252: + ((HelpListItem) this .m_tocList
253: .getSelectedValue()).getURL());
254: } catch (IllegalArgumentException e) {
255: System.out
256: .println("Name: "
257: + ((HelpListItem) this .m_tocList
258: .getSelectedValue()).getTitle()
259: + " URL:"
260: + ((HelpListItem) this .m_tocList
261: .getSelectedValue()).getURL());
262: }
263: }
264:
265: /* (non-Javadoc)
266: * @see java.lang.Runnable#run()
267: */
268: public void run() {
269:
270: }
271: }
|