/*
 * Statistics.java
 *
 */

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

/**
 *
 * @author  bmiller
 * @created November 14, 2002
 */
public class Statistics extends javax.swing.JDialog {
    private ArrayList data;
    private ArrayList params;
    
    /** Creates new form Statistics */
    public Statistics(javax.swing.JFrame parent) {
        super(parent, false);
        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;
        data = dataList;
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
    }
    
    /** 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();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();

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

        setTitle("Statistics");
        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.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setFixedCellHeight(18);
        jList1.setMaximumSize(new java.awt.Dimension(200, 1000));
        jList1.setMinimumSize(new java.awt.Dimension(100, 100));
        jList1.setPreferredSize(new java.awt.Dimension(300, 400));
        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.X_AXIS));

        jPanel2.setPreferredSize(new java.awt.Dimension(300, 304));
        jPanel3.setLayout(new java.awt.GridLayout(8, 1));

        jPanel3.setMinimumSize(new java.awt.Dimension(100, 300));
        jPanel3.setPreferredSize(new java.awt.Dimension(150, 304));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Parameter Name: ");
        jLabel1.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel1.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel1.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel1);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText(" # of Points: ");
        jLabel2.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel2.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel2.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel2);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Minimum Value: ");
        jLabel3.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel3.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel3.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Maximum Value: ");
        jLabel4.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel4.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel4.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel4);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Median Value: ");
        jLabel5.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel5.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel5.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel5);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Std. Deviation: ");
        jLabel6.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel6.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel6.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel6);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Average Value: ");
        jLabel7.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel7.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel7.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel7);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Sum: ");
        jLabel8.setMaximumSize(new java.awt.Dimension(32767, 32767));
        jLabel8.setMinimumSize(new java.awt.Dimension(10, 10));
        jLabel8.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel3.add(jLabel8);

        jPanel2.add(jPanel3);

        jPanel4.setLayout(new java.awt.GridLayout(8, 1));

        jTextField1.setEditable(false);
        jTextField1.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField1.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField1);

        jTextField2.setEditable(false);
        jTextField2.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField2.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField2);

        jTextField3.setEditable(false);
        jTextField3.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField3.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField3);

        jTextField4.setEditable(false);
        jTextField4.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField4.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField4);

        jTextField5.setColumns(8);
        jTextField5.setEditable(false);
        jTextField5.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField5.setPreferredSize(new java.awt.Dimension(88, 38));
        jPanel4.add(jTextField5);

        jTextField6.setEditable(false);
        jTextField6.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField6.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField6);

        jTextField7.setEditable(false);
        jTextField7.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField7.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField7);

        jTextField8.setEditable(false);
        jTextField8.setMinimumSize(new java.awt.Dimension(10, 10));
        jTextField8.setPreferredSize(new java.awt.Dimension(150, 38));
        jPanel4.add(jTextField8);

        jPanel2.add(jPanel4);

        getContentPane().add(jPanel2);

        pack();
    }//GEN-END:initComponents
        
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        int index = jList1.locationToIndex(evt.getPoint());
        double[] dataArray = (double[]) data.get(index);
        jTextField1.setText((String) params.get(index));
        jTextField2.setText((new Integer(dataArray.length)).toString());
        jTextField3.setText(new Float(Summary.minimum(dataArray)).toString());
        jTextField4.setText(new Float(Summary.maximum(dataArray)).toString());
        jTextField5.setText(new Float(Summary.median(dataArray)).toString());
        jTextField6.setText(new Float(Summary.sampleStandardDeviation(dataArray)).toString());
        jTextField7.setText(new Float(average(dataArray)).toString());
        jTextField8.setText(new Float(sum(dataArray)).toString());
    }//GEN-LAST:event_jList1MouseClicked
    
    private double average(double[] array){
        double sum = 0.0;
        for (int i = 0; i < array.length; i++) {
            sum = sum+array[i];
        }
        return sum/array.length;
    }
    
    private double sum(double[] array){
        double sum = 0.0;
        for (int i = 0; i < array.length; i++) {
            sum = sum+array[i];
        }
        return sum;
    }
    /** 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.JTextField jTextField6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JList jList1;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}