/*
 * -------------------------------------------------------------------------
 *      $Id: Driver.java,v 1.9 2003/09/17 22:02:33 brophy 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.risk;
import com.imsl.stat.Covariances;
import java.awt.Dimension;

/**
 *
 * @author  brophy
 * @created February 1, 2002
 */
public class Driver extends javax.swing.JFrame {
    static private final int STEP_CHART_INTRO = 0;
    static private final int STEP_CHART_SERIES = 1;
    static private final int STEP_CHART_RETURN = 2;
    static private final int STEP_CHART_SIMULATE = 3;

    private Database    db;
    private int         step;
    private ChartSeries chartSeries;
    private ChartReturns chartReturns;
    private Simulate    simulate;
    private Dimension   screenSize;
    private boolean     exitOnClose ;

    /** Creates new form Driver */
    public Driver(boolean exitOnClose) throws Exception {
        this.exitOnClose = exitOnClose;
        initComponents();

        screenSize = getToolkit().getScreenSize();
        int w = Math.min(screenSize.width/2,600);
        //jPanelEditor.setPreferredSize(new Dimension(w, 175));
        //jScrolPanel.setPreferredSize(new Dimension(w, 175));
        setLocation(screenSize.width-w,0);
        pack();

        db = new Database();
        step = STEP_CHART_INTRO;
        update();
    }

    public void update() {
        try {
            jButtonBack.setEnabled(step > STEP_CHART_INTRO);
            jButtonNext.setEnabled(step < STEP_CHART_SIMULATE);

            if (chartSeries != null)  chartSeries.setVisible(false);
            if (chartReturns != null)  chartReturns.setVisible(false);
            if (simulate != null)  simulate.setVisible(false);

            Dimension size = getSize();
            switch (step) {
                case STEP_CHART_INTRO:
                    jEditorPaneDescribe.setPage(url("intro.html"));
                    break;
                case STEP_CHART_SERIES:
                    jEditorPaneDescribe.setPage(url("chartSeries.html"));
                    if (chartSeries == null) {
                        chartSeries = new ChartSeries(db);
                        int h = Math.min(screenSize.width/2, screenSize.height-size.height-32);
                        int w = (int)(h/.8);
                        chartSeries.setSize(w, h);
                        chartSeries.setLocation(screenSize.width/3, size.height);
                    }
                    chartSeries.setVisible(true);
                    break;
                case STEP_CHART_RETURN:
                    jEditorPaneDescribe.setPage(url("chartReturns.html"));
                    if (chartReturns == null) {
                        chartReturns = new ChartReturns(db, "spx");
                        int h = Math.min(screenSize.width/2, screenSize.height-size.height-32);
                        int w = (int)(h/.8);
                        chartReturns.setSize(w, h);
                        chartReturns.setLocation(screenSize.width/3, size.height);
                    }
                    chartReturns.setVisible(true);
                    break;
                case STEP_CHART_SIMULATE:
                    jEditorPaneDescribe.setPage(url("simulate.html"));
                    if (simulate == null) {
                        Covariances covariance = new Covariances(db.getReturns());
                        double covar[][] = covariance.compute(Covariances.VARIANCE_COVARIANCE_MATRIX);
                        simulate = new Simulate(db, covar);
                        int h = Math.min(screenSize.width/2, screenSize.height-size.height-32);
                        int w = (int)(h/.8);
                        simulate.setSize(w, h);
                        simulate.setLocation(screenSize.width/3, size.height);
                    }
                    simulate.setVisible(true);
                    break;
            }
        } catch (Exception e) {
            javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(), "Exception", javax.swing.JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    private String url(String name) {
        String s = "com/imsl/demo/risk/"  + name;
        return getClass().getClassLoader().getResource(s).toExternalForm();
    }



    /** 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
        jPanel2 = new javax.swing.JPanel();
        jButtonBack = new javax.swing.JButton();
        jButtonNext = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPaneDescribe = new javax.swing.JEditorPane();

        setTitle("Risk Analysis");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

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

        jPanel2.add(jButtonBack);

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

        jPanel2.add(jButtonNext);

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

        jEditorPaneDescribe.setEditable(false);
        jEditorPaneDescribe.setContentType("text/html");
        jEditorPaneDescribe.setPreferredSize(new java.awt.Dimension(Math.min((int)(getToolkit().getScreenSize().width/2),600),175));
        jEditorPaneDescribe.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                jEditorPaneDescribeHyperlinkUpdate(evt);
            }
        });

        jScrollPane1.setViewportView(jEditorPaneDescribe);

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

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

    private void jEditorPaneDescribeHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_jEditorPaneDescribeHyperlinkUpdate
        if (evt.getEventType() != javax.swing.event.HyperlinkEvent.EventType.ACTIVATED) return;
        String page = evt.getURL().toExternalForm();
        com.imsl.demo.gallery.Describe describe = new com.imsl.demo.gallery.Describe(this,page);
        describe.show();
    }//GEN-LAST:event_jEditorPaneDescribeHyperlinkUpdate

    private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNextActionPerformed
        step++;
        update();
    }//GEN-LAST:event_jButtonNextActionPerformed

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

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        if (exitOnClose) {
            System.exit(0);
        } else {
            dispose();
        }
    }//GEN-LAST:event_exitForm

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        boolean exitOnClose = true;
        if (args.length > 0  && args[0].equals("-noexit"))  exitOnClose = false;
        new Driver(exitOnClose).show();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JEditorPane jEditorPaneDescribe;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jButtonNext;
    private javax.swing.JButton jButtonBack;
    // End of variables declaration//GEN-END:variables
}