/*
 * ScatterplotDialog.java
 *
 * Created on June 26, 2008, 9:30 AM
 */

package org.statcato.dialogs.graph;

import org.statcato.graph.StatcatoChartFrame;
import org.statcato.*;
import org.statcato.spreadsheet.*;
import org.statcato.utils.HelperFunctions;
import org.statcato.graph.GraphFactory;

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

import org.jfree.chart.*;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


/**
 * A dialog for creating scatterplots.  Allows the user to specify the
 * data series to be graphed, axis labels, title,
 * and whether a legend and a regression line are displayed.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @see org.jfree.chart
 * @since 1.0
 */
public class ScatterplotDialog extends StatcatoDialog {
    /** Creates new form ScatterplotDialog */
    public ScatterplotDialog(java.awt.Frame parent, boolean modal, Statcato app) {
        super(parent, modal);
        this.app = app;
        ParentSpreadsheet = app.getSpreadsheet();
 
        initComponents();
        customInitComponents();
        setHelpFile("graph-scatterplot");
        name = "Scatterplot";
        description = "For creating scatterplots showing a collection of " +
                "points on an x-y plane in cartesian coordinates.";
        helpStrings.add("To add a series of (x, y) points to the plot, " +
                "select the column variable containing the x coordinates " +
                "and the column variable containing the y coordinates," +
                "then click the Add Series button. ");
        helpStrings.add("Enter a plot title and labels for the x and y axis " +
                "in the corresponding text fields.");
        helpStrings.add("Select whether to show a legend and a regression line.");
        
        pack();
    }
    
    private void customInitComponents() {
        ParentSpreadsheet.populateComboBox(XComboBox);
        ParentSpreadsheet.populateComboBox(YComboBox);
        SeriesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        ButtonGroup group = new ButtonGroup();
        group.add(LinRegRadioButton);
        group.add(QuadRegRadioButton);
        group.add(CubicRegRadioButton);
        group.add(PolyRegRadioButton);
        group.add(LogRegRadioButton);
        group.add(PowRegRadioButton);
        group.add(FixedPowRegRadioButton);
        group.add(ExpRegRadioButton);
        
        getRootPane().setDefaultButton(OKButton);
        
    }

