// DisplayFrame.java

package lab05;

import javax.swing.JOptionPane;

/**
 * This class is the toplevel application window for Lab 05.
 * 
 * @author J.K. Daugherty
 */
public class DisplayFrame extends javax.swing.JFrame {

    private Queue queue;
    private DisplayPanel display;
    
    public DisplayFrame() {
        queue = new Queue();
        display = new DisplayPanel (queue);
        
        initComponents();
        setSize (580, 600);
        setLocationRelativeTo (null);
        setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        setTitle("Lab05: Queue Tester");
        add (display, java.awt.BorderLayout.CENTER);
    }
    
    private void initComponents() {
        controlPanel = new javax.swing.JPanel();
        fldAdd = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        fldRemove = new javax.swing.JTextField();
        btnRemove = new javax.swing.JButton();
        btnTrim = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        statusPanel = new javax.swing.JPanel();
        lblItemCount = new javax.swing.JLabel();
        fldItemCount = new javax.swing.JTextField();
        fldItemCount.setText("" + queue.getItemCount());
        lblCapacity = new javax.swing.JLabel();
        fldCapacity = new javax.swing.JTextField();
        fldCapacity.setText("" + queue.getCapacity());
        lblNewCapacity = new javax.swing.JLabel();
        fldNewCapacity = new javax.swing.JTextField();
        btnApply = new javax.swing.JButton();

        controlPanel.setBackground(new java.awt.Color(255, 255, 255));
        fldAdd.setColumns(5);
        fldAdd.setToolTipText("Enter name  to be added");
        fldAdd.setPreferredSize(new java.awt.Dimension(66, 28));
        fldAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fldAddActionPerformed(evt);
            }
        });
        controlPanel.add(fldAdd);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        controlPanel.add(btnAdd);

        fldRemove.setColumns(5);
        fldRemove.setEditable(false);
        fldRemove.setPreferredSize(new java.awt.Dimension(66, 28));
        controlPanel.add(fldRemove);

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        controlPanel.add(btnRemove);

        btnTrim.setText("Trim");
        btnTrim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrimActionPerformed(evt);
            }
        });
        controlPanel.add(btnTrim);

        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });
        controlPanel.add(btnClear);

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        controlPanel.add(btnReset);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        controlPanel.add(btnExit);

        add(controlPanel, java.awt.BorderLayout.SOUTH);

        statusPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 5));

        statusPanel.setBackground(new java.awt.Color(255, 255, 255));
        lblItemCount.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblItemCount.setText("ItemCount:");
        statusPanel.add(lblItemCount);

        fldItemCount.setColumns(2);
        fldItemCount.setEditable(false);
        fldItemCount.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        fldItemCount.setPreferredSize(new java.awt.Dimension(33, 28));
        statusPanel.add(fldItemCount);

        lblCapacity.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblCapacity.setText("Current capacity:");
        statusPanel.add(lblCapacity);

        fldCapacity.setColumns(2);
        fldCapacity.setEditable(false);
        fldCapacity.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        fldCapacity.setPreferredSize(new java.awt.Dimension(33, 28));
        statusPanel.add(fldCapacity);

        lblNewCapacity.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblNewCapacity.setText("New capacity:");
        statusPanel.add(lblNewCapacity);

        fldNewCapacity.setColumns(2);
        fldNewCapacity.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        fldNewCapacity.setPreferredSize(new java.awt.Dimension(33, 28));
        statusPanel.add(fldNewCapacity);

        btnApply.setText("Apply");
        btnApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyActionPerformed(evt);
            }
        });
        statusPanel.add(btnApply);

        add(statusPanel, java.awt.BorderLayout.NORTH);
    }

    private void btnTrimActionPerformed(java.awt.event.ActionEvent evt) {
        queue.trim();
        fldCapacity.setText("" + queue.getCapacity());
        display.repaint();
    }

    private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            String temp = fldNewCapacity.getText();
            int newCapacity = Integer.parseInt (fldNewCapacity.getText());
            queue.setCapacity (newCapacity);
            fldCapacity.setText("" + queue.getCapacity());
	    display.repaint();
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog (this,
                "Invalid integer syntax. Please retype\n" +
                    " the value for the new capacity.",
                "Invalid Integer",
                JOptionPane.ERROR_MESSAGE);
        }
        catch (IllegalStateException e) {
            JOptionPane.showMessageDialog (this,
                "The capacity cannot be less than\n" +
                "the current number of stored items.\n",
                "Insufficient Capacity",
                JOptionPane.ERROR_MESSAGE);
        }
    }

    private void fldAddActionPerformed(java.awt.event.ActionEvent evt) {
        addItem();
    }

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
        queue.clear();
        fldItemCount.setText("" + queue.getItemCount());
        fldCapacity.setText("" + queue.getCapacity());
        fldAdd.setText("");
        fldRemove.setText("");
        fldNewCapacity.setText("");
        display.repaint();
    }

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
        queue.reset();
        fldItemCount.setText("" + queue.getItemCount());
        fldCapacity.setText("" + queue.getCapacity());
        fldAdd.setText("");
        fldRemove.setText("");
        fldNewCapacity.setText("");
        display.repaint();
    }

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
        System.exit(0);
    }

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {
        if (queue.isEmpty()) {
            fldRemove.setText("");
            JOptionPane.showMessageDialog (this,
                "The Queue is empty, no value to remove.",
                "Empty Queue",
                JOptionPane.ERROR_MESSAGE);
        }
        else {
            fldRemove.setText ((String) queue.remove());
            fldItemCount.setText("" + queue.getItemCount());
            display.repaint();
        }
    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
        addItem();
    }
    
    private void addItem () {
        queue.add (fldAdd.getText());
        fldAdd.setText("");
        fldItemCount.setText("" + queue.getItemCount());
        fldCapacity.setText("" + queue.getCapacity());
        display.repaint();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DisplayFrame().setVisible(true);
            }
        });
    }
    
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnApply;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnTrim;
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnReset;
    private javax.swing.JTextField fldAdd;
    private javax.swing.JTextField fldCapacity;
    private javax.swing.JTextField fldItemCount;
    private javax.swing.JTextField fldNewCapacity;
    private javax.swing.JTextField fldRemove;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JLabel lblCapacity;
    private javax.swing.JLabel lblItemCount;
    private javax.swing.JLabel lblNewCapacity;
}
