/*
 * PvalueDialog.java
 *
 * Created on April 15, 2008, 11:17 AM
 */

package org.statcato.dialogs.calc;

import org.statcato.*;
import org.statcato.utils.HelperFunctions;
import org.statcato.statistics.*;
import javax.swing.*;

/**
 * A dialog for computing p-Values of four types of distributions 
 * (normal, Student's t, chi-square, and F) and 
 * three types of tests (left-tail, right-tail, and two-tail), 
 * given the distribution parameters and test statistic.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class PvalueDialog extends StatcatoDialog {
    final int normal = 0;
    final int studentT = 1;
    final int chiSquare = 2;
    final int fisher = 3;
    final int leftTail = 0;
    final int rightTail = 1;
    final int twoTail = 2;
    
    /** Creates new form PvalueDialog */
    public PvalueDialog(java.awt.Frame parent, boolean modal, Statcato app) {
        super(parent, modal);
        this.app = app;
        initComponents();
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("calc-pvalue");
        name = "p-Value";
        description = "For calculating p-values given the type of distribution," +
                " distribution parameters, and the type of test.";
        helpStrings.add("Select the type of distribution and enter its " +
                "parameters.");
        helpStrings.add("Select the type of test: left-, right-, or two-tailed");
        helpStrings.add("Enter the test statistic.");
    }
    
    /** 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() {

        TitleLabel = new javax.swing.JLabel();
        InputsPanel = new javax.swing.JPanel();
        DistributionLabel = new javax.swing.JLabel();
        DistributionMenu = new javax.swing.JComboBox();
        TestTypeLabel = new javax.swing.JLabel();
        TestTypeMenu = new javax.swing.JComboBox();
        TestStatisticsLabel = new javax.swing.JLabel();
        TestStatisticsBox = new javax.swing.JTextField();
        DOF1Label = new javax.swing.JLabel();
        DOF2Label = new javax.swing.JLabel();
        DOF1Box = new javax.swing.JTextField();
        DOF2Box = new javax.swing.JTextField();
        ClearButton = new javax.swing.JButton();
        OKButton = new javax.swing.JButton();
        TestStatisticsErrorLabel = new javax.swing.JLabel();
        DOF1ErrorLabel = new javax.swing.JLabel();
        DOF2ErrorLabel = new javax.swing.JLabel();
        ResultsPanel = new javax.swing.JPanel();
        ResultsText = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("p-Value");
        setMinimumSize(new java.awt.Dimension(400, 700));

        TitleLabel.setBackground(new java.awt.Color(255, 255, 255));
        TitleLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        TitleLabel.setText("Computing p-Value");

        InputsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Inputs", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));

        DistributionLabel.setText("Distribution");

        DistributionMenu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Student's t", "Chi-square", "F (Fisher)" }));
        DistributionMenu.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                DistributionMenuItemStateChanged(evt);
            }
        });

        TestTypeLabel.setText("Type of Test");

        TestTypeMenu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Left-tail", "Right-tail", "Two-tail" }));
        TestTypeMenu.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                TestTypeMenuItemStateChanged(evt);
            }
        });

        TestStatisticsLabel.setText("Test Statistic");

        TestStatisticsBox.setToolTipText("Enter a valid number");
        TestStatisticsBox.setInputVerifier(new TestStatisticsFieldVerifier());
        TestStatisticsBox.setMinimumSize(new java.awt.Dimension(30, 22));

        DOF1Label.setText("Degrees of Freedom");

        DOF2Label.setText("Degrees of Freedom 2");

        DOF1Box.setToolTipText("Enter a positive integer");
        DOF1Box.setInputVerifier(new DOF1FieldVerifier());

        DOF2Box.setToolTipText("Enter a positive integer");
        DOF2Box.setInputVerifier(new DOF2FieldVerifier());

        ClearButton.setText("Clear");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

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

        TestStatisticsErrorLabel.setBackground(new java.awt.Color(255, 255, 255));
        TestStatisticsErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        TestStatisticsErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
        TestStatisticsErrorLabel.setText("Enter a valid number");

        DOF1ErrorLabel.setBackground(new java.awt.Color(255, 255, 255));
        DOF1ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        DOF1ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
        DOF1ErrorLabel.setText("Enter a positive integer");

        DOF2ErrorLabel.setBackground(new java.awt.Color(255, 255, 255));
        DOF2ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        DOF2ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
        DOF2ErrorLabel.setText("Enter a positive integer");

        javax.swing.GroupLayout InputsPanelLayout = new javax.swing.GroupLayout(InputsPanel);
        InputsPanel.setLayout(InputsPanelLayout);
        InputsPanelLayout.setHorizontalGroup(
            InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InputsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TestStatisticsLabel)
                    .addGroup(InputsPanelLayout.createSequentialGroup()
                        .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DistributionLabel)
                            .addComponent(DistributionMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(OKButton)
                                .addComponent(DOF1Label))
                            .addComponent(DOF1Box, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(InputsPanelLayout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(DOF2Label)
                                    .addComponent(TestTypeLabel)
                                    .addComponent(TestTypeMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(DOF2ErrorLabel)
                                    .addComponent(DOF2Box, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(InputsPanelLayout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(ClearButton))))
                    .addGroup(InputsPanelLayout.createSequentialGroup()
                        .addComponent(TestStatisticsBox, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TestStatisticsErrorLabel))
                    .addComponent(DOF1ErrorLabel))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        InputsPanelLayout.setVerticalGroup(
            InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InputsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DistributionLabel)
                    .addComponent(TestTypeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DistributionMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TestTypeMenu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(TestStatisticsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TestStatisticsErrorLabel)
                    .addComponent(TestStatisticsBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DOF1Label)
                    .addComponent(DOF2Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DOF1Box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DOF2Box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DOF1ErrorLabel)
                    .addComponent(DOF2ErrorLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(InputsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(ClearButton))
                .addGap(22, 22, 22))
        );

        DOF1Label.setVisible(false);
        DOF2Label.setVisible(false);
        DOF1Box.setVisible(false);
        DOF2Box.setVisible(false);
        TestStatisticsErrorLabel.setVisible(false);
        DOF1ErrorLabel.setVisible(false);
        DOF2ErrorLabel.setVisible(false);

        ResultsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Results", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));

        ResultsText.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        ResultsText.setText("P-value=");
        ResultsText.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout ResultsPanelLayout = new javax.swing.GroupLayout(ResultsPanel);
        ResultsPanel.setLayout(ResultsPanelLayout);
        ResultsPanelLayout.setHorizontalGroup(
            ResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ResultsPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ResultsText, javax.swing.GroupLayout.PREFERRED_SIZE, 331, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        ResultsPanelLayout.setVerticalGroup(
            ResultsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ResultsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ResultsText, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ResultsPanel.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ResultsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TitleLabel)
                    .addComponent(InputsPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(43, 43, 43))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel)
                .addGap(18, 18, 18)
                .addComponent(InputsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ResultsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(94, Short.MAX_VALUE))
        );

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

    private void DistributionMenuItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_DistributionMenuItemStateChanged
        // DistributionMenu item changed
        
        // normal
        if (evt.getItem() == (String) DistributionMenu.getItemAt(normal)) {
            //System.out.println("normal");
            DOF1Label.setVisible(false);
            DOF1Box.setVisible(false);
            DOF2Label.setVisible(false);
            DOF2Box.setVisible(false);
        }
        // student t
        else if (evt.getItem() == (String) DistributionMenu.getItemAt(studentT)) {
            //System.out.println("student t");
            DOF1Label.setVisible(true);
            DOF1Label.setText("Degrees of Freedom");
            DOF1Box.setVisible(true);
            DOF1Box.setText("");
            DOF2Label.setVisible(false);
            DOF2Box.setVisible(false);
        }
        // chi-square
        else if (evt.getItem() == (String) DistributionMenu.getItemAt(chiSquare)) {
            //System.out.println("chi-square");
            DOF1Label.setVisible(true);
            DOF1Label.setText("Degrees of Freedom");
            DOF1Box.setVisible(true);
            DOF1Box.setText("");
            DOF2Label.setVisible(false);
            DOF2Box.setVisible(false);
        }
        // Fisher
        else {
            //System.out.println("fisher");
            DOF1Label.setVisible(true);
            DOF1Label.setText("Degrees of Freedom 1");
            DOF1Box.setVisible(true);
            DOF1Box.setText("");
            DOF2Label.setVisible(true);
            DOF2Box.setVisible(true);
            DOF2Box.setText("");
        }
    }//GEN-LAST:event_DistributionMenuItemStateChanged

    private void TestTypeMenuItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_TestTypeMenuItemStateChanged
       
        ResultsPanel.setVisible(false);
    }//GEN-LAST:event_TestTypeMenuItemStateChanged

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        // ClearButton is pressed -> restore all inputs to default
        // normal, left-tail, all text fields empty
        DistributionMenu.setSelectedIndex(0);
        TestTypeMenu.setSelectedIndex(0);
        TestStatisticsBox.setText("");
        DOF1Box.setVisible(false);
        DOF1Box.setText("");
        DOF1ErrorLabel.setVisible(false);
        DOF1Label.setVisible(false);
        DOF2Box.setVisible(false);
        DOF2Box.setText("");
        DOF2ErrorLabel.setVisible(false);
        DOF2Label.setVisible(false);
        ResultsPanel.setVisible(false);
    }//GEN-LAST:event_ClearButtonActionPerformed

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("p value");
        
        // OKButton is pressed -> compute p-value
        double area, pvalue;
        double testStatistics = 0;
        int dof1 = 0, dof2 = 0;
        boolean inputError = false;
        String results = "";
        String TestStatisticsText = "";
        String DistributionText = "";
        String TestTypeText = "";
        String DOF1Text = "";
        String DOF2Text = "";
        String CumProbText = "";
        String PValueText = "";
        
        try {
            testStatistics = Double.parseDouble(TestStatisticsBox.getText());
            if (DistributionMenu.getSelectedIndex() == chiSquare || DistributionMenu.getSelectedIndex() == fisher) {
                // check if ts is negative
                if (testStatistics < 0) {
                    inputError = true;
                    TestStatisticsErrorLabel.setVisible(true);
                }
            }
        } catch (NumberFormatException e) {
            inputError = true;
            TestStatisticsErrorLabel.setVisible(true);
        }
        
        /* DISTRIBUTION */
        // normal
        if (DistributionMenu.getSelectedIndex() == normal) {
            //area = SpecialFunctions.cumNorm(testStatistics);
            NormalProbabilityDistribution npd = new NormalProbabilityDistribution(0, 1);
            area = npd.cumulativeProbability(testStatistics);
            DistributionText += "Distribution = Normal<br>";
        }
        // student t
        else if (DistributionMenu.getSelectedIndex() == studentT) {
            try {
                dof1 = Integer.parseInt(DOF1Box.getText());
                if (dof1 <= 0) {
                    inputError = true;
                    DOF1ErrorLabel.setVisible(true);
                }
            } catch (NumberFormatException e) {
                inputError = true;
                DOF1ErrorLabel.setVisible(true);
            }
            StudentTProbabilityDistribution dist = new StudentTProbabilityDistribution(dof1);
            //area = SpecialFunctions.cumT(testStatistics, dof1);
            area = dist.cumulativeProbability(testStatistics);
            DistributionText += "Distribution = Student's t<br>";
            DOF1Text += "Degree of freedom = " + dof1 + "<br>";
        }
        // chi-square
        else if (DistributionMenu.getSelectedIndex() == chiSquare) {
            try {
                dof1 = Integer.parseInt(DOF1Box.getText());
                if (dof1 <= 0) {
                    inputError = true;
                    DOF1ErrorLabel.setVisible(true);
                }
            } catch (NumberFormatException e) {
                inputError = true;
                DOF1ErrorLabel.setVisible(true);
            }
            ChiSquareProbabilityDistribution dist = new ChiSquareProbabilityDistribution(dof1);
            //area = SpecialFunctions.cumChiSquare(testStatistics, dof1);
            area = dist.cumulativeProbability(testStatistics);
            DistributionText += "Distribution = Chi-square<br>";
            DOF1Text += "Degree of freedom = " + dof1 + "<br>";
        }
        // Fisher
        else {
            try {
                dof1 = Integer.parseInt(DOF1Box.getText());
                if (dof1 <= 0) {
                    inputError = true;
                    DOF1ErrorLabel.setVisible(true);
                }
            } catch (NumberFormatException e) {
                inputError = true;
                DOF1ErrorLabel.setVisible(true);
            }
            try {
                dof2 = Integer.parseInt(DOF2Box.getText());
                if (dof2 <= 0) {
                    inputError = true;
                    DOF2ErrorLabel.setVisible(true);
                }
            } catch (NumberFormatException e) {
                DOF2ErrorLabel.setVisible(true);
            }
            FProbabilityDistribution dist = new FProbabilityDistribution(dof1, dof2);
            //area = SpecialFunctions.cumF(testStatistics, dof1, dof2);
            area = dist.cumulativeProbability(testStatistics);
            DistributionText += "Distribution = Fisher<br>";
            DOF1Text += "Degree of freedom 1 = " + dof1 + "<br>";
            DOF2Text += "Degree of freedom 2 = " + dof2 + "<br>";
        }
        
        /* TYPE OF TEST */
        // left tail
        if (TestTypeMenu.getSelectedIndex() == leftTail) {
            pvalue = area;
            TestTypeText += "Type of Test = Left-tail<br>";
        }
        // right tail
        else if (TestTypeMenu.getSelectedIndex() == rightTail) {
            pvalue = 1 - area;
            TestTypeText += "Type of Test = Right-tail<br>";
        }
        // two tail
        else {
            pvalue = 2* Math.min(area, 1-area);
            TestTypeText += "Type of Test = Two-tail<br>";
        }
        
        if (!inputError) {
            TestStatisticsText += "Test Statistic = " + HelperFunctions.formatFloat(testStatistics, 4) + "<br>";
            PValueText += "p-Value = ";
            CumProbText += "Cumulative Probability = " + HelperFunctions.formatFloat(area, 4) + "<br>";
            
            // p-value can never be 0 for normal or t distribution
            if (DistributionMenu.getSelectedIndex() == normal || DistributionMenu.getSelectedIndex() == studentT) {
                if (pvalue == 0) {
                    PValueText += "0+";
                } else
                    PValueText += HelperFunctions.formatFloat(pvalue, 4);
            }
            // p-value can never be 0 when the test statistic is positive
            // for the chi-square and F distribution
            else {
                if (testStatistics > 0 && pvalue == 0) {
                    PValueText += "0+";
                } else
                    PValueText += HelperFunctions.formatFloat(pvalue, 4);
            }
            
            // display results
            results += DistributionText + TestTypeText + TestStatisticsText;
            results += DOF1Text + DOF2Text + CumProbText + PValueText;

            app.addLogParagraph("Computing p-Value", results);
            
            app.compoundEdit.end();
            app.addCompoundEdit(app.compoundEdit);
        
            ResultsText.setText("<html>" + results + "</html>");
            ResultsPanel.setVisible(true);
            
            toFront();
            setVisible(true);
        }
        
    }//GEN-LAST:event_OKButtonActionPerformed

    class TestStatisticsFieldVerifier extends InputVerifier {
         public boolean verify(JComponent input) {
 	       JTextField tf = (JTextField) input;
               boolean ok = true;
 	       try {
                   double ts = Double.parseDouble(tf.getText());
                   if (DistributionMenu.getSelectedIndex() == chiSquare || DistributionMenu.getSelectedIndex() == fisher)
                   {
                       // check if input is non-negative
                       if (ts < 0) {
                           ok = false;
                           TestStatisticsErrorLabel.setVisible(true);
                       }
                       else
                           TestStatisticsErrorLabel.setVisible(false);
                   }
                   else
                       TestStatisticsErrorLabel.setVisible(false);
               }
               catch (NumberFormatException e) {
                   ok = false;
                   TestStatisticsErrorLabel.setVisible(true);
               }
               return ok;
         }
     }     
    
    class DOF1FieldVerifier extends InputVerifier {
         public boolean verify(JComponent input) {
 	       JTextField tf = (JTextField) input;
               boolean ok = true;
 	       try {
                   int dof = Integer.parseInt(tf.getText());
                   DOF1ErrorLabel.setVisible(false);
                   if (dof <= 0) {
                        ok = false;
                        DOF1ErrorLabel.setVisible(true);
                   }
               }
               catch (NumberFormatException e) {
                   ok = false;
                   DOF1ErrorLabel.setVisible(true);
               }
               return ok;
         }
     }  
    
     class DOF2FieldVerifier extends InputVerifier {
         public boolean verify(JComponent input) {
 	       JTextField tf = (JTextField) input;
               boolean ok = true;
 	       try {
                   int dof = Integer.parseInt(tf.getText());
                   DOF2ErrorLabel.setVisible(false);
                   if (dof <= 0) {
                        ok = false;
                        DOF2ErrorLabel.setVisible(true);
                   }
               }
               catch (NumberFormatException e) {
                   ok = false;
                   DOF2ErrorLabel.setVisible(true);
               }
               return ok;
         }
     }    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ClearButton;
    private javax.swing.JTextField DOF1Box;
    private javax.swing.JLabel DOF1ErrorLabel;
    private javax.swing.JLabel DOF1Label;
    private javax.swing.JTextField DOF2Box;
    private javax.swing.JLabel DOF2ErrorLabel;
    private javax.swing.JLabel DOF2Label;
    private javax.swing.JLabel DistributionLabel;
    private javax.swing.JComboBox DistributionMenu;
    private javax.swing.JPanel InputsPanel;
    private javax.swing.JButton OKButton;
    private javax.swing.JPanel ResultsPanel;
    private javax.swing.JLabel ResultsText;
    private javax.swing.JTextField TestStatisticsBox;
    private javax.swing.JLabel TestStatisticsErrorLabel;
    private javax.swing.JLabel TestStatisticsLabel;
    private javax.swing.JLabel TestTypeLabel;
    private javax.swing.JComboBox TestTypeMenu;
    private javax.swing.JLabel TitleLabel;
    // End of variables declaration//GEN-END:variables
    
}
