KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > window > quickhelp > QuickHelpWindow


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.window.quickhelp;
20
21 import java.awt.*;
22 import java.io.*;
23 import java.net.*;
24 import java.util.*;
25 import java.util.List JavaDoc;
26
27 import javax.help.*;
28 import javax.swing.*;
29 import javax.swing.event.*;
30 import javax.xml.parsers.*;
31
32 import org.jaxen.*;
33 import org.jaxen.dom.*;
34 import org.w3c.dom.*;
35 import org.xml.sax.*;
36
37
38 /**
39
40  * @author Matthew Large
41  *
42  */

43 public class QuickHelpWindow extends JPanel implements Runnable JavaDoc, ListSelectionListener {
44
45     private JHelpContentViewer m_viewer = null;
46     
47     private JList m_tocList = null;
48     
49     private String JavaDoc m_sHelpRootPath = "/";
50     
51     public QuickHelpWindow() {
52         super();
53         this.setup();
54     }
55     
56     private void setup() {
57         this.setBackground(Color.WHITE);
58         this.setBorder(BorderFactory.createLineBorder(Color.black));
59
60         BorderLayout layout = new BorderLayout();
61         this.setLayout(layout);
62
63         try {
64             JHelp jhelp = null;
65             HelpSet hs = null;
66             try {
67                String JavaDoc helpHS = "Harmonise Information Manager.hs";
68                ClassLoader JavaDoc cl = QuickHelpWindow.class.getClassLoader();
69                 URL hsURL = HelpSet.findHelpSet(cl, helpHS);
70                 hs = new HelpSet(null, hsURL);
71                 hs.setHomeID("How_To___");
72                 jhelp = new JHelp(hs);
73             } catch (Exception JavaDoc ee) {
74             }
75             
76             this.m_viewer = jhelp.getContentViewer();
77             
78             try {
79                 InputStream is = QuickHelpWindow.class.getResourceAsStream(this.m_sHelpRootPath + "Harmonise Information ManagerTOC.xml");
80         
81                 if(is==null) {
82                     this.m_sHelpRootPath = "/helpfiles/";
83                     is = QuickHelpWindow.class.getResourceAsStream(this.m_sHelpRootPath + "Harmonise Information ManagerTOC.xml");
84                 }
85                 
86                 BufferedReader buff = new BufferedReader( new InputStreamReader(is));
87                 StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
88                 String JavaDoc sLine = null;
89                 
90                 while((sLine=buff.readLine())!=null) {
91                     sBuff.append(sLine);
92                 }
93             
94                 Document helpTOC = DocumentBuilderFactory
95                                         .newInstance()
96                                         .newDocumentBuilder()
97                                         .parse(new org.xml.sax.InputSource JavaDoc(
98                                                 new StringReader(sBuff.toString())));
99                 
100                 DOMXPath howToXPath = new DOMXPath("descendant::tocitem[@text='How To...']");
101                 Element howToEl = (Element) howToXPath.selectSingleNode(helpTOC.getDocumentElement());
102                 
103                 HelpListItem itemRoot = this.convert(howToEl, 0);
104                 
105                 List JavaDoc helpList = new ArrayList();
106                 this.addTOCtoList(itemRoot, helpList);
107
108                 this.m_tocList = new JList(helpList.toArray());
109                 this.m_tocList.addListSelectionListener(this);
110                 this.m_tocList.setCellRenderer(new HelpListCellRenderer());
111                 
112                 JScrollPane scroller = new JScrollPane(this.m_tocList);
113                 JPanel panel = new JPanel();
114                 panel.setLayout(new BorderLayout());
115                 panel.add(scroller);
116                 
117                 JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, jhelp.getContentViewer());
118                 this.add(split);
119             } catch (SAXException e) {
120                 e.printStackTrace();
121             } catch (IOException e) {
122                 e.printStackTrace();
123             } catch (ParserConfigurationException e) {
124                 e.printStackTrace();
125             } catch (FactoryConfigurationError e) {
126                 e.printStackTrace();
127             } catch (JaxenException e) {
128                 e.printStackTrace();
129             } catch (NullPointerException JavaDoc e) {
130                 e.printStackTrace();
131             }
132             
133         } catch (FactoryConfigurationError e) {
134             e.printStackTrace();
135         } catch (NullPointerException JavaDoc e) {
136             e.printStackTrace();
137         }
138     }
139     
140     private void addTOCtoList(HelpListItem item, List JavaDoc helpList) {
141         helpList.add(item);
142         Iterator itor = item.getChildren().iterator();
143         while (itor.hasNext()) {
144             HelpListItem element = (HelpListItem) itor.next();
145             this.addTOCtoList(element, helpList);
146         }
147     }
148     
149     private HelpListItem convert(Element el, int nDepth) {
150         String JavaDoc sTitle = el.getAttribute("text");
151         String JavaDoc sURL = el.getAttribute("target");
152         if(sURL.startsWith("gloss.")) {
153             sURL = sURL.replaceAll("gloss.", "");
154         }
155         sURL = this.m_sHelpRootPath + "HTML/" + sURL + ".htm";
156         
157         HelpListItem itemRetn = new HelpListItem(sTitle, sURL, nDepth);
158         
159         nDepth = nDepth+1;
160         NodeList nodes = el.getChildNodes();
161         for(int i=0; i<nodes.getLength(); i++) {
162             Node node = nodes.item(i);
163             if(node.getNodeType()==Node.ELEMENT_NODE) {
164                 Element elChild = (Element)node;
165                 itemRetn.addChild(this.convert(elChild, nDepth));
166             }
167         }
168         
169         return itemRetn;
170     }
171     
172     protected class HelpListItem {
173     
174         private String JavaDoc m_sTitle = null;
175         private String JavaDoc m_sURL = null;
176         
177         private int m_nDepth = 0;
178         
179         private List JavaDoc m_children = new ArrayList();
180         
181         public HelpListItem(String JavaDoc sTitle, String JavaDoc sURL, int nDepth) {
182             super();
183             this.m_sTitle = sTitle;
184             this.m_sURL = sURL;
185             this.m_nDepth = nDepth;
186         }
187         
188         public int getDepth() {
189             return this.m_nDepth;
190         }
191         
192         public String JavaDoc getTitle() {
193             return this.m_sTitle;
194         }
195         
196         public String JavaDoc getURL() {
197             return this.m_sURL;
198         }
199         
200         public void addChild(HelpListItem child) {
201             this.m_children.add(child);
202         }
203         
204         public List JavaDoc getChildren() {
205             return this.m_children;
206         }
207         
208         public String JavaDoc toString() {
209             StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc("Title: " + this.m_sTitle + "\n");
210             sBuff.append("URL: " + this.m_sURL + "\n");
211             sBuff.append("------------------------\n");
212             Iterator itor = this.m_children.iterator();
213             while (itor.hasNext()) {
214                 HelpListItem element = (HelpListItem) itor.next();
215                 sBuff.append(element.toString());
216             }
217             
218             sBuff.append("------------------------\n");
219             sBuff.append("========================\n");
220             
221             return sBuff.toString();
222         }
223         
224     }
225
226     /* (non-Javadoc)
227      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
228      */

229     public void valueChanged(ListSelectionEvent lse) {
230         try {
231             URL url = QuickHelpWindow.class.getResource(((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
232             this.m_viewer.setCurrentURL(url);
233         } catch(NullPointerException JavaDoc e) {
234             System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
235         } catch(IllegalArgumentException JavaDoc e) {
236             System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
237         }
238     }
239
240     /* (non-Javadoc)
241      * @see java.lang.Runnable#run()
242      */

243     public void run() {
244
245     }
246 }
247
Popular Tags