1 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 ; 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 43 public class QuickHelpWindow extends JPanel implements Runnable , ListSelectionListener { 44 45 private JHelpContentViewer m_viewer = null; 46 47 private JList m_tocList = null; 48 49 private String 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 helpHS = "Harmonise Information Manager.hs"; 68 ClassLoader 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 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 sBuff = new StringBuffer (); 88 String 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 ( 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 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 e) { 130 e.printStackTrace(); 131 } 132 133 } catch (FactoryConfigurationError e) { 134 e.printStackTrace(); 135 } catch (NullPointerException e) { 136 e.printStackTrace(); 137 } 138 } 139 140 private void addTOCtoList(HelpListItem item, List 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 sTitle = el.getAttribute("text"); 151 String 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 m_sTitle = null; 175 private String m_sURL = null; 176 177 private int m_nDepth = 0; 178 179 private List m_children = new ArrayList(); 180 181 public HelpListItem(String sTitle, String 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 getTitle() { 193 return this.m_sTitle; 194 } 195 196 public String 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 getChildren() { 205 return this.m_children; 206 } 207 208 public String toString() { 209 StringBuffer sBuff = new StringBuffer ("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 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 e) { 234 System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL()); 235 } catch(IllegalArgumentException e) { 236 System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL()); 237 } 238 } 239 240 243 public void run() { 244 245 } 246 } 247 | Popular Tags |