/*
 * DerInt.java
 *
 */

package com.imsl.demo.SeriesAnalysis.Lib;
import java.util.ArrayList;
import com.imsl.demo.SeriesAnalysis.SeriesAnalysis;

/**
 *
 * @author  Ed Stewart
 * @created March 4, 2003
 */
public class DerInt extends javax.swing.JDialog {
    private ArrayList data;
    private ArrayList params;
    private int index1;
    private javax.swing.JFrame parentFrame;
    
    public DerInt(javax.swing.JFrame parent) {
        super(parent, false);
        parentFrame = parent;
        initComponents();
        java.awt.Dimension parentSize = parent.getSize();
        java.awt.Point parentLoc = parent.getLocationOnScreen();
        parentLoc.x += parentSize.width/2.5;
        parentLoc.y += parentSize.height/2.5;
        setLocation(parentLoc.x, parentLoc.y);
    }
    
    
    public void setData(ArrayList paramsList, ArrayList dataList) {
        params = paramsList;
        data = dataList;
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        jButtonInt.setEnabled(false);
        jButtonDer.setEnabled(false);
        jButtonMov.setEnabled(false);
    }
    
    /** 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();
        jList1 = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jButtonInt = new javax.swing.JButton();
        jButtonDer = new javax.swing.JButton();
        jButtonMov = new javax.swing.JButton();

        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.X_AXIS));

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

        jScrollPane1.setPreferredSize(new java.awt.Dimension(100, 300));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setFixedCellHeight(18);
        jList1.setPreferredSize(new java.awt.Dimension(300, 500));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        getContentPane().add(jScrollPane1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(300, 300));
        jPanel3.setPreferredSize(new java.awt.Dimension(300, 75));
        jLabel1.setText("Parameter Name:");
        jLabel1.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel1.setMinimumSize(new java.awt.Dimension(4, 20));
        jLabel1.setPreferredSize(new java.awt.Dimension(150, 20));
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jPanel3.add(jLabel1);

        jTextField1.setEditable(false);
        jTextField1.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jTextField1.setPreferredSize(new java.awt.Dimension(150, 20));
        jPanel3.add(jTextField1);

        jPanel2.add(jPanel3, java.awt.BorderLayout.NORTH);

        jLabel2.setText("Select Time Series to Compute:");
        jPanel2.add(jLabel2, java.awt.BorderLayout.CENTER);

        jPanel11.setPreferredSize(new java.awt.Dimension(300, 40));
        jLabel3.setText("Select Time Series to Compute:");
        jPanel11.add(jLabel3);

        jButtonInt.setText("Integral (Cumulative Sum)");
        jButtonInt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIntActionPerformed(evt);
            }
        });

        jPanel11.add(jButtonInt);

        jButtonDer.setText("Derivative");
        jButtonDer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDerActionPerformed(evt);
            }
        });

        jPanel11.add(jButtonDer);

        jButtonMov.setText("20pt Moving Average");
        jButtonMov.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMovActionPerformed(evt);
            }
        });

        jPanel11.add(jButtonMov);

        jPanel2.add(jPanel11, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2);

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

    private void jButtonDerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDerActionPerformed
        double[] dataArray = (double[]) data.get(index1);
        double[] deriv = new double[dataArray.length];
        deriv[0] = dataArray[1]-dataArray[0];
        deriv[dataArray.length-1] = dataArray[dataArray.length-1]-dataArray[dataArray.length-2];
        for (int i=1; i<(dataArray.length-1); i++) {
            deriv[i] = (dataArray[i+1] - dataArray[i-1])/2;
        }
        params.add((String) params.get(index1) + "_Derivative");
        data.add(deriv);
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        
        ((SeriesAnalysis)parentFrame).updateList();
    }//GEN-LAST:event_jButtonDerActionPerformed

    private void jButtonMovActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMovActionPerformed
        final int period = 20;
        double[] dataArray = (double[]) data.get(index1);
        double[] mov = new double[dataArray.length];
        int num = 0;
        double sum = 0.0;
        for (int i=0; i<dataArray.length; i++) {
            sum += dataArray[i];
            if (i < period) {
                num++;
                mov[i] = sum/num;
            } else {
                sum -= dataArray[i-period];
                mov[i] = sum/period;
            }
        }
        params.add((String) params.get(index1) + "_MovingAvg");
        data.add(mov);
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        
        ((SeriesAnalysis)parentFrame).updateList();

    }//GEN-LAST:event_jButtonMovActionPerformed
    
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        index1 = jList1.locationToIndex(evt.getPoint());
        jTextField1.setText((String) params.get(index1));
        jButtonInt.setEnabled(true);
        jButtonDer.setEnabled(true);
        jButtonMov.setEnabled(true);
    }//GEN-LAST:event_jList1MouseClicked
    
    private void jButtonIntActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIntActionPerformed
        double[] dataArray = (double[]) data.get(index1);
        double[] integral = new double[dataArray.length];
        integral[0] = dataArray[0];
        for (int i=1; i<dataArray.length; i++) {
            integral[i] = integral[i-1] + dataArray[i];
        }
        params.add((String) params.get(index1) + "_Integral");
        data.add(integral);
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        
        //((SeriesAnalysis)parentFrame).setData(data);
        //((SeriesAnalysis)parentFrame).setParams(params);
        ((SeriesAnalysis)parentFrame).updateList();
    }//GEN-LAST:event_jButtonIntActionPerformed
            
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        setVisible(false);
        dispose();
    }//GEN-LAST:event_exitForm
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JList jList1;
    private javax.swing.JButton jButtonDer;
    private javax.swing.JButton jButtonInt;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton jButtonMov;
    // End of variables declaration//GEN-END:variables
}