/*
 * -------------------------------------------------------------------------
 *      $Id: Browser.java,v 1.3 2003/04/04 14:10:51 trudisch Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.WallStreet;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.JEditorPane;
import javax.swing.event.*;
import javax.swing.text.html.*;

/**
 *
 * @author  brophy
 * @created October 5, 2001
 */
public class Browser extends javax.swing.JFrame {
    private ArrayList   listURL;
    private int         listURLindex;


    /** Creates new form Browser */
    public Browser(URL url) throws java.io.IOException {
        initComponents();
        listURL = new ArrayList(20);
        listURL.add(url);
        listURLindex = 0;
        jump();
    }


    private void jump() {
        try {
            URL url = (URL)listURL.get(listURLindex);
            jEditorPane.setPage(url);
            jLabelStatus.setText(url.toExternalForm());
        } catch (java.io.IOException e) {
            jLabelStatus.setText(e.toString());
        } finally {
            jButtonForward.setEnabled(listURLindex+1 < listURL.size());
            jButtonBack.setEnabled(listURLindex > 0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane = new javax.swing.JEditorPane();
        jPanelControl = new javax.swing.JPanel();
        jButtonBack = new javax.swing.JButton();
        jButtonForward = new javax.swing.JButton();
        jButtonReload = new javax.swing.JButton();
        jButtonHome = new javax.swing.JButton();
        jLabelStatus = new javax.swing.JLabel();

        jScrollPane1.setPreferredSize(new java.awt.Dimension(700, 900));
        jEditorPane.setEditable(false);
        jEditorPane.setContentType("text/html");
        jEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                jEditorPaneHyperlinkUpdate(evt);
            }
        });

        jScrollPane1.setViewportView(jEditorPane);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanelControl.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jButtonBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/test/chart/harness/Back16.gif")));
        jButtonBack.setToolTipText("Back");
        jButtonBack.setEnabled(false);
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonBack);

        jButtonForward.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/test/chart/harness/Forward16.gif")));
        jButtonForward.setToolTipText("Forward");
        jButtonForward.setEnabled(false);
        jButtonForward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonForwardActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonForward);

        jButtonReload.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/test/chart/harness/Refresh16.gif")));
        jButtonReload.setToolTipText("Reload");
        jButtonReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonReloadActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonReload);

        jButtonHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/test/chart/harness/Home16.gif")));
        jButtonHome.setToolTipText("Home");
        jButtonHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonHomeActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonHome);

        getContentPane().add(jPanelControl, java.awt.BorderLayout.NORTH);

        getContentPane().add(jLabelStatus, java.awt.BorderLayout.SOUTH);

        pack();
    }//GEN-END:initComponents

    private void jButtonReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReloadActionPerformed
        jump();
    }//GEN-LAST:event_jButtonReloadActionPerformed

    private void jButtonHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonHomeActionPerformed
        listURLindex = 0;
        jump();
    }//GEN-LAST:event_jButtonHomeActionPerformed

    private void jButtonForwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonForwardActionPerformed
        ++listURLindex;
        jump();
    }//GEN-LAST:event_jButtonForwardActionPerformed

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        --listURLindex;
        jump();
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jEditorPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPaneHyperlinkUpdate
        if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            JEditorPane pane = (JEditorPane) evt.getSource();
            if (evt instanceof HTMLFrameHyperlinkEvent) {
                HTMLFrameHyperlinkEvent  hevt = (HTMLFrameHyperlinkEvent)evt;
                HTMLDocument doc = (HTMLDocument)pane.getDocument();
                doc.processHTMLFrameHyperlinkEvent(hevt);
            } else {
                try {
                    URL url = evt.getURL();
                    for (int k = listURL.size()-1;  k > listURLindex;  k--)  listURL.remove(k);
                    ++listURLindex;
                    listURL.add(url);
                    jump();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_jEditorPaneHyperlinkUpdate

    /** Exit the Application */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JEditorPane jEditorPane;
    private javax.swing.JPanel jPanelControl;
    private javax.swing.JButton jButtonHome;
    private javax.swing.JButton jButtonBack;
    private javax.swing.JButton jButtonForward;
    private javax.swing.JButton jButtonReload;
    // End of variables declaration//GEN-END:variables


    static public void main(String arg[]) throws Exception {
        Browser hf = new Browser(new URL(arg[0]));
        hf.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                System.exit(0);
            }
        });
        hf.show();
    }
}