/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SignTestDialog.java
 *
 * Created on Oct 14, 2009, 3:24:17 PM
 */

package org.statcato.dialogs.stat.nonparametrics;

import org.statcato.statistics.inferential.nonparametrics.SignTest;
import org.statcato.StatcatoDialog;
import org.statcato.Statcato;
import org.statcato.DialogEdit;
import org.statcato.spreadsheet.Cell;
import org.statcato.utils.HelperFunctions;
import java.util.Vector;
import javax.swing.ButtonGroup;

/**
 * A dialog for the sign test (distribution-free, one-sample).
 * Allows the user to specify data samples in a column of a Datasheet,
 * or provide summary data values.  The user must also provide
 * the alternative hypothesis and the hypothesized median.
 * Computes and displays in log window the number of positive and
 * negative signs as well as the p-Value.
 *
 * @version %I%, %G%
 * @see org.statcato.statistics.inferential.nonparametrics.SignTest
 * @since 1.0
 * @author Margaret Yau
 */
public class SignTestDialog extends StatcatoDialog {

    /** Creates new form SignTestDialog */
    public SignTestDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        app = (Statcato)parent;
        ParentSpreadsheet = app.getSpreadsheet();

        ParentSpreadsheet.populateComboBox(ColumnComboBox);

        ButtonGroup group = new ButtonGroup();
        group.add(SampleRadioButton);
        group.add(SummaryRadioButton);

        getRootPane().setDefaultButton(OKButton);
        setHelpFile("stat-nonparam-signtest");
        name = "1-Sample Sign Test";
        description = "For performing the sign test.";
        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 positive and negative signs " +
                "in the provided text fields.");
        helpStrings.add("Provide the alternative hypothesis.");
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        NumPositiveTextField = new javax.swing.JTextField();
        ColumnComboBox = new javax.swing.JComboBox();
        PositiveLabel = new javax.swing.JLabel();
        SampleRadioButton = new javax.swing.JRadioButton();
        SummaryRadioButton = new javax.swing.JRadioButton();
        NegativeLabel = new javax.swing.JLabel();
        NumNegativeTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        AltComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        HyMedianTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("1-Sample Sign Test");

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

        NumPositiveTextField.setEnabled(false);

        PositiveLabel.setText("Number of positive (+):");
        PositiveLabel.setEnabled(false);

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

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

        NegativeLabel.setText("Number of negative (-):");
        NegativeLabel.setEnabled(false);

        NumNegativeTextField.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGap(21, 21, 21)
                            .addComponent(ColumnComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(SampleRadioButton, javax.swing.GroupLayout.Alignment.LEADING))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(PositiveLabel)
                        .addComponent(NegativeLabel)
                        .addComponent(SummaryRadioButton)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(NumPositiveTextField)
                    .addComponent(NumNegativeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.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))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(SummaryRadioButton)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NegativeLabel)
                    .addComponent(NumNegativeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PositiveLabel)
                    .addComponent(NumPositiveTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

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

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

        jLabel3.setText("Alternative hypothesis:");

        jLabel1.setText("Hypothesized median:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(HyMedianTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(AltComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.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))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(HyMedianTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );

        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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(OKButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelButton)))
                .addContainerGap())
        );

        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()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addContainerGap(21, Short.MAX_VALUE))
        );

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

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

    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 SummaryRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SummaryRadioButtonStateChanged
        if (SummaryRadioButton.isSelected()) {
            PositiveLabel.setEnabled(true);
            NegativeLabel.setEnabled(true);
            NumPositiveTextField.setEnabled(true);
            NumNegativeTextField.setEnabled(true);
        } else  {
            PositiveLabel.setEnabled(false);
            NegativeLabel.setEnabled(false);
            NumPositiveTextField.setEnabled(false);
            NumNegativeTextField.setEnabled(false);
        }
}//GEN-LAST:event_SummaryRadioButtonStateChanged

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("sign test");

        int typeTest;
        double median;

        Vector<Double> ColumnVector = new Vector<Double>();

        String heading = "Sign Test:";
        String text = "";

        // get type of test
        typeTest = AltComboBox.getSelectedIndex();

        // get hypothesized median
        try {
            median = Double.parseDouble(HyMedianTextField.getText());
        } catch (NumberFormatException e) {
            app.showErrorDialog("Enter a valid number for the hypothesized median.");
            return;
        }

        SignTest st;

        // 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);
            ColumnVector = HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
            ColumnVector = HelperFunctions.removeNullValues(ColumnVector);
            if (ColumnVector == null) {
                app.showErrorDialog("Invalid input column " + columnLabel +
                        ": all data must be numbers.");
                return;
            }
            text += "Input: " + columnLabel + "<br>";
            st = new SignTest(ColumnVector, median, typeTest);
        } else {
            int numPositive = 0, numNegative = 0;
            // get summary data
            try {
                numPositive = Integer.parseInt(NumPositiveTextField.getText());
                numNegative = Integer.parseInt(NumNegativeTextField.getText());
            } catch (NumberFormatException e) {
                app.showErrorDialog("Invalid summary data.");
                return;
            }
            text += "Input: Summary data" + "<br>";
            st = new SignTest(numPositive, numNegative, median, typeTest);
        }

        // outputs
        text += "H<sub>0</sub>: median = " + median + "<br>";
        text += "H<sub>1</sub>: median ";
        if (typeTest == SignTest.LESS)
            text += " &lt; ";
        else if (typeTest == SignTest.GREATER)
            text += " &gt; ";
        else
            text += " &ne; ";
        text += median + "<br>";
        text += st;

        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


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AltComboBox;
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox ColumnComboBox;
    private javax.swing.JTextField HyMedianTextField;
    private javax.swing.JLabel NegativeLabel;
    private javax.swing.JTextField NumNegativeTextField;
    private javax.swing.JTextField NumPositiveTextField;
    private javax.swing.JButton OKButton;
    private javax.swing.JLabel PositiveLabel;
    private javax.swing.JRadioButton SampleRadioButton;
    private javax.swing.JRadioButton SummaryRadioButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables

}
