/*
 * FrequencyTableDialog.java
 *
 * Created on August 1, 2008, 10:05 AM
 */

package org.statcato.dialogs.calc;

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

/**
 * A dialog for creating a frequency table for a given set of data.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class FrequencyTableDialog extends StatcatoDialog {

    /** Creates new form FrequencyTableDialog */
    public FrequencyTableDialog(java.awt.Frame parent, boolean modal,
            Statcato app) {
        super(parent, modal);
        this.app = app;
        ParentSpreadsheet = app.getSpreadsheet();
        initComponents();
        customInitComponents();
    }
    
    private void customInitComponents() {
        ParentSpreadsheet.populateComboBox(SourceComboBox);
        ParentSpreadsheet.populateComboBox(LimitComboBox);
        
        ButtonGroup group = new ButtonGroup();
        group.add(CatRadioButton);
        group.add(ClassRadioButton);
        group.add(Class2RadioButton);
        group.add(AutoRadioButton);
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("calc-freqtable");
        name = "Frequency Table";
        description = "For computing frqeuencies of data values.";
        helpStrings.add("Select the column containing data for which frequencies" +
                " will be computed.");
        helpStrings.add("Select the method for computing frequency data.");
        helpStrings.add("Select the columns in which the frequency results " +
                "are to be stored.");
    }

    /**
     * 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(SourceComboBox);
        updateComboBox(LimitComboBox);
    }
    
    /** 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();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        SourceComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        NumClassTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Class2RadioButton = new javax.swing.JRadioButton();
        jLabel11 = new javax.swing.JLabel();
        FirstLimitTextField = new javax.swing.JTextField();
        ClassWidthTextField = new javax.swing.JTextField();
        CatRadioButton = new javax.swing.JRadioButton();
        LimitComboBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        ClassRadioButton = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        AutoRadioButton = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        FrequencyTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        CategoryTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Compute Frequency Table");

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

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

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Source Data");

        jLabel1.setText("Compute frequency data for column:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(SourceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(4, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SourceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Computing Frequencies"));

        NumClassTextField.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Method of Computing Frequencies");

        Class2RadioButton.setText("Source data contains numerical data.");
        Class2RadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                Class2RadioButtonStateChanged(evt);
            }
        });

        jLabel11.setText("(optional)");

        FirstLimitTextField.setEnabled(false);

        ClassWidthTextField.setEnabled(false);

        CatRadioButton.setText("Treat source data as categories.  Count the frequency of each category.");
        CatRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                CatRadioButtonItemStateChanged(evt);
            }
        });

        LimitComboBox.setEnabled(false);

        jLabel9.setText("Class width:");

        jLabel10.setText("Number of classes:");

        ClassRadioButton.setText("<html>Source data contains numerical data.  Count the frequency of each class, <br>where the upper class limits are in column:</html>");
        ClassRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ClassRadioButtonItemStateChanged(evt);
            }
        });

        jLabel8.setText("First upper class limit:");

        AutoRadioButton.setSelected(true);
        AutoRadioButton.setText("<html>Source data contains numerical data.  Automatically use 10 classes, <br>with the first lower class limit decided by the minimum value.\n");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ClassRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(AutoRadioButton)
                            .addComponent(jLabel2)
                            .addComponent(CatRadioButton)
                            .addComponent(Class2RadioButton)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(29, 29, 29)
                                        .addComponent(jLabel8))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(45, 45, 45)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel10)
                                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(FirstLimitTextField)
                                    .addComponent(ClassWidthTextField)
                                    .addComponent(NumClassTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE))
                                .addGap(10, 10, 10)
                                .addComponent(jLabel11))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(LimitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CatRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ClassRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(LimitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(Class2RadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(FirstLimitTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(ClassWidthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(NumClassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(AutoRadioButton))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Results"));

        jLabel6.setText("Frequency column:");

        jLabel5.setText("Category column:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Store Frequency Table in");

        jLabel7.setText("e.g. C1 or variable name");

        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()
                        .addGap(4, 4, 4)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(FrequencyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel4)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(CategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(FrequencyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addContainerGap(14, 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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, 0, 203, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(255, Short.MAX_VALUE)
                .addComponent(OKButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CancelButton)
                .addGap(253, 253, 253))
        );

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

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

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

    @SuppressWarnings("unchecked") 
    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("frequency table");
        
        // check for incomplete input data
        if (SourceComboBox.getSelectedIndex() == 0) {
            app.showErrorDialog("Select the column containing the source data.");
            return;
        }
        if (ClassRadioButton.isSelected() && LimitComboBox.getSelectedIndex() == 0) {
            app.showErrorDialog("Select the column containing upper class limits.");
            return;
        }
        
        // get data from source column
        String source = SourceComboBox.getSelectedItem().toString();
        int sourceColumn = ParentSpreadsheet.parseColumnNumber(source);
        String text = "";
        text += "Source column: " + source + "<br>";
        
        Vector<Cell> CellColumnVector = null;
        Vector<Double> ColumnVector = null;
        CellColumnVector = ParentSpreadsheet.getColumn(sourceColumn);    
        CellColumnVector = HelperFunctions.removeNullCells(CellColumnVector);
        if (ClassRadioButton.isSelected() || Class2RadioButton.isSelected() ||
                AutoRadioButton.isSelected()) {
            // check that input column contain only numerical data
            ColumnVector = HelperFunctions.ConvertInputVectorToDoubles(CellColumnVector);
            if (ColumnVector == null) {
                app.showErrorDialog("Invalid input column " + source +
                        ": all data must be numbers.");
                return;
            }
        }
        int storeCatColumnNum, storeFreqColumnNum;
        String StoreCatColumn = CategoryTextField.getText();
        // check if Column is valid
        storeCatColumnNum = ParentSpreadsheet.getColumnNumber(StoreCatColumn);
        if (storeCatColumnNum == -1) { // invalid column
            app.showErrorDialog("Enter a valid column name (e.g. C1) or a " +
                    "valid variable name to store the categories of the " +
                    "frequency table.");
            return;
        }        
        String StoreFreqColumn = FrequencyTextField.getText();
        // check if column is valid
        storeFreqColumnNum = ParentSpreadsheet.getColumnNumber(StoreFreqColumn);
        if (storeFreqColumnNum == -1) { // invalid column
            app.showErrorDialog("Enter a valid column name (e.g. C1) or a " +
                    "valid variable name to store the frequencies of the " +
                    "frequency table.");
            return;
        }       
        text += "Category column: " + StoreCatColumn + "<br>";
        text += "Frequency column: " + StoreFreqColumn;
        
        // category data
        if (CatRadioButton.isSelected()) {
            Object[] returnValues = 
                    HelperFunctions.ComputeCategoryFrequency(CellColumnVector);
            Vector<Cell> categories = (Vector<Cell>)returnValues[0];
            Vector<Double> frequencies = (Vector<Double>)returnValues[1];
            
            ParentSpreadsheet.setCellColumn(storeCatColumnNum, categories);
            ParentSpreadsheet.setColumn(storeFreqColumnNum, 
                    HelperFunctions.ConvertDoubleVectorToString(frequencies));
        }
        // numerical data, upper class limits provided
        else if (ClassRadioButton.isSelected()) {
            // get class limits column
            String classes = LimitComboBox.getSelectedItem().toString();
            int classesColumn = ParentSpreadsheet.parseColumnNumber(classes);
            // read class limits vector
            Vector<Cell> LimitsColumnVector;
            Vector<Double> LimitsVector;
            LimitsColumnVector = ParentSpreadsheet.getColumn(classesColumn);    
            LimitsColumnVector = HelperFunctions.removeNullCells(LimitsColumnVector);            
            LimitsVector = 
                    HelperFunctions.ConvertInputVectorToDoubles(LimitsColumnVector);
            if (LimitsVector == null) {
                app.showErrorDialog("Invalid input column " + source +
                        " for class limits: all data must be numbers.");
                return;
            }
            Vector<Double> frequencies = HelperFunctions.ComputeClassFrequency(
                    ColumnVector, LimitsVector);
            Vector<String> limits = 
                    HelperFunctions.ConvertDoubleVectorToString(LimitsVector);
            limits.addElement("+");
            ParentSpreadsheet.setColumn(storeCatColumnNum, limits);
            ParentSpreadsheet.setColumn(storeFreqColumnNum, 
                    HelperFunctions.ConvertDoubleVectorToString(frequencies));
        }
        // numerical data, first lower class limit and class width provided
        else if (Class2RadioButton.isSelected()) {
            try {
                double limit1 = Double.parseDouble(FirstLimitTextField.getText());
                double width = Double.parseDouble(ClassWidthTextField.getText());
                double max = BasicStatistics.max(ColumnVector);
                int numClasses = -1;
                if (!NumClassTextField.getText().equals("")) {
                    try {
                        numClasses = Integer.parseInt(NumClassTextField.getText());
                        if (numClasses <= 0) 
                            throw new NumberFormatException();
                    }
                    catch (NumberFormatException e) {
                        app.showErrorDialog("Enter a positive integer for the " +
                                "number of classes.");
                        return;
                    }
                }

                // construct limits vector
                Vector<Double> LimitsVector = new Vector<Double>();
                double thisLimit = limit1;
                int classes = 0;
                LimitsVector.addElement(thisLimit);
                classes++;
                while (thisLimit < max && (numClasses == -1 || classes < numClasses)) {
                    thisLimit += width;
                    LimitsVector.addElement(thisLimit);                    
                    classes++;
                }
                
                Vector<Double> frequencies = HelperFunctions.ComputeClassFrequency(
                    ColumnVector, LimitsVector);
                frequencies.removeElementAt(frequencies.size() - 1);
                Vector<String> limits = 
                        HelperFunctions.ConvertDoubleVectorToString(LimitsVector);
                ParentSpreadsheet.setColumn(storeCatColumnNum, limits);
                ParentSpreadsheet.setColumn(storeFreqColumnNum, 
                        HelperFunctions.ConvertDoubleVectorToString(frequencies));
            }
            catch (NumberFormatException e) {
                app.showErrorDialog("Enter valid numbers for the first" +
                        " upper class limit and the class width.");
                return;
            } 
        }
        // numerical data, automatically using 10 classes, with the first
        // lower class limit decided by the minimum value
        else {
            double numClasses = 10;
            double min = BasicStatistics.min(ColumnVector);
            double max = BasicStatistics.max(ColumnVector);
            double width = (max - min) / numClasses;
            // construct limits vector
            Vector<Double> LimitsVector = new Vector<Double>();
            for (int i = 1; i <= numClasses; ++i) {
                LimitsVector.addElement(min + i * width);
            }

            Vector<Double> frequencies = HelperFunctions.ComputeClassFrequency(
                    ColumnVector, LimitsVector);
            frequencies.removeElementAt(frequencies.size() - 1);
            Vector<String> limits =
                    HelperFunctions.ConvertDoubleVectorToString(LimitsVector);
            ParentSpreadsheet.setColumn(storeCatColumnNum, limits);
            ParentSpreadsheet.setColumn(storeFreqColumnNum,
                    HelperFunctions.ConvertDoubleVectorToString(frequencies));
        }
        
        app.addLogParagraph("Frequency Table", 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 CatRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_CatRadioButtonItemStateChanged
        if (CatRadioButton.isSelected()) {
            //LimitComboBox.setEnabled(false);
        }
        else {
            //LimitComboBox.setEnabled(true);
        }
    }//GEN-LAST:event_CatRadioButtonItemStateChanged

    private void ClassRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ClassRadioButtonItemStateChanged
        if (ClassRadioButton.isSelected()) {
            LimitComboBox.setEnabled(true);
        }
        else {
            LimitComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_ClassRadioButtonItemStateChanged

    private void Class2RadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_Class2RadioButtonStateChanged
        if (Class2RadioButton.isSelected()) {
            FirstLimitTextField.setEnabled(true);
            ClassWidthTextField.setEnabled(true);
            NumClassTextField.setEnabled(true);
        }
        else {
            FirstLimitTextField.setEnabled(false);
            ClassWidthTextField.setEnabled(false);
            NumClassTextField.setEnabled(false);
        }
    }//GEN-LAST:event_Class2RadioButtonStateChanged

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton AutoRadioButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JRadioButton CatRadioButton;
    private javax.swing.JTextField CategoryTextField;
    private javax.swing.JRadioButton Class2RadioButton;
    private javax.swing.JRadioButton ClassRadioButton;
    private javax.swing.JTextField ClassWidthTextField;
    private javax.swing.JTextField FirstLimitTextField;
    private javax.swing.JTextField FrequencyTextField;
    private javax.swing.JComboBox LimitComboBox;
    private javax.swing.JTextField NumClassTextField;
    private javax.swing.JButton OKButton;
    private javax.swing.JComboBox SourceComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    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.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables
    
}
