package event.editor;

import java.awt.event.*;
import java.util.*;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import scriptbuilder.structures.events.CMSEvaluationEvent;
import scriptbuilder.structures.events.ScriptEventInterface;

/**
 *
 * @author nathaniellehrer
 */
public class CMSEvaluationPanel extends javax.swing.JPanel implements RemoveablePanel, ScriptEventEditorPanel
{

    private HashMap<String, Class> properties;
    private JTable dialogTable;
    private ActionListener removeListener;
    private CMSEvaluationEvent event;

    /**
     * Creates new form CMSEvaluation
     */
    public CMSEvaluationPanel()
    {
        initComponents();

        dialogTable = GenericTable.genericizeNumberedTable(jScrollPane1, addButton, deleteButton);
    }

    public void setRemoveListener(ActionListener listener)
    {
        removeListener = listener;
    }

    public void getEventObject(ScriptEventInterface sei)
    {
        event = (CMSEvaluationEvent) sei;
        txtID.setText(event.cmsID);
        txtLocation.setText(event.location);
        txtMessage.setText("");
        for (int i = 0; i < event.message.size(); i++)
        {
            for (MouseListener ml : addButton.getMouseListeners())
            {
                ml.mouseClicked(new MouseEvent(addButton, 1, 1, 1, 1, 1, 1, true));
            }
            ((MyTableModel) dialogTable.getModel()).setValueAt(event.message.get(i), i, 1);
        }
        for (int i = 0; i < TypeDropdown.getItemCount(); i++)
        {
            if (event.cmsType.equalsIgnoreCase(TypeDropdown.getItemAt(i).toString()))
            {
                TypeDropdown.setSelectedIndex(i);
            }
        }
        dialogTable.getModel().addTableModelListener(new TableModelListener()
        {

            public void tableChanged(TableModelEvent e)
            {
                if (e.getType() == TableModelEvent.UPDATE)
                {
                    event.message.set(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 0).toString());
                }
                if(e.getType() == TableModelEvent.DELETE)
                {
                    event.message.remove(e.getLastRow());
                }
            }
        });

    }

    public void update(Observable o, Object arg)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * 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()
    {

        removeButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtLocation = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TypeDropdown = new javax.swing.JComboBox();
        txtID = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        addButton = new javax.swing.JButton();

        removeButton.setText("Remove");
        removeButton.setToolTipText("Removes this property");
        removeButton.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                removeButtonremoveThisProperty(evt);
            }
        });

        jLabel3.setText("Location");

        txtLocation.setToolTipText("Example: SB 55 @ WARNER AVE");

        jLabel2.setText("Type");

        TypeDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Update", "New", "Remove" }));

        txtID.setToolTipText("Example: 72");

        jLabel1.setText("CMS ID");

        deleteButton.setText("Delete Selected Message");
        deleteButton.setToolTipText("Deletes the selected message from the table");

        jScrollPane1.setHorizontalScrollBar(null);

        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        jScrollPane1.setViewportView(txtMessage);

        addButton.setText("Add Message");
        addButton.setToolTipText("Adds a message to the table");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(6, 6, 6)
                        .add(addButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(deleteButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 178, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(293, 293, 293))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, removeButton)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel1)
                                    .add(jLabel3)
                                    .add(jLabel2))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(TypeDropdown, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(txtLocation)
                                    .add(txtID))))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(17, 17, 17)
                .add(removeButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(txtID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(TypeDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(txtLocation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(31, 31, 31)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(deleteButton)
                    .add(addButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void removeButtonremoveThisProperty(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removeButtonremoveThisProperty
        if (removeListener != null)
        {
            removeListener.actionPerformed(new ActionEvent(this, 0, ""));
        }
}//GEN-LAST:event_removeButtonremoveThisProperty


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox TypeDropdown;
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeButton;
    private javax.swing.JFormattedTextField txtID;
    private javax.swing.JTextField txtLocation;
    private javax.swing.JTextArea txtMessage;
    // End of variables declaration//GEN-END:variables

}
