
package uk.co.wingpath.gui;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.util.*;
import java.beans.*;
import java.lang.reflect.*;
import uk.co.wingpath.util.*;
import uk.co.wingpath.event.Event;

/**
* Class for displaying help pages in an application.
* The actual displaying is delegated to an HtmlViewer.
*/
public class HelpViewer
{
    private Variable<Boolean> useBrowser;
    private JavaHtmlViewer javaViewer;
    private BrowserHtmlViewer browserViewer;
    private String errorPrefix;

    public HelpViewer (WWindow owner, String title,
        Variable<Boolean> useBrowser)
    {
        this.useBrowser = useBrowser;
        errorPrefix = "";

        ClassLoader loader = HelpViewer.class.getClassLoader ();
        try
        {
            // 'loader' should be a JarLoader, but we can't cast it to
            // a JarLoader since it was loaded by a different class loader!
            // So use reflection to call its 'getTempDir' method.
            Class<?> c = loader.getClass ();
            Method m = c.getMethod ("getTempDir", new Class [0]);
            Object result = m.invoke (loader, new Object [0]);
            File tempDir = (File) result;
            browserViewer = new BrowserHtmlViewer (tempDir);
        }
        catch (Exception e)
        {
            browserViewer = null;
        }
        javaViewer = new JavaHtmlViewer (owner, title);
    }

    /**
    * Makes the viewer's frame visible and displays help text..
    * @param id identifier of help text to be displayed
    */
    public void view (String id)
    {
        Event.checkIsEventDispatchThread ();
        String name;
        String pos;
        int i = id.indexOf ('#');
        if (i > 0)
        {
            name = id.substring (0, i);
            pos = id.substring (i + 1);
        }
        else
        {
            name = id;
            pos = null;
        }
        HtmlViewer viewer = javaViewer;
        if (useBrowser.getValue () && browserViewer != null)
            viewer = browserViewer;
        viewer.show ("doc/" + name + ".html", pos);
    }

    /**
    * Makes the viewer's frame visible and displays error help text in it.
    * @param id ID of the help to be displayed. The {@link #errorPrefix}
    * will be be prefixed to the ID before converting it to a URL.
    */
    public void viewError (String id)
    {
        Event.checkIsEventDispatchThread ();
        view (errorPrefix + id);
    }

    /**
    * Returns an {@code Action} that will display help with the specified ID in
    * the help window.
    * <p>The action will have the name "Help" and mnemonic Alt-H.
    * @param id ID of the help to be displayed.
    * @return an {@code Action} that will display the required help.
    */
    public Action getAction (final String id)
    {
        Action action = new AbstractAction ("Help")
            {
                public void actionPerformed (ActionEvent e)
                {
                    Event.checkIsEventDispatchThread ();
                    view (id);
                }
            };
        return action;
    }

    /**
    * Sets the prefix to be used with error help IDs.
    * @param errorPrefix the prefix for error help IDs.
    */
    public void setErrorPrefix (String errorPrefix)
    {
        this.errorPrefix = errorPrefix;
    }
}


