/*
 * HyTest1PopPropDialog.java
 *
 * Created on May 29, 2008, 12:40 PM
 */

package org.statcato.dialogs.stat.hytest;

import org.statcato.*;
import org.statcato.spreadsheet.*;
import org.statcato.statistics.inferential.*;
import org.statcato.utils.HelperFunctions;
import java.util.*;
import javax.swing.*;

/**
 * A dialog for testing claims about a population proportion.
 * Allows the user to specify data samples in a column of a Datasheet,
 * or provide summary data values.  The user must also provide
 * the confidence level, alternative hypothesis, and hypothesized proportion.
 * Computes and displays in log window the significance level, 
 * critical value, test statistic, and p-Value.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.HypothesisTest1Prop
 * @since 1.0
 */
public class HyTest1PopPropDialog extends StatcatoDialog {

    /** Creates new form HyTest1PopPropDialog */
    public HyTest1PopPropDialog(java.awt.Frame parent, boolean modal,
            Statcato mTab) {
        super(parent, modal);
        initComponents();
        
        app = mTab;
        ParentSpreadsheet = app.getSpreadsheet(); 
        
        ButtonGroup group1 = new ButtonGroup();
        group1.add(SampleRadioButton);
        group1.add(SummaryRadioButton);
        
        ButtonGroup group3 = new ButtonGroup();
        group3.add(SigRadioButton);
        group3.add(ConfRadioButton);
                
        ParentSpreadsheet.populateComboBox(ColumnComboBox);
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("stat-hytest-prop");
        name = "Hypothesis Test: 1-Population Proportion";
        description = "For performing calculations for testing claims about " +
                "a population proportion.";
        helpStrings.add("If individual samples are entered in a single " +
                "column of the datasheet, select the Samples in " +
                "column: radio button, and select the column name " +
                "in the drop-down menu.");
        helpStrings.add("To use summary statistics of the sample data, " +
                "select the Summarized sample data: radio button, " +
                "and input the number of events and the number of trials in the provided text fields.");
        helpStrings.add("Enter the significance or confidence level (between 0 and 1).");
        helpStrings.add("Select the form of the alternative hypothesis " +
                "and enter the hypothesized population proportion in the provided text box.");
    }

    /**
     * Updates elements on the dialog so that they have the most
     * current values.  Called by {@link #setVisible} to make
     * sure the dialog displays current values when made visible again.
     * 
     * @see #setVisible(boolean)
     */
    @Override
    public void updateElements() {
        updateComboBox(ColumnComboBox);
    }
    
