/*
 * CalculatorDialog.java
 *
 * Created on October 10, 2008, 2:21 PM
 */

package org.statcato.dialogs.calc;

import org.statcato.calculator.*;
import org.statcato.StatcatoDialog;
import org.statcato.utils.HelperFunctions;
import org.statcato.Statcato;
import javax.swing.*;
import java.util.Vector;

/**
 *
 * @author  Margaret Yau
 */
public class CalculatorDialog extends StatcatoDialog {
    
    /** Creates new form CalculatorDialog */
    public CalculatorDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.app = (Statcato)parent;
        this.ParentSpreadsheet = app.getSpreadsheet(); 
        initComponents();
        getRootPane().setDefaultButton(OKButton);
        
        ButtonGroup group1 = new ButtonGroup();
        group1.add(RadRadioButton);
        group1.add(DegRadioButton);
        
        // initialize functions list
        FunList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //FunList.setLayoutOrientation(JList.VERTICAL_WRAP);
        FunList.setListData(MathFunctions.getSortedFunNames());
        
        ResultPanel.setVisible(false);
        pack();
        setHelpFile("calc-calculator");
        name = "Calculator";
        description = "For basic calculations involving data in columns " +
                "or constants.";
        helpStrings.add("Enter a valid expression to evaluate.  Press F1 for help.");
    }
    
    /** 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();
        ExpTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        BackButton = new javax.swing.JButton();
        ClearButton = new javax.swing.JButton();
        OKButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        PiButton = new javax.swing.JButton();
        Num4Button = new javax.swing.JButton();
        Num0Button = new javax.swing.JButton();
        Num8Button = new javax.swing.JButton();
        Num5Button = new javax.swing.JButton();
        Num2Button = new javax.swing.JButton();
        Num7Button = new javax.swing.JButton();
        DotButton = new javax.swing.JButton();
        Num1Button = new javax.swing.JButton();
        Num3Button = new javax.swing.JButton();
        Num9Button = new javax.swing.JButton();
        Num6Button = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        MinusButton = new javax.swing.JButton();
        PlusButton = new javax.swing.JButton();
        MultiplyButton = new javax.swing.JButton();
        PowerButton = new javax.swing.JButton();
        DivideButton = new javax.swing.JButton();
        ParanButton = new javax.swing.JButton();
        SqrtButton = new javax.swing.JButton();
        ExpButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        FunList = new javax.swing.JList();
        FunButton = new javax.swing.JButton();
        ResultPanel = new javax.swing.JPanel();
        ResultLabel = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        DegRadioButton = new javax.swing.JRadioButton();
        RadRadioButton = new javax.swing.JRadioButton();
        jPanel6 = new javax.swing.JPanel();
        StoreLabel = new javax.swing.JLabel();
        StoreCheckBox = new javax.swing.JCheckBox();
        StoreTextField = new javax.swing.JTextField();

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

        jLabel1.setText("Enter expression:");

        BackButton.setText("<html>&larr; Back</html>");
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

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

        OKButton.setText("<html>&crarr; OK</html>");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        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(ExpTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(OKButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BackButton)
                        .addGap(6, 6, 6)
                        .addComponent(ClearButton))
                    .addComponent(jLabel1))
                .addGap(32, 32, 32))
        );
        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.BASELINE)
                    .addComponent(ExpTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BackButton)
                    .addComponent(OKButton)
                    .addComponent(ClearButton))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Numbers"));

        PiButton.setText("<html><font face=\"serif\">&pi;</font></html>\n");
        PiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PiButtonActionPerformed(evt);
            }
        });

        Num4Button.setText("4");
        Num4Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num4ButtonActionPerformed(evt);
            }
        });

        Num0Button.setText("0");
        Num0Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num0ButtonActionPerformed(evt);
            }
        });

        Num8Button.setText("8");
        Num8Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num8ButtonActionPerformed(evt);
            }
        });

        Num5Button.setText("5");
        Num5Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num5ButtonActionPerformed(evt);
            }
        });

        Num2Button.setText("2");
        Num2Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num2ButtonActionPerformed(evt);
            }
        });

        Num7Button.setText("7");
        Num7Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num7ButtonActionPerformed(evt);
            }
        });

        DotButton.setText(".");
        DotButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DotButtonActionPerformed(evt);
            }
        });

        Num1Button.setText("1");
        Num1Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num1ButtonActionPerformed(evt);
            }
        });

        Num3Button.setText("3");
        Num3Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num3ButtonActionPerformed(evt);
            }
        });

        Num9Button.setText("9");
        Num9Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num9ButtonActionPerformed(evt);
            }
        });

        Num6Button.setText("6");
        Num6Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Num6ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Num7Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num8Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num9Button))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Num4Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num5Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num6Button))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Num1Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num2Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Num3Button))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Num0Button)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DotButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PiButton)))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {DotButton, Num0Button, Num1Button, Num2Button, Num3Button, Num4Button, Num5Button, Num6Button, Num7Button, Num8Button, Num9Button, PiButton});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num7Button)
                    .addComponent(Num8Button)
                    .addComponent(Num9Button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num4Button)
                    .addComponent(Num5Button)
                    .addComponent(Num6Button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num1Button)
                    .addComponent(Num2Button)
                    .addComponent(Num3Button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Num0Button)
                    .addComponent(DotButton)
                    .addComponent(PiButton))
                .addContainerGap())
        );

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

        MinusButton.setText("-");
        MinusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MinusButtonActionPerformed(evt);
            }
        });

        PlusButton.setText("+");
        PlusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlusButtonActionPerformed(evt);
            }
        });

        MultiplyButton.setText("*");
        MultiplyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MultiplyButtonActionPerformed(evt);
            }
        });

        PowerButton.setText("^");
        PowerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PowerButtonActionPerformed(evt);
            }
        });

        DivideButton.setText("/");
        DivideButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DivideButtonActionPerformed(evt);
            }
        });

        ParanButton.setText("()");
        ParanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ParanButtonActionPerformed(evt);
            }
        });

        SqrtButton.setText("<HTML>&radic;</html>\n");
        SqrtButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SqrtButtonActionPerformed(evt);
            }
        });

        ExpButton.setText("<html>e^</html>");
        ExpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExpButtonActionPerformed(evt);
            }
        });

        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, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(PowerButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ParanButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(PlusButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(MinusButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(MultiplyButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DivideButton))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(SqrtButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ExpButton, 0, 0, Short.MAX_VALUE)))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {DivideButton, ExpButton, MinusButton, MultiplyButton, ParanButton, PlusButton, PowerButton, SqrtButton});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PlusButton)
                    .addComponent(MinusButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MultiplyButton)
                    .addComponent(DivideButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PowerButton)
                    .addComponent(ParanButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SqrtButton)
                    .addComponent(ExpButton))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Functions"));

        jScrollPane1.setViewportView(FunList);

        FunButton.setText("Select");
        FunButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FunButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addComponent(FunButton)
                .addGap(27, 27, 27))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(FunButton)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        ResultPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Result"));

        javax.swing.GroupLayout ResultPanelLayout = new javax.swing.GroupLayout(ResultPanel);
        ResultPanel.setLayout(ResultPanelLayout);
        ResultPanelLayout.setHorizontalGroup(
            ResultPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ResultPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ResultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
                .addContainerGap())
        );
        ResultPanelLayout.setVerticalGroup(
            ResultPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ResultPanelLayout.createSequentialGroup()
                .addComponent(ResultLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("For Trigonometric Functions"));

        DegRadioButton.setText("Degrees");

        RadRadioButton.setSelected(true);
        RadRadioButton.setText("Radians");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(RadRadioButton)
                    .addComponent(DegRadioButton))
                .addContainerGap(166, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RadRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(DegRadioButton)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Store Results"));

        StoreLabel.setText("Column:");
        StoreLabel.setEnabled(false);

        StoreCheckBox.setText("Store in Worksheet");
        StoreCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StoreCheckBoxActionPerformed(evt);
            }
        });

        StoreTextField.setEnabled(false);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(StoreLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(StoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(StoreCheckBox))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(StoreCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StoreLabel)
                    .addComponent(StoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        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(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(ResultPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, 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)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 86, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, 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(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ResultPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

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

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        String exp = ExpTextField.getText();
        if (!exp.equals("")) {
            String output = "";
            MathExpParserComplete parser = new MathExpParserComplete(ParentSpreadsheet);
            CompUnit result = parser.parse(exp, RadRadioButton.isSelected());
            if (result == null)
                output = "<html>" + parser.getError() + "</html>";
            else 
                output = "<html><b>" + result + "</b><br><br></html>";
            
            if (StoreCheckBox.isSelected()) {
                String storeColumn = StoreTextField.getText();
                if (!storeColumn.equals("")) {
                    // check if Column is valid
                    int storeColumnNum = ParentSpreadsheet.getColumnNumber(storeColumn);
                    if (storeColumnNum == -1) { // invalid column
                        app.showErrorDialog("Enter a valid column name (e.g. C1) or a valid variable name to store the results.");
                    return;
                    }
                    Vector<Double> v;
                    if (result != null)
                    {
                        if (result.getType() == Token.NUMBER){
                            v = new Vector<Double>();
                            v.addElement(new Double(result.getNumber()));
                        }
                        else 
                            v = result.getColumn();
                        ParentSpreadsheet.setColumn(storeColumnNum,
                                HelperFunctions.ConvertDoubleVectorToString(v));          
                        output = "<html><b>" + result + "</b><br><br>" +
                            "Stored in " + storeColumn + "</html>";
                    }                    
                }
            }
            ResultLabel.setText(output);
            ResultPanel.setVisible(true);
            pack();
        }
        else {
            ResultPanel.setVisible(false);
        }
    }//GEN-LAST:event_OKButtonActionPerformed

    private void Num7ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num7ButtonActionPerformed
        addText("7");
    }//GEN-LAST:event_Num7ButtonActionPerformed

    private void Num8ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num8ButtonActionPerformed
        addText("8");
    }//GEN-LAST:event_Num8ButtonActionPerformed

    private void Num9ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num9ButtonActionPerformed
        addText("9");
    }//GEN-LAST:event_Num9ButtonActionPerformed

    private void Num4ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num4ButtonActionPerformed
        addText("4");
    }//GEN-LAST:event_Num4ButtonActionPerformed

    private void Num5ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num5ButtonActionPerformed
        addText("5");
    }//GEN-LAST:event_Num5ButtonActionPerformed

    private void Num6ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num6ButtonActionPerformed
        addText("6");
    }//GEN-LAST:event_Num6ButtonActionPerformed

    private void Num1ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num1ButtonActionPerformed
        addText("1");
    }//GEN-LAST:event_Num1ButtonActionPerformed

    private void Num2ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num2ButtonActionPerformed
        addText("2");
    }//GEN-LAST:event_Num2ButtonActionPerformed

    private void Num3ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num3ButtonActionPerformed
        addText("3");
    }//GEN-LAST:event_Num3ButtonActionPerformed

    private void Num0ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Num0ButtonActionPerformed
        addText("0");
    }//GEN-LAST:event_Num0ButtonActionPerformed

    private void DotButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DotButtonActionPerformed
        addText(".");
    }//GEN-LAST:event_DotButtonActionPerformed

    private void PiButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PiButtonActionPerformed
        addText("pi");
}//GEN-LAST:event_PiButtonActionPerformed

    private void PlusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlusButtonActionPerformed
        addText("+");
    }//GEN-LAST:event_PlusButtonActionPerformed

    private void MinusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MinusButtonActionPerformed
        addText("-");
    }//GEN-LAST:event_MinusButtonActionPerformed

    private void MultiplyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MultiplyButtonActionPerformed
        addText("*");
    }//GEN-LAST:event_MultiplyButtonActionPerformed

    private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DivideButtonActionPerformed
        addText("/");
    }//GEN-LAST:event_DivideButtonActionPerformed

    private void PowerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PowerButtonActionPerformed
        addText("^");
    }//GEN-LAST:event_PowerButtonActionPerformed

    private void ParanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ParanButtonActionPerformed
        int pos = ExpTextField.getCaretPosition();
        addText("()");
        ExpTextField.setCaretPosition(pos+1);
    }//GEN-LAST:event_ParanButtonActionPerformed

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        int pos = ExpTextField.getCaretPosition();
        String text = ExpTextField.getText().trim();
        String newText = "";
        if (!text.equals("")) {
            newText = text.substring(0, pos-1);
            if (pos < text.length())
                newText += text.substring(pos);
            ExpTextField.setText(newText);
            ExpTextField.setCaretPosition(pos-1);
        }
        
        ExpTextField.requestFocusInWindow();
    }//GEN-LAST:event_BackButtonActionPerformed

    private void SqrtButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SqrtButtonActionPerformed
        int pos = ExpTextField.getCaretPosition();
        addText("sqrt()");
        ExpTextField.setCaretPosition(pos+5);
}//GEN-LAST:event_SqrtButtonActionPerformed

    private void ExpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExpButtonActionPerformed
        int pos = ExpTextField.getCaretPosition();
        addText("exp()");
        ExpTextField.setCaretPosition(pos+4);
}//GEN-LAST:event_ExpButtonActionPerformed

    private void FunButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FunButtonActionPerformed
        String fun = (String)FunList.getSelectedValue();
        int pos = ExpTextField.getCaretPosition();
        addText(fun + "()");
        ExpTextField.setCaretPosition(pos+fun.length()+1);
}//GEN-LAST:event_FunButtonActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        ExpTextField.setText("");
        ResultPanel.setVisible(false);
        pack();
        ExpTextField.requestFocusInWindow();
    }//GEN-LAST:event_ClearButtonActionPerformed

    private void StoreCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StoreCheckBoxActionPerformed
        if (StoreCheckBox.isSelected()) {
            StoreLabel.setEnabled(true);
            StoreTextField.setEnabled(true);
        } 
        else {
            StoreLabel.setEnabled(false);
            StoreTextField.setEnabled(false);
        }
    }//GEN-LAST:event_StoreCheckBoxActionPerformed
    
    private void addText(String str) {
        int pos = ExpTextField.getCaretPosition();
        String text = ExpTextField.getText();
        String newText = "";
        if (text.equals("")) 
            newText = str;
        else {
            newText = text.substring(0, pos) + str;
            if (pos < text.length())
                newText += text.substring(pos);
        }
        ExpTextField.setText(newText);
        ExpTextField.setCaretPosition(pos+str.length());
        ExpTextField.requestFocusInWindow();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BackButton;
    private javax.swing.JButton ClearButton;
    private javax.swing.JRadioButton DegRadioButton;
    private javax.swing.JButton DivideButton;
    private javax.swing.JButton DotButton;
    private javax.swing.JButton ExpButton;
    private javax.swing.JTextField ExpTextField;
    private javax.swing.JButton FunButton;
    private javax.swing.JList FunList;
    private javax.swing.JButton MinusButton;
    private javax.swing.JButton MultiplyButton;
    private javax.swing.JButton Num0Button;
    private javax.swing.JButton Num1Button;
    private javax.swing.JButton Num2Button;
    private javax.swing.JButton Num3Button;
    private javax.swing.JButton Num4Button;
    private javax.swing.JButton Num5Button;
    private javax.swing.JButton Num6Button;
    private javax.swing.JButton Num7Button;
    private javax.swing.JButton Num8Button;
    private javax.swing.JButton Num9Button;
    private javax.swing.JButton OKButton;
    private javax.swing.JButton ParanButton;
    private javax.swing.JButton PiButton;
    private javax.swing.JButton PlusButton;
    private javax.swing.JButton PowerButton;
    private javax.swing.JRadioButton RadRadioButton;
    private javax.swing.JLabel ResultLabel;
    private javax.swing.JPanel ResultPanel;
    private javax.swing.JButton SqrtButton;
    private javax.swing.JCheckBox StoreCheckBox;
    private javax.swing.JLabel StoreLabel;
    private javax.swing.JTextField StoreTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
