/*
 * -------------------------------------------------------------------------
 *      $Id: Describe.java,v 1.8 2004/05/26 18:13:18 estewart 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.gallery;
import java.net.URL;
import java.util.ArrayList;
import javax.swing.event.HyperlinkEvent;

/**
 *
 * @author  brophy
 * @created February 6, 2002
 */
public class Describe extends javax.swing.JDialog {
    private ArrayList   listURL;
    private int         listURLindex;

    /** Creates new form Describe */
    public Describe(java.awt.Frame parent, String page) {
        super(parent, false);
        initComponents();
        listURL = new ArrayList(20);
        listURLindex = 0;
        setPage(page, true);
        setLocation(getToolkit().getScreenSize().width-getWidth(), 0);
        updateNavigationControls();
        pack();
        //javax.swing.JOptionPane.showMessageDialog(this,"ctor "+page);
    }

    public Describe(java.awt.Frame parent) {
        this(parent, "intro.html");
    }

    /**
     *  @param  name is the location of the page to set.
     *  @param  reset is true if the existing naviagtion stack is to be reset.
     */
    public void setPage(String name, boolean reset) {
        java.net.URL url = getClass().getResource(name);
        try {
            jEditorPane.setPage(url);
        } catch (Exception e) {
            try {
                jEditorPane.setPage(name);
            } catch (java.io.IOException e2) {
                javax.swing.JOptionPane.showMessageDialog(this,"Error loading page "+name+"  "+url);
            }
        } finally {
            if (reset)  listURL.clear();
            listURL.add(jEditorPane.getPage());
            listURLindex = listURL.size() - 1;
            updateNavigationControls();
        }
    }


    private void updateNavigationControls() {
        jButtonBack.setEnabled(listURLindex > 0);
        jButtonNext.setEnabled(listURLindex < listURL.size()-1);
        jPanelControl.setVisible(listURL.size() > 1);
        jPanelStatus.setVisible(listURL.size() > 1);
        URL url = jEditorPane.getPage();
        if (url != null)  jLabelStatus.setText(url.toExternalForm());
        repaint();
    }


    /** 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();
        jButtonNext = new javax.swing.JButton();
        jPanelStatus = new javax.swing.JPanel();
        jLabelStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JMSL Demo Description");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension((int)(getToolkit().getScreenSize().width-216),(int)(getToolkit().getScreenSize().height/6)));
        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));

        jPanelControl.setBackground(new java.awt.Color(0, 0, 50));
        jButtonBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/gallery/Back16.gif")));
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonBack);

        jButtonNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/gallery/Forward16.gif")));
        jButtonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNextActionPerformed(evt);
            }
        });

        jPanelControl.add(jButtonNext);

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

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

        jLabelStatus.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabelStatus.setForeground(java.awt.Color.blue);
        jLabelStatus.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jPanelStatus.add(jLabelStatus);

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

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

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        setVisible(false);
        dispose();
    }//GEN-LAST:event_formWindowClosing

    private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNextActionPerformed
        try {
            if (listURLindex+1 == listURL.size())  return;
            jEditorPane.setPage((URL)listURL.get(++listURLindex));
        } catch (Exception e) {
        } finally {
            updateNavigationControls();
        }
    }//GEN-LAST:event_jButtonNextActionPerformed

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        try {
            if (listURLindex == 0)  return;
            jEditorPane.setPage((URL)listURL.get(--listURLindex));
        } catch (Exception e) {
        } finally {
            updateNavigationControls();
        }
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jEditorPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPaneHyperlinkUpdate
        if (evt.getEventType() != HyperlinkEvent.EventType.ACTIVATED) return;
        try {
            for (int k = listURL.size()-1;  k > listURLindex;  k--)  listURL.remove(k);
            setPage(evt.getURL().toExternalForm(), false);
        } catch (Exception e) {
        } finally {
            updateNavigationControls();
        }
    }//GEN-LAST:event_jEditorPaneHyperlinkUpdate

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new Describe(new javax.swing.JFrame()).show();
    }


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