/*
 * Akima.java
 *
 */

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

/**
 *
 * @author  bmiller
 * @created November 15, 2002
 */
public class Akima extends javax.swing.JDialog {
    private ArrayList data;
    private ArrayList params;
    private CsAkima spln;
    private int index1;
    private javax.swing.JFrame parentFrame;
    
    /** Creates new form Akima */
    public Akima(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 names, ArrayList dataList) {
        params = names;
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        data = dataList;
        jList1.setListData(params.toArray());
        String[] order = {" Select"," 0th"," 1st"," 2nd"," 3rd"," 4th"," 5th"," 6th"," 7th"," 8th"," 9th"};
        for (int i = 0; i < 11; i++) jComboBox1.addItem(order[i]);
        jComboBox1.setEnabled(false);
        jButton1.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();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel10 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jPanel11 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

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

        setTitle("Akima Cubic Spline");
        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.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 javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));

        jPanel2.setPreferredSize(new java.awt.Dimension(300, 300));
        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.X_AXIS));

        jPanel3.setPreferredSize(new java.awt.Dimension(300, 40));
        jLabel1.setText(" Name:");
        jLabel1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel1.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        jLabel1.setMinimumSize(new java.awt.Dimension(4, 20));
        jLabel1.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel3.add(jLabel1);

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

        jPanel2.add(jPanel3);

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

        jPanel4.setPreferredSize(new java.awt.Dimension(300, 20));
        jLabel2.setText(" Integral");
        jLabel2.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel2.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel2.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel2.setPreferredSize(new java.awt.Dimension(300, 20));
        jPanel4.add(jLabel2);

        jPanel2.add(jPanel4);

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

        jPanel5.setPreferredSize(new java.awt.Dimension(300, 120));
        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.Y_AXIS));

        jPanel8.setPreferredSize(new java.awt.Dimension(150, 120));
        jLabel5.setText(" Upper Limit:");
        jLabel5.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel5.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel5.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel8.add(jLabel5);

        jLabel6.setText(" Lower Limit:");
        jLabel6.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel6.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel6.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel8.add(jLabel6);

        jLabel7.setText(" Value:");
        jLabel7.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel7.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel7.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel8.add(jLabel7);

        jPanel5.add(jPanel8);

        jPanel9.setLayout(new javax.swing.BoxLayout(jPanel9, javax.swing.BoxLayout.Y_AXIS));

        jPanel9.setPreferredSize(new java.awt.Dimension(150, 120));
        jTextField2.setEditable(false);
        jTextField2.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel9.add(jTextField2);

        jTextField3.setEditable(false);
        jTextField3.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel9.add(jTextField3);

        jTextField4.setEditable(false);
        jTextField4.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel9.add(jTextField4);

        jPanel5.add(jPanel9);

        jPanel2.add(jPanel5);

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

        jPanel6.setPreferredSize(new java.awt.Dimension(300, 20));
        jLabel3.setText(" Derivative");
        jLabel3.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel3.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel3.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel3.setPreferredSize(new java.awt.Dimension(300, 20));
        jPanel6.add(jLabel3);

        jPanel2.add(jPanel6);

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

        jPanel7.setPreferredSize(new java.awt.Dimension(300, 40));
        jLabel4.setText(" Order:");
        jLabel4.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel4.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel4.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel4.setPreferredSize(new java.awt.Dimension(150, 40));
        jPanel7.add(jLabel4);

        jComboBox1.setPreferredSize(new java.awt.Dimension(150, 40));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jPanel7.add(jComboBox1);

        jPanel2.add(jPanel7);

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

        jPanel10.setPreferredSize(new java.awt.Dimension(300, 20));
        jLabel8.setText(" Add Akima Cubic Spline");
        jLabel8.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel8.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        jLabel8.setPreferredSize(new java.awt.Dimension(300, 20));
        jPanel10.add(jLabel8);

        jPanel2.add(jPanel10);

        jPanel11.setPreferredSize(new java.awt.Dimension(300, 40));
        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel11.add(jButton1);

        jPanel2.add(jPanel11);

        getContentPane().add(jPanel2);

        pack();
    }//GEN-END:initComponents
    
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        index1 = jList1.locationToIndex(evt.getPoint());
        double[] dataArray = (double[]) data.get(index1);
        double[] x = new double [dataArray.length];
        for (int i = 0; i < dataArray.length; i++) x[i] = i;
        spln = new CsAkima(x,dataArray);
        jTextField1.setText((String) params.get(index1));
        jComboBox1.setEnabled(true);
        jButton1.setEnabled(true);
        double max = Summary.maximum(x);
        double min = Summary.minimum(x);
        jTextField2.setText(new Float(max).toString());
        jTextField3.setText(new Float(min).toString());
        jTextField4.setText(new Float(spln.integral(min,max)).toString());
    }//GEN-LAST:event_jList1MouseClicked
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        double[] dataArray = (double[]) data.get(index1);
        params.add((String) params.get(index1) + "_CsAkima");
        double[] tmp = spln.value((double[]) data.get(index1));
        data.add(tmp);
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        ((SeriesAnalysis)parentFrame).updateList();
    }//GEN-LAST:event_jButton1ActionPerformed
        
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        int index2 = jComboBox1.getSelectedIndex();
        if (index2 > 0) {
            double[] dataArray = (double[]) data.get(index1);
            double[] x = new double [dataArray.length];
            for (int i = 0; i < dataArray.length; i++) x[i] = i;
            params.add(((String) params.get(index1))+((String) jComboBox1.getSelectedItem())+"_CsA_Derivative");
            double[] tmp = spln.derivative(x,index2-1);
            data.add(tmp);
            jList1.setVisibleRowCount(params.size());
            jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
            jList1.setListData(params.toArray());
            jButton1.setEnabled(true);
            ((SeriesAnalysis)parentFrame).updateList();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    /** 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.JLabel jLabel8;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JList jList1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}