/*
 * SampleSizeMeanDialog.java
 *
 * Created on May 27, 2008, 10:18 AM
 */

package org.statcato.dialogs.stat.samplesize;

import org.statcato.*;
import org.statcato.statistics.inferential.*;
import org.statcato.utils.HelperFunctions;

/**
 * A dialog for determining the sample size required to estimate 
 * a population mean. Requires to provide the confidence level, 
 * desired margin of error, and population standard deviation.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.SampleSizeDetermination
 * @since 1.6
 */
public class SampleSizeMeanDialog extends StatcatoDialog {

    /** Creates new form SampleSizeMeanDialog */
    public SampleSizeMeanDialog(java.awt.Frame parent, boolean modal,
            Statcato mTab) {
        super(parent, modal);
        this.app = mTab;
        initComponents();
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("stat-sample-size-mean");
        name = "Sample Size Determination: 1-Population Mean";
        description = "For determining the sample size required to estimate " +
                "a population mean.";
        helpStrings.add("Provide the confidence level, desired margin of error, " +
                "and population standard deviation. ");
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ConfidenceTextField = new javax.swing.JTextField();
        EstimateTextField = new javax.swing.JTextField();
        ErrorTextField = new javax.swing.JTextField();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sample Size Determination: 1-Population Mean");

        jLabel1.setText("Confidence Level:");

        jLabel3.setText("Standard Deviation:");

        jLabel5.setText("Margin of Error:");

        ConfidenceTextField.setText("0.95");

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("0 - 1.00 (e.g. 0.95)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(EstimateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(ErrorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
                .addGap(6, 6, 6)
                .addComponent(jLabel2)
                .addContainerGap(21, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addComponent(OKButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(CancelButton)
                .addGap(109, 109, 109))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ConfidenceTextField, ErrorTextField, EstimateTextField});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CancelButton, OKButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(EstimateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(ErrorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("sample size-mean");
        
        double confidenceLevel = 0, estimate = -1, error = 0;
        
        // get and check confidence level
        try {
            confidenceLevel = Double.parseDouble(ConfidenceTextField.getText());
        } catch (NumberFormatException e) {
            app.showErrorDialog("Enter a number between 0 and 1 for the confidence level.");
            return;
        }
        if (confidenceLevel <= 0 || confidenceLevel >= 1) {
            app.showErrorDialog("Enter a number between 0 and 1 for the confidence level.");
            return;
        }
        
        // get and check proportion estimate
        try {
            estimate = Double.parseDouble(EstimateTextField.getText());
        } catch (NumberFormatException e) {
            app.showErrorDialog("Enter a number for standard deviation.");
            return;
        }
        
        // get margin of error
        try {
            error = Double.parseDouble(ErrorTextField.getText());
        } catch (NumberFormatException e) {
            app.showErrorDialog("Enter a valid number for the margin of error.");
            return;
        }
        
        // calculate sample size
        int n = SampleSizeDetermination.mean(1 - confidenceLevel, estimate, error);
        
        app.addLogHeading("Sample Size Determination: 1-Population Mean");
        app.addLogText("Confidence Level = " +
                HelperFunctions.formatFloat(confidenceLevel * 100, 2) + "%");
        app.addLogText("Standard Deviation = " + estimate + "");
        app.addLogText("Margin of Error = " + error + "");
        app.addLogText("<b>Sample Size = " + n + "</b>");
                
        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
        
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField ConfidenceTextField;
    private javax.swing.JTextField ErrorTextField;
    private javax.swing.JTextField EstimateTextField;
    private javax.swing.JButton OKButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
    
}