    /**
     * 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(XComboBox);
        updateComboBox(YComboBox);        
        clearMutableColumnsList(SeriesList);
    }
    
    /** 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();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        SeriesList = new JList(new DefaultListModel());
        jLabel4 = new javax.swing.JLabel();
        RemoveButton = new javax.swing.JButton();
        YComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        XComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        AddButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        LegendCheckBox = new javax.swing.JCheckBox();
        YTextField = new javax.swing.JTextField();
        TitleTextField = new javax.swing.JTextField();
        XTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        LineCheckBox = new javax.swing.JCheckBox();
        RegPanel = new javax.swing.JPanel();
        LinRegRadioButton = new javax.swing.JRadioButton();
        QuadRegRadioButton = new javax.swing.JRadioButton();
        CubicRegRadioButton = new javax.swing.JRadioButton();
        PolyRegRadioButton = new javax.swing.JRadioButton();
        jLabel9 = new javax.swing.JLabel();
        PolyTextField = new javax.swing.JTextField();
        LogRegRadioButton = new javax.swing.JRadioButton();
        PowRegRadioButton = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        PolyTextField2 = new javax.swing.JTextField();
        FixedPowRegRadioButton = new javax.swing.JRadioButton();
        ExpRegRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Scatterplot");

        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("Graph Variables"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Graph X,Y Series");

        SeriesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                SeriesListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(SeriesList);

        jLabel4.setText("y variable:");

        RemoveButton.setText("Remove Series");
        RemoveButton.setEnabled(false);
        RemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveButtonActionPerformed(evt);
            }
        });

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

        jLabel2.setText("Select the X and Y variable of a new series:");

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

        jLabel3.setText("X variable:");

        AddButton.setText("Add Series");
        AddButton.setEnabled(false);
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        jLabel8.setText("Select the series to be removed:");

        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.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(RemoveButton)
                            .addComponent(jLabel8)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(YComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(XComboBox, 0, 107, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(AddButton))
                            .addComponent(jLabel2)))
                    .addComponent(jLabel1))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(XComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(YComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(AddButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RemoveButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                .addContainerGap())
        );

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

        jLabel6.setText("Plot Title:");

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

        LegendCheckBox.setSelected(true);
        LegendCheckBox.setText("Show legend");

        TitleTextField.setText("Scatterplot");

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

        LineCheckBox.setSelected(true);
        LineCheckBox.setText("Show regression curve");
        LineCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LineCheckBoxStateChanged(evt);
            }
        });

        LinRegRadioButton.setSelected(true);
        LinRegRadioButton.setText("Linear: y = ax+b");

        QuadRegRadioButton.setText("<html>Quadratic: y = ax<sup>2</sup> + bx + c</html>");

        CubicRegRadioButton.setText("<html>Cubic: y=ax<sup>3</sup> + bx<sup>2</sup> + cx + d</html>");

        PolyRegRadioButton.setText("<html>Polynomial: y = ax<sup>n</sup> + bx<sup>n-1</sup>+ cx<sup>n-2</sup> + ...</html>");

        jLabel9.setText("n = ");

        PolyTextField.setText("4");
        PolyTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PolyTextFieldFocusGained(evt);
            }
        });

        LogRegRadioButton.setText("Logarithmic: y = a lnx + b");

        PowRegRadioButton.setText("<html>Power: y = a x<sup>b</sup></html>");

        jLabel10.setText("n = ");

        PolyTextField2.setText("-1");
        PolyTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PolyTextField2FocusGained(evt);
            }
        });

        FixedPowRegRadioButton.setText("<html>Fixed Power: y = a x<sup>n</sup></html>");

        ExpRegRadioButton.setText("<html>Exponential: y = a b<sup>x</sup></html>");

        javax.swing.GroupLayout RegPanelLayout = new javax.swing.GroupLayout(RegPanel);
        RegPanel.setLayout(RegPanelLayout);
        RegPanelLayout.setHorizontalGroup(
            RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegPanelLayout.createSequentialGroup()
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LinRegRadioButton)
                    .addComponent(QuadRegRadioButton)
                    .addComponent(CubicRegRadioButton)
                    .addComponent(PolyRegRadioButton))
                .addGap(27, 27, 27)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PowRegRadioButton)
                    .addComponent(LogRegRadioButton)
                    .addComponent(FixedPowRegRadioButton)
                    .addComponent(ExpRegRadioButton))
                .addGap(69, 69, 69))
            .addGroup(RegPanelLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(PolyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(198, 198, 198)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PolyTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(128, 128, 128))
        );
        RegPanelLayout.setVerticalGroup(
            RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RegPanelLayout.createSequentialGroup()
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LinRegRadioButton)
                    .addComponent(LogRegRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(QuadRegRadioButton)
                    .addComponent(PowRegRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CubicRegRadioButton)
                    .addComponent(ExpRegRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PolyRegRadioButton)
                    .addComponent(FixedPowRegRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(RegPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(PolyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(PolyTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        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, false)
                            .addComponent(LineCheckBox)
                            .addComponent(LegendCheckBox)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(YTextField)
                                    .addComponent(TitleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(XTextField))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(RegPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 424, 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()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(XTextField, 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(jLabel5)
                    .addComponent(YTextField, 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(jLabel6)
                    .addComponent(TitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LegendCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LineCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(RegPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        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()
                        .addGap(176, 176, 176)
                        .addComponent(OKButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CancelButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, 0, 475, Short.MAX_VALUE))))
                .addContainerGap())
        );

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

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(4, 4, 4)
                .addComponent(jPanel2, 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(CancelButton)
                    .addComponent(OKButton))
                .addGap(21, 21, 21))
        );

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

    private void XComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_XComboBoxItemStateChanged
        if (XComboBox.getSelectedIndex() == 0 || YComboBox.getSelectedIndex() == 0)
            AddButton.setEnabled(false);
        else
            AddButton.setEnabled(true);
    }//GEN-LAST:event_XComboBoxItemStateChanged

    private void YComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_YComboBoxItemStateChanged
        if (XComboBox.getSelectedIndex() == 0 || YComboBox.getSelectedIndex() == 0)
            AddButton.setEnabled(false);
        else
            AddButton.setEnabled(true);
    }//GEN-LAST:event_YComboBoxItemStateChanged

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        // add selected series to list
        if (XComboBox.getSelectedIndex() == 0) {
            app.showErrorDialog("Select x variable of the new series.");
            return;
        }
        if (YComboBox.getSelectedIndex() == 0) {
            app.showErrorDialog("Select y variable of the new series.");
            return;
        }
        String x = XComboBox.getSelectedItem().toString();
        String y = YComboBox.getSelectedItem().toString();
        ((DefaultListModel)SeriesList.getModel()).addElement(x.trim()+","+y.trim());
    }//GEN-LAST:event_AddButtonActionPerformed

    private void SeriesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_SeriesListValueChanged
        if (SeriesList.getSelectedIndex() != -1)
            RemoveButton.setEnabled(true);
        else
            RemoveButton.setEnabled(false);
    }//GEN-LAST:event_SeriesListValueChanged

    private void RemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveButtonActionPerformed
        // remove selected series from list
        if (SeriesList.getSelectedValue() != null) {
            ((DefaultListModel)SeriesList.getModel()).removeElementAt(
                SeriesList.getSelectedIndex());
        }
    }//GEN-LAST:event_RemoveButtonActionPerformed

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("scatterplot");
        
        XYSeriesCollection seriesCollection = new XYSeriesCollection();
        double min = 0, max = 0;
        
        if (SeriesList.getModel().getSize() == 0) {
            app.showErrorDialog("Add at least one series.");
            return;
        }
        
        String text = "";
        
        for (int i = 0; i < SeriesList.getModel().getSize(); ++i) {
            String xy = (String) SeriesList.getModel().getElementAt(i);
            text += "Series: " + xy + "<br>";
            String[] items = xy.split(",");
            int selectedXColumn = ParentSpreadsheet.parseColumnNumber(items[0]);
            int selectedYColumn = ParentSpreadsheet.parseColumnNumber(items[1]);
            
            Vector<Cell> StrColumnVector = ParentSpreadsheet.getColumn(selectedXColumn);
            // get x input column of doubles
            Vector<Double> XColumnVector = 
                    HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
            
            if (XColumnVector == null) {
                app.showErrorDialog("Invalid input column " + items[0] +
                        ": all data must be numbers.");
                return;
            }
            XColumnVector = HelperFunctions.removeNullValues(XColumnVector);
            
            StrColumnVector = ParentSpreadsheet.getColumn(selectedYColumn);
            // get x input column of doubles
            Vector<Double> YColumnVector = 
                    HelperFunctions.ConvertInputVectorToDoubles(StrColumnVector);
            
            if (YColumnVector == null) {
                app.showErrorDialog("Invalid input column " + items[1] +
                        ": all data must be numbers.");
                return;
            }
            YColumnVector = HelperFunctions.removeNullValues(YColumnVector);
            
            if (XColumnVector.size() != YColumnVector.size()) {
                app.showErrorDialog("The number of x values must be the " +
                        "same as the number of y values.");
                return;
            }
            
            // create new series
            XYSeries series = new XYSeries(xy);
            for (int j = 0; j < XColumnVector.size(); ++j) {
                double x = XColumnVector.elementAt(j).doubleValue();
                if (x < min)
                    min = x;
                if (x > max)
                    max = x;
                series.add(x, 
                        YColumnVector.elementAt(j).doubleValue());
            }
            seriesCollection.addSeries(series);
        }

        int regressionType = GraphFactory.REG_NONE;
        double regressionArg = 0;

        if (LineCheckBox.isSelected()) {
            if (LinRegRadioButton.isSelected())
                regressionType = GraphFactory.REG_LIN;
            else if (QuadRegRadioButton.isSelected())
                regressionType = GraphFactory.REG_QUAD;
            else if (CubicRegRadioButton.isSelected())
                regressionType = GraphFactory.REG_CUBIC;
            else if (PolyRegRadioButton.isSelected()) {
                regressionType = GraphFactory.REG_POLY;
                try {
                    int n = Integer.parseInt(PolyTextField.getText());
                    if (n <= 0)
                        throw new NumberFormatException();
                    else
                        regressionArg = n;
                } catch (NumberFormatException e) {
                    app.showErrorDialog("Enter a positive integer for the degree" +
                            " of the polynomial model");
                    return;
                }
            }
            else if (LogRegRadioButton.isSelected()) {
                regressionType = GraphFactory.REG_LOG;
            }
            else if (PowRegRadioButton.isSelected()) {
                regressionType = GraphFactory.REG_POW;
            }
            else if (FixedPowRegRadioButton.isSelected()) {
                regressionType = GraphFactory.REG_FIXEDPOW;
                try {
                    double n = Double.parseDouble(PolyTextField2.getText());
                    regressionArg = n;
                } catch (NumberFormatException e) {
                    app.showErrorDialog("Enter a real number for the degree" +
                            " of the fixed power model");
                    return;
                }
            }
            else if (ExpRegRadioButton.isSelected()) {
                regressionType = GraphFactory.REG_EXP;
                try {
                    double n = Double.parseDouble(PolyTextField2.getText());
                    regressionArg = n;
                } catch (NumberFormatException e) {
                    app.showErrorDialog("Enter a real number for the degree" +
                            " of the fixed power model");
                    return;
                }
            }
        }

        JFreeChart chart = GraphFactory.createScatterplot(
                seriesCollection, 
                TitleTextField.getText(), 
                XTextField.getText(), 
                YTextField.getText(), 
                LegendCheckBox.isSelected(), 
                regressionType,
                regressionArg,
                min, 
                max);

        if (chart == null) {
            app.showErrorDialog("Error creating scatterplot. Check input data.");
            return;
        }
        app.addLogParagraph("Scatterplot", text);

        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
        
        StatcatoChartFrame frame = 
                new StatcatoChartFrame(TitleTextField.getText(), chart, app);
        frame.pack();
        frame.setVisible(true);
        
        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 LineCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LineCheckBoxStateChanged
        if (LineCheckBox.isSelected())
            RegPanel.setVisible(true);
        else
            RegPanel.setVisible(false);
        pack();
    }//GEN-LAST:event_LineCheckBoxStateChanged

    private void PolyTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PolyTextFieldFocusGained
        PolyRegRadioButton.setSelected(true);
    }//GEN-LAST:event_PolyTextFieldFocusGained

    private void PolyTextField2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PolyTextField2FocusGained
        FixedPowRegRadioButton.setSelected(true);
    }//GEN-LAST:event_PolyTextField2FocusGained
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JRadioButton CubicRegRadioButton;
    private javax.swing.JRadioButton ExpRegRadioButton;
    private javax.swing.JRadioButton FixedPowRegRadioButton;
    private javax.swing.JCheckBox LegendCheckBox;
    private javax.swing.JRadioButton LinRegRadioButton;
    private javax.swing.JCheckBox LineCheckBox;
    private javax.swing.JRadioButton LogRegRadioButton;
    private javax.swing.JButton OKButton;
    private javax.swing.JRadioButton PolyRegRadioButton;
    private javax.swing.JTextField PolyTextField;
    private javax.swing.JTextField PolyTextField2;
    private javax.swing.JRadioButton PowRegRadioButton;
    private javax.swing.JRadioButton QuadRegRadioButton;
    private javax.swing.JPanel RegPanel;
    private javax.swing.JButton RemoveButton;
    private javax.swing.JList SeriesList;
    private javax.swing.JTextField TitleTextField;
    private javax.swing.JComboBox XComboBox;
    private javax.swing.JTextField XTextField;
    private javax.swing.JComboBox YComboBox;
    private javax.swing.JTextField YTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    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.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
