/*
 * NormalityTestDialog.java
 *
 * Created on November 25, 2008, 9:09 AM
 */

package org.statcato.dialogs.stat.basicstats;

import org.statcato.*;
import org.statcato.spreadsheet.*;
import org.statcato.utils.HelperFunctions;
import org.statcato.graph.*;
import org.statcato.statistics.NormalityTest;

import java.util.*;
import javax.swing.*;

import org.jfree.chart.*;


/**
 *
 * @author  Margaret Yau
 */
public class NormalityTestDialog extends StatcatoDialog {
    
    /** Creates new form NormalityTestDialog */
    public NormalityTestDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.app = (Statcato) parent;
        ParentSpreadsheet = app.getSpreadsheet();
        initComponents();
        customInitComponents();
        setHelpFile("stat-basic-normality");
        name = "Normality Test";
        description = "For performing the Ryan-Joiner normality test and " +
                "creating a normal quantile plot for a set of sample data values. ";
        helpStrings.add("Select the column containing the data values in " +
                "the Input Variable drop-down menu.");
        helpStrings.add("Select the significance level used in the Ryan-Joiner " +
                "normality test.");
        helpStrings.add("Select the Generate normal quantile plot check box " +
                "if a normal quantile plot for the input data should be displayed.");
        pack();
    }
    
     /**
     * 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(ColComboBox);       
    }
    
    private void customInitComponents() {
        ParentSpreadsheet.populateComboBox(ColComboBox);
        ButtonGroup group = new ButtonGroup();
        group.add(DataRadioButton);
        group.add(ZscoresRadioButton);
        getRootPane().setDefaultButton(OKButton);
    }
    
    /** 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() {

        jPanel1 = new javax.swing.JPanel();
        ColComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        PlotPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        yTextField = new javax.swing.JTextField();
        TitleTextField = new javax.swing.JTextField();
        xTextField = new javax.swing.JTextField();
        LineCheckBox = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        DataRadioButton = new javax.swing.JRadioButton();
        ZscoresRadioButton = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        SigComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        NormalPlotCheckBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Normality Test");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input Variable"));

        ColComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ColComboBoxItemStateChanged(evt);
            }
        });

        jLabel1.setText("Select the column containing sample data:");

        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)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(ColComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ColComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        PlotPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Graph Options"));

        jLabel2.setText("Plot Title:");

        TitleTextField.setText("Normal Quantile Plot");

        LineCheckBox.setSelected(true);
        LineCheckBox.setText("Show regression line");

        jLabel4.setText("Y-axis Label:");

        jLabel3.setText("X-axis Label:");

        jLabel5.setText("X-axis values represent:");

        DataRadioButton.setSelected(true);
        DataRadioButton.setText("data");

        ZscoresRadioButton.setText("z-scores");

        javax.swing.GroupLayout PlotPanelLayout = new javax.swing.GroupLayout(PlotPanel);
        PlotPanel.setLayout(PlotPanelLayout);
        PlotPanelLayout.setHorizontalGroup(
            PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PlotPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(PlotPanelLayout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(xTextField))
                        .addGroup(PlotPanelLayout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(TitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(PlotPanelLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(yTextField)))
                    .addGroup(PlotPanelLayout.createSequentialGroup()
                        .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LineCheckBox)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(DataRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ZscoresRadioButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        PlotPanelLayout.setVerticalGroup(
            PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PlotPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(TitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(xTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(yTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(LineCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ZscoresRadioButton)
                    .addGroup(PlotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(DataRadioButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Normality Test (Ryan-Joiner)"));

        SigComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0.01", "0.05", "0.10" }));

        jLabel6.setText("Significance Level:");

        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()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(SigComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(64, 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(jLabel6)
                    .addComponent(SigComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

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

        NormalPlotCheckBox.setSelected(true);
        NormalPlotCheckBox.setText("Generate normal quantile plot");
        NormalPlotCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                NormalPlotCheckBoxStateChanged(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()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PlotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NormalPlotCheckBox)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(214, 214, 214)
                        .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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NormalPlotCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PlotPanel, 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(OKButton)
                    .addComponent(CancelButton))
                .addGap(20, 20, 20))
        );

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

    private void ColComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ColComboBoxItemStateChanged

    }//GEN-LAST:event_ColComboBoxItemStateChanged

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("normality test");
        
        String heading = "Normality Test";
        
        if (ColComboBox.getSelectedIndex() == 0) {
            app.showErrorDialog("Select the column containing the " +
                    "values to be plotted.");
            return;
        }
        
        String yCol = (String)ColComboBox.getSelectedItem();
        int yColumn = ParentSpreadsheet.parseColumnNumber(yCol);
        
        Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(yColumn);
        Vector<Double> YColumnVector =
                HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
        if (YColumnVector == null) {
            app.showErrorDialog("Invalid input column " + yCol +
                    ": all data must be numbers.");
            return;
        }
        YColumnVector = HelperFunctions.removeNullValues(YColumnVector);
        String text = "Input variable: " + yCol + "<br>";
        
        // normality test results
        double significance = Double.parseDouble((String)SigComboBox.getSelectedItem());
        
        NormalityTest normalityTest = new NormalityTest(YColumnVector, significance);
        text += normalityTest;
        
        app.addLogParagraph(heading, text);

        if (NormalPlotCheckBox.isSelected())
            CreatePlot(YColumnVector);
        
        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
                        
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CreatePlot(Vector<Double> YColumnVector) {
        JFreeChart plot = GraphFactory.createNormalQuantilePlot(YColumnVector,
            TitleTextField.getText(), xTextField.getText(), yTextField.getText(),
            DataRadioButton.isSelected(), LineCheckBox.isSelected());
 
        StatcatoChartFrame frame = 
                new StatcatoChartFrame(TitleTextField.getText(), plot, app);
        frame.pack();
        frame.setVisible(true);    
                
    }
    
    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed

    private void NormalPlotCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_NormalPlotCheckBoxStateChanged
        if (NormalPlotCheckBox.isSelected()) {
            PlotPanel.setVisible(true);
            pack();
        }
        else {
            PlotPanel.setVisible(false);
            pack();
        }
    }//GEN-LAST:event_NormalPlotCheckBoxStateChanged

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JComboBox ColComboBox;
    private javax.swing.JRadioButton DataRadioButton;
    private javax.swing.JCheckBox LineCheckBox;
    private javax.swing.JCheckBox NormalPlotCheckBox;
    private javax.swing.JButton OKButton;
    private javax.swing.JPanel PlotPanel;
    private javax.swing.JComboBox SigComboBox;
    private javax.swing.JTextField TitleTextField;
    private javax.swing.JRadioButton ZscoresRadioButton;
    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;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField xTextField;
    private javax.swing.JTextField yTextField;
    // End of variables declaration//GEN-END:variables
    
}
