package event.editor;

import java.awt.event.*;
import java.util.ArrayList;
import java.util.Observable;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import scriptbuilder.structures.events.I_EvaluationEvent;
import scriptbuilder.structures.events.I_ScriptEvent;

/**
 *
 * @author nathaniellehrer
 */
public class GenericEvaluationPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel
{

    private I_EvaluationEvent event;
    private JTable dialogTable;

    /**
     * Creates new form GenericEvaluation
     */
    public GenericEvaluationPanel()
    {
        initComponents();

        dialogTable = GenericTable.genericizeTable(
                scrollPane, CriteriaDropdown, addButton, deleteButton, "Scale", "Criteria");
    }

    @Override
    public void getEventObject(I_ScriptEvent sei)
    {
        if (sei instanceof I_EvaluationEvent)
        {
            event = (I_EvaluationEvent) sei;
            ArrayList<String> strs = event.getExpectedActions();
            for (String s : strs)
            {
                ((MyTableModel) dialogTable.getModel()).addRow(
                        CriteriaDropdown.getItemAt(0).toString(), s);
            }

        }
        addButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                event.addAction("");
            }
        });
        dialogTable.getModel().addTableModelListener(new TableModelListener()
        {

            public void tableChanged(TableModelEvent e)
            {
                if (e.getType() == TableModelEvent.UPDATE)
                {
                    event.updateAction(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 1).toString());
                }
                if (e.getType() == TableModelEvent.DELETE)
                {
                    event.removeAction(e.getLastRow());
                }
            }
        });
    }

    @Override
    public void update(Observable o, Object arg)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public boolean removeAssociatedEvent()
    {
        ((I_ScriptEvent)event).removeThis();
        event = null;
        return true;
    }

    @Override
    public void uponClose()
    {
        //No change is necessary here; this panel saves its state continually
    }

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

        addButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        txtNotes = new javax.swing.JTextArea();
        CriteriaDropdown = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        addButton.setText("Add Criteria");
        addButton.setToolTipText("Adds a new row in the table below");

        deleteButton.setText("Delete Selected Criteria");
        deleteButton.setToolTipText("Deletes the selected row in the table below");

        scrollPane.setToolTipText("");

        txtNotes.setColumns(20);
        txtNotes.setRows(5);
        scrollPane.setViewportView(txtNotes);

        CriteriaDropdown.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes/No", "1-5", "1-10", "Written" }));
        CriteriaDropdown.setToolTipText("The type of criteria evaluation scale to be used");

        jLabel1.setText("Criteria Evaluation Scale:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(CriteriaDropdown, 0, 293, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(addButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(deleteButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 178, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(CriteriaDropdown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(deleteButton)
                    .add(addButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox CriteriaDropdown;
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea txtNotes;
    // End of variables declaration//GEN-END:variables

}
