/*
 * HelpMenu.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class implements the help menu 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class HelpMenu extends JMenu implements ActionListener
{
  private JMenuItem aboutItem = new JMenuItem ("About");
  private JMenuItem helpItem = new JMenuItem ("Help");
  private JMenuItem configurationItem = new JMenuItem ("Configuration");
  private static final String ABOUT_TEXT = "docs/about.html";
  private static final String HELP_TEXT = "docs/help.html";
  private static final String CONFIGURATION_TEXT = "docs/configuration.html";
  private static final long ABOUT_INTERVAL = 5000;
  private static final String BOOK_ICON = "images/LDAP AddressBook.png";
  private JWindow aboutWindow;
  
  private class HtmlDisplayPane extends JEditorPane
  {
    public HtmlDisplayPane (URL text) throws IOException
    {
      super (text);
      setEditable (false);
    }
  }
  
  public HelpMenu ()
  {
    super ("Help");
    
    // Add help command    
    helpItem.addActionListener (this);
    add (helpItem);
    
    // Add configuration command    
    configurationItem.addActionListener (this);
    add (configurationItem);
    
    // Add about command    
    aboutItem.addActionListener (this);
    add (aboutItem);
    
  }
    
  public void actionPerformed (ActionEvent e)
  {
    try
    {
      if (e.getSource() == aboutItem)
      {
        // Display about screen
        aboutWindow = new JWindow();
        aboutWindow.setSize (300, 200);
        aboutWindow.getContentPane().add (new HtmlDisplayPane (getClass().getResource (ABOUT_TEXT)));
        aboutWindow.setVisible (true);
        
        // Set timer to remove about screen
        java.util.Timer timer = new java.util.Timer ();
        TimerTask task = new TimerTask ()
        {
          public void run ()
          {
            aboutWindow.dispose ();
          }
        };
        timer.schedule (task, ABOUT_INTERVAL);
      }
      else if (e.getSource() == helpItem)
      {
        // Display help file in scrollable window
        JFrame helpWindow = new JFrame("LDAP Address Book Help");
        helpWindow.setSize (400, 400);
        helpWindow.setIconImage ((new ImageIcon (getClass().getResource (BOOK_ICON))).getImage());  
        helpWindow.getContentPane().add (new JScrollPane (new HtmlDisplayPane (getClass().getResource (HELP_TEXT))));
        helpWindow.setVisible (true);
      }
      else if (e.getSource() == configurationItem)
      {
        // Display configuration file in scrollable window
        JFrame helpWindow = new JFrame("LDAP Address Book Configuration");
        helpWindow.setSize (400, 400);
        helpWindow.setIconImage ((new ImageIcon (getClass().getResource (BOOK_ICON))).getImage());  
        helpWindow.getContentPane().add (new JScrollPane (new HtmlDisplayPane (getClass().getResource (CONFIGURATION_TEXT))));
        helpWindow.setVisible (true);
      }
        
    }
    catch (Exception ex)
    {}
  }
}	