anslab2.AnsLab2.java Source code

Java tutorial

Introduction

Here is the source code for anslab2.AnsLab2.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package anslab2;

import static algo.CTUngrouped.round;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author El Gato
 */
public class AnsLab2 extends javax.swing.JFrame {

    private DefaultListModel rawlist;
    private DefaultListModel ascendingList;
    private static DefaultTableModel model;
    private String title, category;
    private ArrayList array;
    private ArrayList array_sorted;
    private static HashMap<String, Integer> string_maps;
    private static TreeMap<Double, Integer> double_maps;
    //private Test dt;

    //alphabetic numeric string
    private int dataType;

    /**
     * Creates new form AnsLab2GUI1
     */
    public AnsLab2() {
        //dt = new Test();
        model = new DefaultTableModel();
        rawlist = new DefaultListModel();
        ascendingList = new DefaultListModel();

        initComponents();

        TypeGroups.add(AlphabeticSelect);
        TypeGroups.add(StringSelect);
        TypeGroups.add(NumericSelect);
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TypeGroups = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        SetTitleButton = new javax.swing.JButton();
        SetTitleField = new javax.swing.JTextField();
        FDT = new javax.swing.JCheckBox();
        RFT = new javax.swing.JCheckBox();
        GenerateTable = new javax.swing.JButton();
        StringSelect = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        NumericSelect = new javax.swing.JRadioButton();
        AlphabeticSelect = new javax.swing.JRadioButton();
        SetDataType = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new javax.swing.JTable() {
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        VertBarGraphButton = new javax.swing.JButton();
        HorizBarGraphButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        Size = new javax.swing.JLabel();
        ResetButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        rawData = new javax.swing.JList();
        AddData = new javax.swing.JButton();
        Finish = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        SetCategoryField = new javax.swing.JTextField();
        SetCategoryButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ascendingData = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        TitleLabel = new javax.swing.JLabel();
        DataTypeLabel = new javax.swing.JLabel();
        sizeLabel = new javax.swing.JLabel();
        TitleLabel1 = new javax.swing.JLabel();
        Close = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        jPanel1.setBackground(new java.awt.Color(0, 153, 255));

        SetTitleButton.setText("Set Title");
        SetTitleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SetTitleButtonActionPerformed(evt);
            }
        });

        FDT.setText("FDT");
        FDT.setEnabled(false);

        RFT.setText("RFT");
        RFT.setEnabled(false);

        GenerateTable.setText("Generate");
        GenerateTable.setEnabled(false);
        GenerateTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GenerateTableActionPerformed(evt);
            }
        });

        StringSelect.setText("String");
        StringSelect.setEnabled(false);

        jLabel4.setText("Set data type:");

        NumericSelect.setText("Numeric");
        NumericSelect.setEnabled(false);

        AlphabeticSelect.setText("Alphabetic");
        AlphabeticSelect.setEnabled(false);

        SetDataType.setText("Set Data Type");
        SetDataType.setEnabled(false);
        SetDataType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SetDataTypeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel1Layout.createSequentialGroup().addContainerGap(22, Short.MAX_VALUE)
                                        .addComponent(SetTitleField, javax.swing.GroupLayout.PREFERRED_SIZE, 216,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(SetTitleButton).addGap(32, 32, 32).addComponent(FDT)
                                        .addGap(18, 18, 18).addComponent(RFT).addGap(18, 18, 18)
                                        .addComponent(GenerateTable).addGap(8, 8, 8))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGap(33, 33, 33).addComponent(jLabel4)
                                .addGap(9, 9, 9).addComponent(AlphabeticSelect)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(NumericSelect)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(StringSelect)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SetDataType)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(SetTitleButton)
                                .addComponent(SetTitleField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(FDT).addComponent(RFT).addComponent(GenerateTable))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(StringSelect).addComponent(jLabel4).addComponent(NumericSelect)
                                .addComponent(AlphabeticSelect).addComponent(SetDataType))
                        .addGap(7, 7, 7)));

        Table.setModel(model);
        jScrollPane1.setViewportView(Table);

        VertBarGraphButton.setText("Vertical Bar Graph");
        VertBarGraphButton.setEnabled(false);
        VertBarGraphButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VertBarGraphButtonActionPerformed(evt);
            }
        });

        HorizBarGraphButton.setText("Horizontal Bar Graph");
        HorizBarGraphButton.setEnabled(false);
        HorizBarGraphButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HorizBarGraphButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("n:");

        Size.setText(" ");

        ResetButton.setText("Reset");
        ResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetButtonActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 102, 51));

        rawData.setModel(rawlist);
        jScrollPane2.setViewportView(rawData);

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

        Finish.setText("Finish");
        Finish.setEnabled(false);
        Finish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FinishActionPerformed(evt);
            }
        });

        jLabel7.setText("Set Category:");

        SetCategoryField.setEnabled(false);

        SetCategoryButton.setText("Set Category");
        SetCategoryButton.setEnabled(false);
        SetCategoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SetCategoryButtonActionPerformed(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()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup().addGap(73, 73, 73)
                                        .addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(AddData, javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(Finish, javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(75, 75, 75)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 214,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel2Layout.createSequentialGroup().addGap(52, 52, 52)
                                        .addComponent(jLabel7).addGap(18, 18, 18)
                                        .addComponent(SetCategoryField, javax.swing.GroupLayout.PREFERRED_SIZE, 239,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(SetCategoryButton)))
                        .addContainerGap(26, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap(12, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(SetCategoryField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(SetCategoryButton))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(AddData, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(Finish, javax.swing.GroupLayout.PREFERRED_SIZE, 60,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));

        jPanel3.setBackground(new java.awt.Color(0, 153, 255));

        jLabel1.setText("Data Set Ascending");

        jScrollPane3.setViewportView(ascendingData);

        jLabel2.setText("Title:");

        jLabel5.setText("Data Type:");

        jLabel6.setText("n:");

        TitleLabel.setText(" ");

        DataTypeLabel.setText(" ");

        sizeLabel.setText(" ");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(15, 15, 15)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(jScrollPane3,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 193,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(66, 66, 66)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel5)
                                        .addGap(28, 28, 28).addComponent(DataTypeLabel,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 118,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel2)
                                        .addGap(32, 32, 32).addComponent(TitleLabel,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 124,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel6)
                                        .addGap(57, 57, 57).addComponent(sizeLabel,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(25, Short.MAX_VALUE)));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(14, 14, 14)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1).addComponent(jLabel2).addComponent(TitleLabel))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 114,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup().addGap(18, 18, 18)
                                        .addGroup(jPanel3Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel5).addComponent(DataTypeLabel))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel3Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel6).addComponent(sizeLabel))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        TitleLabel1.setText(" ");

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                        .addComponent(TitleLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 198,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(159, 159, 159))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addComponent(HorizBarGraphButton)
                                                .addGap(122, 122, 122).addComponent(VertBarGraphButton))
                                        .addGroup(layout.createSequentialGroup().addComponent(jLabel3)
                                                .addGap(29, 29, 29).addComponent(Size,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 46,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        553, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup().addGap(185, 185, 185)
                                                .addComponent(ResetButton).addGap(18, 18, 18).addComponent(Close)))
                                        .addGap(0, 0, Short.MAX_VALUE)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(TitleLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 299,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3).addComponent(Size))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27,
                                        Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(VertBarGraphButton).addComponent(HorizBarGraphButton))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(ResetButton).addComponent(Close))
                                .addGap(20, 20, 20))
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18).addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));

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

    private void SetTitleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetTitleButtonActionPerformed
        // TODO add your handling code here:
        title = SetTitleField.getText();
        TitleLabel.setText(title);
        TitleLabel1.setText(title);
        SetTitleField.setEnabled(false);
        SetTitleButton.setEnabled(false);

        setDataType(true);
    }//GEN-LAST:event_SetTitleButtonActionPerformed

    private void GenerateTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GenerateTableActionPerformed
        // TODO add your handling code here:
        boolean fdt = FDT.isSelected();
        boolean rft = RFT.isSelected();

        model = generateTable(fdt, rft, dataType, array_sorted, title);

        setFDTRFTBoth(false);
        setgenerateGraph(true);
    }//GEN-LAST:event_GenerateTableActionPerformed

    private void SetDataTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetDataTypeActionPerformed
        // TODO add your handling code here:
        if (AlphabeticSelect.isSelected()) {
            dataType = 1;
            DataTypeLabel.setText("Alphabetic");
        } else if (NumericSelect.isSelected()) {
            dataType = 2;
            DataTypeLabel.setText("Numeric");
        } else if (StringSelect.isSelected()) {
            dataType = 3;
            DataTypeLabel.setText("String");
        }
        setDataType(false);
        setaddingData(true);
    }//GEN-LAST:event_SetDataTypeActionPerformed

    private void AddDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddDataActionPerformed
        // TODO add your handling code here:
        String input = new String();
        do {
            input = JOptionPane.showInputDialog(null, "Input data");
        } while (!Test.typesMatch(input, dataType));
        rawlist.addElement(input);
    }//GEN-LAST:event_AddDataActionPerformed

    private void FinishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FinishActionPerformed
        // TODO add your handling code here:
        AddData.setEnabled(false);
        array = new ArrayList(Arrays.asList(rawlist.toArray()));

        array_sorted = Test.sortData(array, dataType);

        for (Object data : array_sorted) {
            ascendingList.addElement(data);
        }

        ascendingData.setModel(ascendingList);

        sizeLabel.setText(String.valueOf(array_sorted.size()));
        Size.setText(String.valueOf(array_sorted.size()));

        setaddingData(false);
        setFDTRFTBoth(true);

    }//GEN-LAST:event_FinishActionPerformed

    private void SetCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SetCategoryButtonActionPerformed
        // TODO add your handling code here:
        category = SetCategoryField.getText();
        SetCategoryButton.setEnabled(false);
        SetCategoryField.setEnabled(false);
    }//GEN-LAST:event_SetCategoryButtonActionPerformed

    private void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetButtonActionPerformed
        // TODO add your handling code here:
        new AnsLab2().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_ResetButtonActionPerformed

    private void HorizBarGraphButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HorizBarGraphButtonActionPerformed
        // TODO add your handling code here:
        generateHorizontalBarGraph();
    }//GEN-LAST:event_HorizBarGraphButtonActionPerformed

    private void VertBarGraphButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VertBarGraphButtonActionPerformed
        // TODO add your handling code here:
        generateVerticalBarGraph();
    }//GEN-LAST:event_VertBarGraphButtonActionPerformed

    private void CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_CloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AnsLab2.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AnsLab2.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AnsLab2.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AnsLab2.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AnsLab2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddData;
    private javax.swing.JRadioButton AlphabeticSelect;
    private javax.swing.JButton Close;
    private javax.swing.JLabel DataTypeLabel;
    private javax.swing.JCheckBox FDT;
    private javax.swing.JButton Finish;
    private javax.swing.JButton GenerateTable;
    private javax.swing.JButton HorizBarGraphButton;
    private javax.swing.JRadioButton NumericSelect;
    private javax.swing.JCheckBox RFT;
    private javax.swing.JButton ResetButton;
    private javax.swing.JButton SetCategoryButton;
    private javax.swing.JTextField SetCategoryField;
    private javax.swing.JButton SetDataType;
    private javax.swing.JButton SetTitleButton;
    private javax.swing.JTextField SetTitleField;
    private javax.swing.JLabel Size;
    private javax.swing.JRadioButton StringSelect;
    private javax.swing.JTable Table;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JLabel TitleLabel1;
    private javax.swing.ButtonGroup TypeGroups;
    private javax.swing.JButton VertBarGraphButton;
    private javax.swing.JList ascendingData;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList rawData;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables

    private void setDataType(boolean state) {
        AlphabeticSelect.setEnabled(state);
        NumericSelect.setEnabled(state);
        StringSelect.setEnabled(state);
        SetDataType.setEnabled(state);
    }

    private void setFDTRFTBoth(boolean state) {
        FDT.setEnabled(state);
        RFT.setEnabled(state);
        GenerateTable.setEnabled(state);
    }

    private void setaddingData(boolean state) {
        AddData.setEnabled(state);
        Finish.setEnabled(state);
        SetCategoryButton.setEnabled(state);
        SetCategoryField.setEnabled(state);
    }

    private void setgenerateGraph(boolean state) {
        HorizBarGraphButton.setEnabled(state);
        VertBarGraphButton.setEnabled(state);
    }

    public static DefaultTableModel generateTable(boolean fdt, boolean rft, int dataType, ArrayList labels,
            String label) {
        //DefaultTableModel model = new DefaultTableModel();

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        for (Object temp : labels) {
            Integer count = map.get(String.valueOf(temp));
            map.put(String.valueOf(temp), (count == null) ? 1 : count + 1);
        }

        Vector _label = new Vector();
        Vector _freq = new Vector();
        Vector _rel_freq = new Vector();

        if (dataType == 1 || dataType == 3) {
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                _label.add(entry.getKey());
                _freq.add(entry.getValue());
                _rel_freq.add(((double) entry.getValue() / labels.size()) * (100));
            }
            string_maps = map;
            model.addColumn(label, _label);
        }

        else if (dataType == 2) {
            TreeMap<Double, Integer> num_map = new TreeMap<Double, Integer>();

            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                num_map.put(Double.valueOf(entry.getKey()), entry.getValue());
            }

            for (Map.Entry<Double, Integer> entry1 : num_map.entrySet()) {
                _label.add(entry1.getKey());
                _freq.add(entry1.getValue());
                _rel_freq.add(round(((double) entry1.getValue() / labels.size()) * (100), 2));
            }
            double_maps = num_map;
            model.addColumn(label, _label);
        }

        if (fdt == true) {
            model.addColumn("Frequency", _freq);
        }
        if (rft == true) {
            model.addColumn("Relative Frequency", _rel_freq);
        }

        return model;
    }

    public void generateVerticalBarGraph() {
        JFreeChart barChart = ChartFactory.createBarChart(category + " chart", "Category", "", createDataset(),
                PlotOrientation.VERTICAL, true, true, false);

        ChartPanel chartPanel = new ChartPanel(barChart);
        chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
        JFrame l = new JFrame();
        l.setContentPane(chartPanel);
        l.setSize(400, 400);
        l.setVisible(true);
    }

    public void generateHorizontalBarGraph() {
        JFreeChart barChart = ChartFactory.createBarChart(category + " chart", "Category", "", createDataset(),
                PlotOrientation.HORIZONTAL, true, true, false);

        ChartPanel chartPanel = new ChartPanel(barChart);
        chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
        JFrame l = new JFrame();
        l.setContentPane(chartPanel);
        l.setSize(400, 400);
        l.setVisible(true);
    }

    private CategoryDataset createDataset() {
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        //model.getValueAt(WIDTH, WIDTH)

        if ((FDT.isSelected() && RFT.isSelected()) || (FDT.isSelected() && !RFT.isSelected())) { //-> generate fdt
            //for(int index=0;index<string_maps.size();index++)
            for (int i = 0; i < model.getRowCount(); i++)
                //String.valueOf(model.getValueAt(i, 1))
                dataset.addValue(Double.valueOf(String.valueOf(model.getValueAt(i, 1))), category,
                        String.valueOf(model.getValueAt(i, 0)));
        }

        else if (RFT.isSelected() && !FDT.isSelected()) { // -> generate rft
            //for(int index=0;index<string_maps.size();index++)
            for (int i = 0; i < model.getRowCount(); i++)
                //String.valueOf(model.getValueAt(i, 1))
                dataset.addValue(Double.valueOf(String.valueOf(model.getValueAt(i, 1))), category,
                        String.valueOf(model.getValueAt(i, 0)));
        }

        return dataset;
    }

}