/*
 * HelpDialog.java
 *
 * Status: In progress
 */

package gui.dialog;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

import gui.ActionHandler;
import manual.Manual;

public class HelpDialog extends JFrame
 {
  private static class Linker implements HyperlinkListener
   {
    JEditorPane source = null;

    Linker(JEditorPane source)
     { this.source = source; }

    public void hyperlinkUpdate(HyperlinkEvent e)
     {
      if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
       {
        try
         { source.setPage(e.getURL()); }
        catch(IOException ignored)
         { }
       }
     }
   }


  /** The only instance of this class. */
  private static final HelpDialog instance = new HelpDialog();


  public static void showIndex()
   {
    instance.loadFile("index.html");
    instance.setVisible(true);
   }


  public static void showContents()
   {
    instance.loadFile("contents.html");
    instance.setVisible(true);
   }


  JEditorPane pane = new JEditorPane();


  HelpDialog()
   {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setTitle("Help for Double Entry Accounting Program");
    setSize(350, 400);
    setIconImage((new ImageIcon(gui.Main.class.getResource("icon.png"))).getImage());
    setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    c.weightx = 1;
    c.weighty = 0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.BOTH;
    JButton button = new JButton("Index");
    button.addActionListener(new ActionHandler(this, "button_Index_Handler"));
    add(button, c);

    c.gridwidth = GridBagConstraints.REMAINDER;
    button = new JButton("Contents");
    button.addActionListener(new ActionHandler(this, "button_Contents_Handler"));
    add(button, c);

    c.weighty = 1;
    add(new JScrollPane(pane), c);

    pane.setEditable(false);
    pane.addHyperlinkListener(new Linker(pane));
   }


  private void loadFile(String name)
   {
    try
     { instance.pane.setPage(Manual.class.getResource(name)); }
    catch(IOException ignored)
     { }
   }


  public void button_Index_Handler(ActionEvent event)
   {
    loadFile("index.html");
   }


  public void button_Contents_Handler(ActionEvent event)
   {
    loadFile("contents.html");
   }
 }