/*
 * SampleSizePropDialog.java
 *
 * Created on May 27, 2008, 9:04 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 proportion. Requires to provide the confidence level, 
 * desired margin of error, and population proportion estimate.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.SampleSizeDetermination
 * @since 1.6
 */
public class SampleSizePropDialog extends StatcatoDialog {
 
    /** Creates new form SampleSizePropDialog */
    public SampleSizePropDialog(java.awt.Frame parent, boolean modal,
            Statcato mTab) {
        super(parent, modal);
        this.app = mTab;
        initComponents();
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("stat-sample-size-prop");
        name = "Sample Size Determination: 1-Population Proportion";
        description = "For determining the sample size required to estimate " +
                "a population proportion. ";
        helpStrings.add("Provide the confidence level and desired margin of " +
                "error. Provide a proportion estimate if known.");
    }
    
    /** 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();
        ConfidenceTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        EstimateTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        ErrorTextField = new javax.swing.JTextField();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

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

        jLabel1.setText("Confidence Level:");

        ConfidenceTextField.setText("0.95");

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

        jLabel3.setText("Proportion Estimate:");

        jLabel4.setText("0 - 1.00 (leave blank if unknown)");

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

        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);
            }
        });

        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(24, 24, 24)
                .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)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(EstimateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                        .addGap(2, 2, 2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ErrorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                        .addGap(2, 2, 2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addContainerGap(20, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addComponent(OKButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CancelButton)
                .addContainerGap(139, Short.MAX_VALUE))
        );

        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(20, 20, 20)
                .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)
                    .addComponent(jLabel4))
                .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))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addContainerGap(22, Short.MAX_VALUE))
        );

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

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("sample size-proportion");
        
        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;
        }
        
        if (!EstimateTextField.getText().equals("")) {
            // get and check proportion estimate
            try {
                estimate = Double.parseDouble(EstimateTextField.getText());
            }
            catch (NumberFormatException e) {
                app.showErrorDialog("Enter a number between 0 and 1 for the proportion estimate.");
                return;
            }
            if (estimate <= 0 || estimate >= 1) {
                app.showErrorDialog("Enter a number between 0 and 1 for the proportion estimate.");
                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.proportion(1 - confidenceLevel, estimate, error);
        
        app.addLogHeading("Sample Size Determination: 1-Population Proportion");
        app.addLogText("Confidence Level = " + 
                HelperFunctions.formatFloat(confidenceLevel * 100, 2) + "%");
        app.addLogText("Proportion Estimate = " + 
                ((estimate == -1)? "unknown" : 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 jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
    
}
