Changeset 5521


Ignore:
Timestamp:
2003-09-19T13:47:47+12:00 (21 years ago)
Author:
mdewsnip
Message:

Rewrote to use the new XML help file and construct the contents automatically.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/help/HelpFrame.java

    r5304 r5521  
    88 * <BR><BR>
    99 *
    10  * Author: John Thompson, Greenstone Digital Library, University of Waikato
     10 * Principal Author: John Thompson, NZDL Project, University of Waikato
    1111 *
    1212 * <BR><BR>
     
    3636 */
    3737
    38  
    39 
    40 
    41 
    42 
    4338package org.greenstone.gatherer.help;
     39
    4440/**
    45  * Title:        The Gatherer<br>
    46  * Description:  The Gatherer: a tool for gathering and enriching digital collections.<br>
    47  * Copyright:    Copyright (c) 2001<br>
    48  * Company:      The University of Waikato<br>
    49  * @author John Thompson, Greenstone Digital Libraries
    50  * @version 2.1
     41 * @author Michael Dewsnip, NZDL Project
    5142 */
    5243import calpa.html.*;
    53 import java.awt.*;
     44import java.awt.BorderLayout;
     45import java.awt.Dimension;
     46import java.awt.Toolkit;
    5447import java.io.File;
    5548import java.net.URL;
    56 import java.util.StringTokenizer;
    57 import javax.swing.BorderFactory;
     49import java.util.Enumeration;
    5850import javax.swing.JFrame;
    5951import javax.swing.JPanel;
    6052import javax.swing.JScrollPane;
    6153import javax.swing.JSplitPane;
    62 import javax.swing.JTabbedPane;
    6354import javax.swing.JTree;
    6455import javax.swing.event.TreeSelectionEvent;
    6556import javax.swing.event.TreeSelectionListener;
     57import javax.swing.tree.DefaultTreeModel;
     58import javax.swing.tree.DefaultMutableTreeNode;
     59import javax.swing.tree.MutableTreeNode;
    6660import javax.swing.tree.TreePath;
    67 import org.greenstone.gatherer.help.ContentModel;
    68 import org.greenstone.gatherer.help.HelpItem;
    69 /** This class provides a nice help facility for the Gatherer tool. It
    70  * is a separate frame that can be positioned as the user wishes, and provides
    71  * standard help-type functionality such as contents, index and search,
    72  * glossary and of course help descriptions. <BR>
     61import org.apache.xerces.parsers.DOMParser;
     62import org.greenstone.gatherer.util.Utility;
     63import org.w3c.dom.*;
     64
     65
     66/**
     67 * This class provides a nice help facility. It is a separate frame that can be positioned
     68 * as the user wishes, and provides a contents page and the help information.
    7369 */
    7470public class HelpFrame
    7571    extends JFrame {
    76     /** The html rendering pane. */
     72
     73    /** The HTML rendering pane */
    7774    private CalHTMLPane view = null;
    78     private ContentModel model = null;
    79      
     75    /** The contents tree model */
     76    private ContentsModel model = null;
     77    /** A contents tree for the top of the frame */
     78    private JTree contents = null;
     79
    8080    private JSplitPane split_pane = null;
    81     /** A contents tree for the left of the frame. */
    82     private JTree contents = null;
    83 
    84     private Observer observer = null;
    85 
    86     /** The size of a button on this pane. */
    87     static final Dimension BUTTON_SIZE = new Dimension(100,50);
    88     /** The minimum size of any component in this frame. */
    89     static final Dimension MIN_SIZE = new Dimension(100,100);
    90     /** The size of the frame itself. */
    91     static final Dimension SIZE = new Dimension(760,560);
    92     /** Constructor.
    93      * @param file_name The file name, as a <Strong>String</strong> of the help page to initially show.
    94       */
    95     public HelpFrame() {
    96     setDefaultCloseOperation(HIDE_ON_CLOSE);
     81
     82    /** The size of the frame */
     83    static final Dimension SIZE = new Dimension(760, 560);
     84
     85
     86    public HelpFrame()
     87    {
     88    setDefaultCloseOperation(HIDE_ON_CLOSE);
    9789    setSize(SIZE);
    98     model = new ContentModel();
    99     contents = new JTree(model);
     90
     91    view = new CalHTMLPane(new CalHTMLPreferences(), new Observer(), "Help Pages");
     92
     93    HelpItem rootNode = new HelpItem("Contents", "<null>");
     94    model = new ContentsModel(rootNode);
     95    contents = new JTree((DefaultTreeModel) model);
    10096    contents.addTreeSelectionListener(new ContentsListener());
    10197    contents.setExpandsSelectedPaths(true);
    102     CalHTMLPreferences prefs = new CalHTMLPreferences();
    103     observer = new Observer();
    104     view = new CalHTMLPane(prefs, observer, "Help Pages");
    10598
    10699    // Creation
     
    109102    JScrollPane index_scroll = new JScrollPane(contents);
    110103    JScrollPane view_scroll = new JScrollPane(view);
     104
    111105    // Layout
    112106    split_pane.add(index_scroll, JSplitPane.LEFT);
     
    114108    content_pane.setLayout(new BorderLayout());
    115109    content_pane.add(split_pane, BorderLayout.CENTER);
     110
    116111    // Center
    117112    Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
    118113    setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);       
    119114    }
    120     /** Destructor. Removes all listeners, closes streams and explicitly sets data members to null. */
    121     public void destroy() {
     115
     116
     117    public void destroy()
     118    {
    122119    view = null;
    123120    contents = null;
    124121    }
    125     public void setView(String index) {
    126     try {
    127                 // Retrieve the node at index
    128         HelpItem current = (HelpItem) model.getRoot();
    129         StringTokenizer tokenizer = new StringTokenizer(index, ".");
    130         while(tokenizer.hasMoreTokens()) {
    131         String token = tokenizer.nextToken();
    132         int position = Integer.parseInt(token);
    133         if(position > 0 && position <= current.getChildCount()) {
    134             current = (HelpItem) current.getChildAt(position - 1);
    135         }
    136         token = null;
    137         }
    138         tokenizer = null;
    139                 // Ensure path is selected
    140         TreePath path = new TreePath(current.getPath());
    141         contents.setSelectionPath(path);
    142         contents.scrollPathToVisible(path);
    143         path = null;
    144                 // Build url
    145         URL url = current.getURL();
    146         current = null;
    147                 // Show help dialog
    148         show();
    149         split_pane.setDividerLocation(0.2);
    150                 // Display help page
    151         if(url != null) {
    152         view.showHTMLDocument(url);
    153         }
    154         url = null;
    155     }
    156     catch (Exception error) {
    157         show();
    158         split_pane.setDividerLocation(0.2);
    159     }
    160     }
    161 
    162     /** This listener is used to lister for selection changes in the contents tree, and to show the appropriate page as required.
    163       */
    164     private class ContentsListener
     122
     123
     124    public void setView(String sectionName)
     125    {
     126    // Find the node in the tree with the specified sectionName
     127    HelpItem node = model.getHelpItemNamed(sectionName);
     128
     129    // Ensure the node is selected and visible
     130    TreePath path = new TreePath(node.getPath());
     131    contents.setSelectionPath(path);
     132    contents.scrollPathToVisible(path);
     133
     134    // Display the selected page
     135    URL url = node.getURL();
     136    if (url != null) {
     137        view.showHTMLDocument(url);
     138    }
     139
     140    // Make the help frame visible
     141    show();
     142    split_pane.setDividerLocation(0.2);
     143    }
     144
     145
     146    private class ContentsModel
     147    extends DefaultTreeModel {
     148
     149    public ContentsModel(MutableTreeNode rootNode) {
     150        super(rootNode);
     151
     152        // Load the XML help file and build the contents structure from it
     153        try {
     154        DOMParser parser = new DOMParser();
     155        parser.parse(Utility.HELP_DIR + "help.xml");
     156        Document document = parser.getDocument();
     157
     158        // Traverse the document hierarchy, building a tree representing its structure
     159        Node documentNode = document.getFirstChild();
     160        NodeList sections = documentNode.getChildNodes();
     161        for (int i = 0; i < sections.getLength(); i++) {
     162            Node section = sections.item(i);
     163            buildContentsHierarchy(rootNode, section);
     164        }
     165        }
     166        catch (Exception ex) {
     167        ex.printStackTrace();
     168        }
     169    }
     170
     171
     172    public void buildContentsHierarchy(MutableTreeNode parent, Node node)
     173    {
     174        // We're only interested in the Section nodes
     175        if (!node.getNodeName().equals("Section")) {
     176        return;
     177        }
     178
     179        // Determine the section name
     180        String sectionName = ((Element) node).getAttribute("name");
     181
     182        // Determine the section title
     183        String sectionTitle = "";
     184        NodeList children = node.getChildNodes();
     185        for (int i = 0; i < children.getLength(); i++) {
     186        Node child = children.item(i);
     187        if (child.getNodeName().equals("Title")) {
     188            sectionTitle = child.getFirstChild().getNodeValue();
     189        }
     190        }
     191
     192        // Add the node into the tree
     193        HelpItem item = new HelpItem(sectionTitle, sectionName);
     194        insertNodeInto(item, parent, parent.getChildCount());
     195
     196        // Apply recursively to the children of this node
     197        for (int i = 0; i < children.getLength(); i++) {
     198        Node child = children.item(i);
     199        buildContentsHierarchy(item, child);
     200        }
     201    }
     202
     203
     204    public HelpItem getHelpItemNamed(String sectionName)
     205    {
     206        // Find the node with name matching sectionName, somewhere in the tree
     207        Enumeration descendants = ((DefaultMutableTreeNode) root).preorderEnumeration();
     208        while (descendants.hasMoreElements()) {
     209        HelpItem node = (HelpItem) descendants.nextElement();
     210        if (node.name.equals(sectionName)) {
     211            return node;
     212        }
     213        }
     214
     215        // Couldn't be found, so just return the root (Contents) node
     216        return (HelpItem) root;
     217    }
     218    }
     219
     220
     221    /** This listener is used to listen for selection changes in the contents tree, and
     222     * to show the appropriate page as required.
     223     */
     224    private class ContentsListener
    165225    implements TreeSelectionListener {
     226
    166227    /** Any implementation of <i>TreeSelectionListener</i> must include this method so we can be informed when the tree selection changes.
    167228     * @param event A <strong>TreeSelectionEvent</strong> containing al the relevant information about the event itself.
    168             */
    169     public void valueChanged(TreeSelectionEvent event) {
     229     */
     230    public void valueChanged(TreeSelectionEvent event)
     231    {
    170232        TreePath path = event.getPath();
    171         HelpItem node = (HelpItem)path.getLastPathComponent();
     233        HelpItem node = (HelpItem) path.getLastPathComponent();
    172234        URL url = node.getURL();
    173         if(url != null) {
     235        if (url != null) {
    174236        view.showHTMLDocument(url);
    175237        }
     
    177239    }
    178240
     241
     242    /** This class provides a wrapper around a <strong>DefaultMutableTreeNode</strong> which
     243     * provides the ability to set an html page to be loaded when this node is selected.
     244     */
     245    private class HelpItem
     246    extends DefaultMutableTreeNode {
     247
     248    /** The unique name of the section (matches the HTML filename) */
     249    public String name = null;
     250    /** The title to be displayed for this tree node. */
     251    public String title = null;
     252
     253
     254    /** Constructor.
     255     * @param title The title to be shown for this node as a <strong>String</strong>.
     256     * @param name The name of the section as a <strong>String</strong>.
     257     */
     258    public HelpItem(String title, String name)
     259    {
     260        this.name = name;
     261        this.title = title;
     262    }
     263
     264
     265    /** Method to create a url from the file name.
     266     * @return A <strong>URL</strong> that points to the file's location.
     267     */
     268    public URL getURL()
     269    {
     270        if (name != null && !name.equals("<null>")) {
     271        try {
     272            File file = new File(Utility.HELP_DIR + name + ".htm");
     273            return file.toURL();
     274        }
     275        catch (Exception e) {
     276        }
     277        }
     278
     279        return null;
     280    }
     281
     282
     283    public String toString()
     284    {
     285        return title;
     286    }
     287    }
     288
     289
     290    /**
     291     * @author John Thompson
     292     */
    179293    private class Observer
    180294    extends DefaultCalHTMLObserver {
    181     public int state = CalCons.DOC_LOADED;
     295
     296    private int state = CalCons.DOC_LOADED;
     297
    182298    public void linkActivatedUpdate(CalHTMLPane pane, URL url, String target_frame, String j_name) {
    183         System.err.println("Link clicked: " + url);
     299        // System.err.println("Link clicked: " + url);
    184300    }
    185301
     
    190306        this.state = state;
    191307        switch(state) {
    192         case CalCons.PRE_CONNECT:
    193         break;
    194         case CalCons.PARSE_FAILED:
    195         break;
    196         case CalCons.CONNECTED:
    197         break;
    198         case CalCons.DOC_LENGTH:
    199         break;
    200         case CalCons.TITLE:
    201         break;
    202         case CalCons.PARSE_FAILED_POST_CONNECT:
    203         break;
    204         case CalCons.WAITING_FOR_IMAGES:
    205         break;
    206         case CalCons.DOC_LOADED:
    207         break;
     308            case CalCons.PRE_CONNECT:
     309            break;
     310            case CalCons.PARSE_FAILED:
     311            break;
     312            case CalCons.CONNECTED:
     313            break;
     314            case CalCons.DOC_LENGTH:
     315            break;
     316            case CalCons.TITLE:
     317            break;
     318            case CalCons.PARSE_FAILED_POST_CONNECT:
     319            break;
     320            case CalCons.WAITING_FOR_IMAGES:
     321            break;
     322            case CalCons.DOC_LOADED:
     323            break;
    208324        }
    209325    }
Note: See TracChangeset for help on using the changeset viewer.