    /** 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() {

        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        InputPanel = new javax.swing.JPanel();
        SampleRadioButton = new javax.swing.JRadioButton();
        TrialsLabel = new javax.swing.JLabel();
        SummaryRadioButton = new javax.swing.JRadioButton();
        ColumnComboBox = new javax.swing.JComboBox();
        EventsLabel = new javax.swing.JLabel();
        EventsTextField = new javax.swing.JTextField();
        TrialsTextField = new javax.swing.JTextField();
        AltPanel = new javax.swing.JPanel();
        AltComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        HyTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        SigPanel = new javax.swing.JPanel();
        SigLabel = new javax.swing.JLabel();
        ConfidenceTextField = new javax.swing.JTextField();
        ConfRadioButton = new javax.swing.JRadioButton();
        SigRadioButton = new javax.swing.JRadioButton();
        SigTextField = new javax.swing.JTextField();
        ConfLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Hypothesis Test: 1-Population Proportion");

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

        InputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Inputs"));

        SampleRadioButton.setSelected(true);
        SampleRadioButton.setText("Samples in column:");
        SampleRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SampleRadioButtonStateChanged(evt);
            }
        });

        TrialsLabel.setText("Number of trials:");
        TrialsLabel.setEnabled(false);

        SummaryRadioButton.setText("Summarized sample data:");
        SummaryRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SummaryRadioButtonStateChanged(evt);
            }
        });

        EventsLabel.setText("Number of events:");
        EventsLabel.setEnabled(false);

        EventsTextField.setEnabled(false);

        TrialsTextField.setEnabled(false);

        javax.swing.GroupLayout InputPanelLayout = new javax.swing.GroupLayout(InputPanel);
        InputPanel.setLayout(InputPanelLayout);
        InputPanelLayout.setHorizontalGroup(
            InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SampleRadioButton)
                    .addGroup(InputPanelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(EventsLabel)
                            .addGroup(InputPanelLayout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(TrialsLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TrialsTextField)
                            .addComponent(EventsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, InputPanelLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(ColumnComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(SummaryRadioButton, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        InputPanelLayout.setVerticalGroup(
            InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SampleRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ColumnComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(SummaryRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EventsLabel)
                    .addComponent(EventsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TrialsLabel)
                    .addComponent(TrialsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        AltPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Alternative Hypothesis"));

        AltComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Less than", "Not Equal to", "Greater than" }));
        AltComboBox.setSelectedIndex(1);

        jLabel3.setText("Alternative Hypothesis:");

        jLabel2.setText("Hypothesized Proportion:");

        javax.swing.GroupLayout AltPanelLayout = new javax.swing.GroupLayout(AltPanel);
        AltPanel.setLayout(AltPanelLayout);
        AltPanelLayout.setHorizontalGroup(
            AltPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AltPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AltPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AltPanelLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AltComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(AltPanelLayout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(HyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        AltPanelLayout.setVerticalGroup(
            AltPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AltPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AltPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(AltComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AltPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(HyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        SigPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Significance"));

        SigLabel.setText("0 - 1.00 (e.g. 0.05)");

        ConfidenceTextField.setText("0.95");
        ConfidenceTextField.setEnabled(false);
        ConfidenceTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ConfidenceTextFieldFocusLost(evt);
            }
        });

        ConfRadioButton.setText("Confidence Level:");
        ConfRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                ConfRadioButtonStateChanged(evt);
            }
        });

        SigRadioButton.setSelected(true);
        SigRadioButton.setText("Significance Level:");
        SigRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SigRadioButtonStateChanged(evt);
            }
        });

        SigTextField.setText("0.05");
        SigTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                SigTextFieldFocusLost(evt);
            }
        });

        ConfLabel.setText("0 - 1.00 (e.g. 0.95)");
        ConfLabel.setEnabled(false);

        javax.swing.GroupLayout SigPanelLayout = new javax.swing.GroupLayout(SigPanel);
        SigPanel.setLayout(SigPanelLayout);
        SigPanelLayout.setHorizontalGroup(
            SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SigPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SigPanelLayout.createSequentialGroup()
                        .addComponent(ConfRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(SigPanelLayout.createSequentialGroup()
                        .addComponent(SigRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(SigLabel)
                    .addComponent(ConfLabel))
                .addContainerGap())
        );
        SigPanelLayout.setVerticalGroup(
            SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SigPanelLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SigRadioButton)
                    .addComponent(SigTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SigLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(SigPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ConfidenceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ConfRadioButton)
                    .addComponent(ConfLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(InputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(AltPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(SigPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(228, 228, 228)
                        .addComponent(OKButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelButton)))
                .addContainerGap(15, Short.MAX_VALUE))
        );

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

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(SigPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AltPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(InputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(OKButton))
                .addGap(19, 19, 19))
        );

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

    private void SummaryRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SummaryRadioButtonStateChanged
        if (SummaryRadioButton.isSelected()) {
            EventsLabel.setEnabled(true);
            EventsTextField.setEnabled(true);
            TrialsLabel.setEnabled(true);
            TrialsTextField.setEnabled(true);
        } else  {
            EventsLabel.setEnabled(false);
            EventsTextField.setEnabled(false);
            TrialsLabel.setEnabled(false);
            TrialsTextField.setEnabled(false);
        }
    }//GEN-LAST:event_SummaryRadioButtonStateChanged

    private void SampleRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SampleRadioButtonStateChanged
        if (SampleRadioButton.isSelected()) {
            ColumnComboBox.setEnabled(true);
        } else {
            ColumnComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_SampleRadioButtonStateChanged

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("hypothesis test 1 proportion");
        
        int events = 0, trials = 0, typeTest;
        double confidenceLevel = 0, pHat = 0;
               
        String heading = "Hypothesis Test - One population proportion:";
        String text = "";
        
        if (ConfRadioButton.isSelected()) {
            // get confidence level from dialog
            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;
            }
        }
        else {
            // get confidence level from dialog
            try {
                confidenceLevel = 1 - Double.parseDouble(SigTextField.getText());
            } catch (NumberFormatException e) {
                app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
                return;
            }
            if (confidenceLevel <= 0 || confidenceLevel >= 1) {
                app.showErrorDialog("Enter a number between 0 and 1 for the significance level.");
                return;
            }
        }
        
        heading += "  confidence level = " + 
                String.format("%."+
                HelperFunctions.getNumDecimalPlaces(ConfidenceTextField.getText())+"f", 
                confidenceLevel);
        
        // get input
        if (SampleRadioButton.isSelected()) {
            // get data for selected input column
            String columnLabel = ColumnComboBox.getSelectedItem().toString();
            if (columnLabel.equals("")) { // no input variable
                app.showErrorDialog("Select the input column.");
                return;
            }
            int column = ParentSpreadsheet.parseColumnNumber(columnLabel);
            Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(column);
            
            String cat1 = "", cat2 = "";    // two categories
            int count1 = 0, count2 = 0;     // number of events in each category
            
            // find out the two categories present in the column
            // error if more than two categories
            for (int i = 0; i < StrColumnVector.size(); ++i) {
                String s = StrColumnVector.elementAt(i).getContents().trim();
                if (!s.equals("")) {
                    if (cat1.equals("")) {  // first instance of category 1
                        cat1 = s;
                        count1++;
                    } else if (s.equals(cat1)) { // non-first instance of category 1
                        count1++;
                    } else if (cat2.equals("")) { // first instance of category 2
                        cat2 = s;
                        count2++;
                    } else if (s.equals(cat2)) { // non-first instance of category 2
                        count2++;
                    } else { // neither category 1 or 2
                        app.showErrorDialog("The input column contains more than two categories.");
                        return;
                    }
                }
            }
            
            trials = count1 + count2;
            if (trials == 0) {  // empty column
                app.showErrorDialog("The input column is empty.");
                return;
            }
            events = count1;
            text += "Input: " + columnLabel + "<br>";
            text += "Event: " + cat1 + "<br>";
        } else {
            // get summary data
            try {
                events = Integer.parseInt(EventsTextField.getText());
                trials = Integer.parseInt(TrialsTextField.getText());
            } catch (NumberFormatException e) {
                app.showErrorDialog("Invalid summary data.");
                return;
            }
            if (events > trials) {
                app.showErrorDialog("The number of events must not be" +
                        " greater than the number of trials.");
                return;
            }
            text += "Input: Summary data" + "<br>";
        }

        // get hypothesized proportion
        try {
            pHat = Double.parseDouble(HyTextField.getText());            
        }
        catch (NumberFormatException e) {
            app.showErrorDialog("Enter a valid number for the hypothesized proportion.");
            return;
        }
        if (pHat <= 0 || pHat >= 1) {
            app.showErrorDialog("Enter a valid number for the hypothesized proportion.");
            return;
        }
        
        double p = (double)events / trials;
        
        if (trials * p < 5 || trials * (1-p) < 5)
            text += "* WARNING: The sample size is too small for normal approximation"
                    + " to be valid. *<br>";        
        
        // get type of test
        typeTest = AltComboBox.getSelectedIndex();
        
        HypothesisTest1Prop ht = new HypothesisTest1Prop(trials, pHat, 
                confidenceLevel, typeTest);
        
        // outputs
        text += "Null hypothesis: p = " + pHat + "<br>";
        text += "Alternative hypothesis: p ";
        if (typeTest == HypothesisTest.LEFT_TAIL)
            text += "&lt;";
        else if (typeTest == HypothesisTest.RIGHT_TAIL)
            text += "&gt;";
        else
            text += "&ne;";
        text += " " + pHat + "<br>";
        text += "<br><table border='1'>";
        text += "<tr><td>N</td><td>Sample Proportion</td>";
        text += "<td>Significance Level</td></td><td>Critical Value</td>" +
                "<td>Test Statistic Z</td>" + "<td>p-Value</td></tr>";
        text += "<tr><td>" + trials + "</td><td>" +
                HelperFunctions.formatFloat(p, 3) + "</td><td>" +
                HelperFunctions.formatFloat(1-confidenceLevel, 2)  +
                "</td><td>" + ht.criticalValue()
                + "</td><td>" + HelperFunctions.formatFloat(ht.testStatistics(p), 3)
                + "</td><td>" + HelperFunctions.formatFloat(ht.pValue(p), 4)
                + "</td></tr></table>";        
                
        app.addLogParagraph(heading, text);

        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

    private void ConfidenceTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ConfidenceTextFieldFocusLost
        try {
            String confString = ConfidenceTextField.getText();
            double conf = Double.parseDouble(confString);
            double sig = 1.0 - conf;
            SigTextField.setText(
                     String.format("%."+HelperFunctions.getNumDecimalPlaces(confString)+"f", sig));
        } catch (NumberFormatException e) {
        }
}//GEN-LAST:event_ConfidenceTextFieldFocusLost

    private void ConfRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_ConfRadioButtonStateChanged
        if (ConfRadioButton.isSelected()) {
            ConfidenceTextField.setEnabled(true);
            ConfLabel.setEnabled(true);
        } else {
            ConfidenceTextField.setEnabled(false);
            ConfLabel.setEnabled(false);
        }
}//GEN-LAST:event_ConfRadioButtonStateChanged

    private void SigRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SigRadioButtonStateChanged
        if (SigRadioButton.isSelected()) {
            SigTextField.setEnabled(true);
            SigLabel.setEnabled(true);
        } else {
            SigTextField.setEnabled(false);
            SigLabel.setEnabled(false);
        }
}//GEN-LAST:event_SigRadioButtonStateChanged

    private void SigTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_SigTextFieldFocusLost
        try {
            String sigString = SigTextField.getText();
            double sig = Double.parseDouble(sigString);
            double conf = 1 - sig;
            ConfidenceTextField.setText(
                    String.format("%."+HelperFunctions.getNumDecimalPlaces(sigString)+"f", conf));
        } catch (NumberFormatException e) {
        }
}//GEN-LAST:event_SigTextFieldFocusLost
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AltComboBox;
    private javax.swing.JPanel AltPanel;
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox ColumnComboBox;
    private javax.swing.JLabel ConfLabel;
    private javax.swing.JRadioButton ConfRadioButton;
    private javax.swing.JTextField ConfidenceTextField;
    private javax.swing.JLabel EventsLabel;
    private javax.swing.JTextField EventsTextField;
    private javax.swing.JTextField HyTextField;
    private javax.swing.JPanel InputPanel;
    private javax.swing.JButton OKButton;
    private javax.swing.JRadioButton SampleRadioButton;
    private javax.swing.JLabel SigLabel;
    private javax.swing.JPanel SigPanel;
    private javax.swing.JRadioButton SigRadioButton;
    private javax.swing.JTextField SigTextField;
    private javax.swing.JRadioButton SummaryRadioButton;
    private javax.swing.JLabel TrialsLabel;
    private javax.swing.JTextField TrialsTextField;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
    
}
