/*
 * Abs.java
 *
 */

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

/**
 *
 * @author  Ed Stewart
 * @created March 5, 2003
 */
public class Abs extends javax.swing.JDialog {
    private ArrayList data;
    private ArrayList params;
    private int index1;
    private javax.swing.JFrame parentFrame;
    
    /* The constructor accepts the required JFrame parent */
    public Abs(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);
    }

    /* The setData method accepts the two required ArrayList parameters */
    public void setData(ArrayList paramsList, ArrayList dataList) {
        params = paramsList;
        data = dataList;
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        jButtonAbs.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();
        jButtonAbs = new javax.swing.JButton();

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

        setTitle("Absolute Value");
        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 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.setForeground(java.awt.Color.blue);
        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));
        jButtonAbs.setText("Compute Absolute Value");
        jButtonAbs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAbsActionPerformed(evt);
            }
        });

        jPanel11.add(jButtonAbs);

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

        getContentPane().add(jPanel2);

        pack();
    }//GEN-END:initComponents
    
    /* Clicking on the list selects the parameter, updates the Name, and enables the Compute button */
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        index1 = jList1.locationToIndex(evt.getPoint());
        jTextField1.setText((String) params.get(index1));
        jButtonAbs.setEnabled(true);
    }//GEN-LAST:event_jList1MouseClicked
    
    /* Clicking on the compute button computes the abs[] array and adds it to the parameter lists
     * locally and in the parent by calling updateList on the parent frame */
    private void jButtonAbsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAbsActionPerformed
        double[] dataArray = (double[]) data.get(index1);
        double[] abs = new double[dataArray.length];
        for (int i=0; i<dataArray.length; i++) {
            abs[i] = com.imsl.math.JMath.abs(dataArray[i]);
        }
        params.add((String) params.get(index1) + "_Abs");
        data.add(abs);
        jList1.setVisibleRowCount(params.size());
        jList1.setPreferredSize(jList1.getPreferredScrollableViewportSize());
        jList1.setListData(params.toArray());
        
        ((SeriesAnalysis)parentFrame).updateList();
    }//GEN-LAST:event_jButtonAbsActionPerformed
            
    /** 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.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 jButtonAbs;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}