QuickHelpWindow.java :  » Content-Management-System » harmonise » org » openharmonise » him » window » quickhelp » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » him » window » quickhelp » QuickHelpWindow.java
/*
 * The contents of this file are subject to the 
 * Mozilla Public License Version 1.1 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * See the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Initial Developer of the Original Code is Simulacra Media Ltd.
 * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
 *
 * All Rights Reserved.
 *
 * Contributor(s):
 */
package org.openharmonise.him.window.quickhelp;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;

import javax.help.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.xml.parsers.*;

import org.jaxen.*;
import org.jaxen.dom.*;
import org.w3c.dom.*;
import org.xml.sax.*;


/**

 * @author Matthew Large
 *
 */
public class QuickHelpWindow extends JPanel implements Runnable, ListSelectionListener {

  private JHelpContentViewer m_viewer = null;
  
  private JList m_tocList = null;
  
  private String m_sHelpRootPath = "/";
  
  public QuickHelpWindow() {
    super();
    this.setup();
  }
  
  private void setup() {
    this.setBackground(Color.WHITE);
    this.setBorder(BorderFactory.createLineBorder(Color.black));

    BorderLayout layout = new BorderLayout();
    this.setLayout(layout);

    try {    
      JHelp jhelp = null;
      HelpSet hs = null;
      try {
         String helpHS = "Harmonise Information Manager.hs";
         ClassLoader cl = QuickHelpWindow.class.getClassLoader();
        URL hsURL = HelpSet.findHelpSet(cl, helpHS);
        hs = new HelpSet(null, hsURL);
        hs.setHomeID("How_To___");
        jhelp = new JHelp(hs);
      } catch (Exception ee) {
      }
      
      this.m_viewer = jhelp.getContentViewer();
      
      try {
        InputStream is = QuickHelpWindow.class.getResourceAsStream(this.m_sHelpRootPath + "Harmonise Information ManagerTOC.xml");
    
        if(is==null) {
          this.m_sHelpRootPath = "/helpfiles/";
          is = QuickHelpWindow.class.getResourceAsStream(this.m_sHelpRootPath + "Harmonise Information ManagerTOC.xml");
        }
        
        BufferedReader buff = new BufferedReader( new InputStreamReader(is));
        StringBuffer sBuff = new StringBuffer();
        String sLine = null;
        
        while((sLine=buff.readLine())!=null) {
          sBuff.append(sLine);
        }
      
        Document helpTOC = DocumentBuilderFactory
                    .newInstance()
                    .newDocumentBuilder()
                    .parse(new org.xml.sax.InputSource(
                        new StringReader(sBuff.toString())));
        
        DOMXPath howToXPath = new DOMXPath("descendant::tocitem[@text='How To...']");
        Element howToEl = (Element) howToXPath.selectSingleNode(helpTOC.getDocumentElement());
        
        HelpListItem itemRoot = this.convert(howToEl, 0);
        
        List helpList = new ArrayList();
        this.addTOCtoList(itemRoot, helpList);

        this.m_tocList = new JList(helpList.toArray());
        this.m_tocList.addListSelectionListener(this);
        this.m_tocList.setCellRenderer(new HelpListCellRenderer());
        
        JScrollPane scroller = new JScrollPane(this.m_tocList);
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scroller);
        
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, jhelp.getContentViewer());
        this.add(split);
      } catch (SAXException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (ParserConfigurationException e) {
        e.printStackTrace();
      } catch (FactoryConfigurationError e) {
        e.printStackTrace();
      } catch (JaxenException e) {
        e.printStackTrace();
      } catch (NullPointerException e) {
        e.printStackTrace();
      }
      
    } catch (FactoryConfigurationError e) {
      e.printStackTrace();
    } catch (NullPointerException e) {
      e.printStackTrace();
    }
  }
  
  private void addTOCtoList(HelpListItem item, List helpList) {
    helpList.add(item);
    Iterator itor = item.getChildren().iterator();
    while (itor.hasNext()) {
      HelpListItem element = (HelpListItem) itor.next();
      this.addTOCtoList(element, helpList);
    }
  }
  
  private HelpListItem convert(Element el, int nDepth) {
    String sTitle = el.getAttribute("text");
    String sURL = el.getAttribute("target");
    if(sURL.startsWith("gloss.")) {
      sURL = sURL.replaceAll("gloss.", "");
    }
    sURL = this.m_sHelpRootPath + "HTML/" + sURL + ".htm";
    
    HelpListItem itemRetn = new HelpListItem(sTitle, sURL, nDepth);
    
    nDepth = nDepth+1;
    NodeList nodes = el.getChildNodes();
    for(int i=0; i<nodes.getLength(); i++) {
      Node node = nodes.item(i);
      if(node.getNodeType()==Node.ELEMENT_NODE) {
        Element elChild = (Element)node;
        itemRetn.addChild(this.convert(elChild, nDepth));
      }
    }
    
    return itemRetn;
  }
  
  protected class HelpListItem {
  
    private String m_sTitle = null;
    private String m_sURL = null;
    
    private int m_nDepth = 0;
    
    private List m_children = new ArrayList();
    
    public HelpListItem(String sTitle, String sURL, int nDepth) {
      super();
      this.m_sTitle = sTitle;
      this.m_sURL = sURL;
      this.m_nDepth = nDepth;
    }
    
    public int getDepth() {
      return this.m_nDepth;
    }
    
    public String getTitle() {
      return this.m_sTitle;
    }
    
    public String getURL() {
      return this.m_sURL;
    }
    
    public void addChild(HelpListItem child) {
      this.m_children.add(child);
    }
    
    public List getChildren() {
      return this.m_children;
    }
    
    public String toString() {
      StringBuffer sBuff = new StringBuffer("Title: " + this.m_sTitle + "\n");
      sBuff.append("URL: " + this.m_sURL + "\n");
      sBuff.append("------------------------\n");
      Iterator itor = this.m_children.iterator();
      while (itor.hasNext()) {
        HelpListItem element = (HelpListItem) itor.next();
        sBuff.append(element.toString());
      }
      
      sBuff.append("------------------------\n");
      sBuff.append("========================\n");
      
      return sBuff.toString();
    }
    
  }

  /* (non-Javadoc)
   * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
   */
  public void valueChanged(ListSelectionEvent lse) {
    try {
      URL url = QuickHelpWindow.class.getResource(((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
      this.m_viewer.setCurrentURL(url);
    } catch(NullPointerException e) {
      System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
    } catch(IllegalArgumentException e) {
      System.out.println("Name: " + ((HelpListItem)this.m_tocList.getSelectedValue()).getTitle() + " URL:" + ((HelpListItem)this.m_tocList.getSelectedValue()).getURL());
    }
  }

  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {

  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.