/*
*
* Copyright (C) 25- mar. 2003  Jean-Yves Beaujean 
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
* more details.
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,  59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.

* 
*/

package jxtray.gui;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Container;
import java.awt.AWTEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import java.net.*;

import javax.swing.text.*;

import jxtray.Jxtray;

import jxtray.swing.JxtButton;


public class HelpFrame extends JFrame implements ActionListener, HyperlinkListener
{
  private JEditorPane editpane = new JEditorPane();


  public HelpFrame()
  {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try  
		{  
        	init();
        	this.pack();
        	this.setSize(640,480);
        }
        catch(Exception e) { e.printStackTrace(); }
  }


  //Component initialization
  private void init() throws Exception 
  {

  	this.setIconImage((new ImageIcon(getClass().getResource("/images/icons/jxtray.png"))).getImage() );
    this.setTitle(Jxtray.getLang("dialog.helpframe.title"));

    try
    {
            URL url = null;
            try
            {
                url = getClass().getResource("/doc/manual_"+Jxtray.getProperty("lang").toLowerCase()+".html");
                editpane = new JEditorPane(url);
                editpane.setContentType("text/html");                
                editpane.setEditable(false);
                editpane.addHyperlinkListener(this);
            }
            catch (java.net.MalformedURLException exc)
            {
            	editpane.setText("MalformedURLException : " + exc.getMessage() );
                url = null;
            }
            catch (Exception exc)
            {
            	editpane.setText("Exception : " + exc.getMessage() );
                url = null;
            }
    }
    catch ( Exception e  )
    {
    	editpane.setText("Exception : " + e.getMessage() );
    }
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JxtButton(Jxtray.getLang("action.close"),this,Jxtray.getLang("action.close")),  BorderLayout.NORTH);
    this.getContentPane().add(new JScrollPane(editpane), BorderLayout.CENTER);
  }

  public void actionPerformed( ActionEvent e )
  {
      if ( e.getActionCommand().equals(Jxtray.getLang("action.close")))
            this.setVisible(false);
  }


  public void hyperlinkUpdate(HyperlinkEvent e)
  {
	      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
	                linkActivated(e.getURL());
  }

  protected void linkActivated(URL u)
  {
	        Cursor c = editpane.getCursor();
	        Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
	        editpane.setCursor(waitCursor);
	        SwingUtilities.invokeLater(new PageLoader(u, c));
   }


    class PageLoader implements Runnable
    {

            PageLoader(URL u, Cursor c)
            {
	            url = u;
	            cursor = c;
	          }

            public void run()
            {
	                if (url == null)
                  {
		                    editpane.setCursor(cursor);
		                    Container parent = editpane.getParent();
		                    parent.repaint();
	                }
                  else
                  {
                        Document doc = editpane.getDocument();
		         try
                        {
		                          editpane.setPage(url);
		        }
                        catch (Exception ioe)
                        {
		                          editpane.setDocument(doc);
		                          getToolkit().beep();
		                    }
                        finally
                        {
		                        url = null;
		                        SwingUtilities.invokeLater(this);
		                    }
	                  }
	            } // fin run

	    URL url;
	    Cursor cursor;
    }

}