package event.editor;

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

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

    private ActionListener removeListener;
    private CHPRadioEvent event;
    private JTable dialogTable;

    /**
     * Creates new form CHPRadioPanel
     */
    public CHPRadioPanel()
    {
        initComponents();
        HashMap<JButton, String> buttonMap = new HashMap<JButton, String>();
        buttonMap.put(addDispatchButton, "Dispatch");
        buttonMap.put(addFieldButton, "Field");
        dialogTable = GenericTable.genericizeTable(jScrollPane1, buttonMap, deleteSelectedButton);
    }

    @Override
    public void getEventObject(I_ScriptEvent sei)
    {
        event = (CHPRadioEvent) sei;
        audioText.setText(event.radioFile);

        for (int i = 0; i < event.lines.size(); i++)
        {
            ((MyTableModel) dialogTable.getModel()).addRow(event.roles.get(i), event.lines.get(i));
        }
        addDispatchButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                int i = event.roles.size();
                event.roles.add("Dispatch");
                event.lines.add("");
            }
        });
        addFieldButton.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                int i = event.roles.size();
                event.roles.add("Field");
                event.lines.add("");
            }
        });
        dialogTable.getModel().addTableModelListener(new TableModelListener()
        {

            public void tableChanged(TableModelEvent e)
            {
                if (e.getType() == TableModelEvent.UPDATE)
                {
                    event.roles.set(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 0).toString());
                    event.lines.set(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 1).toString());
                }
                if (e.getType() == TableModelEvent.DELETE)
                {
                    event.roles.remove(e.getLastRow());
                    event.lines.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.
    }

    @Override
    public boolean removeAssociatedEvent()
    {
        ((I_ScriptEvent) event).removeThis();
        event = null;
        return true;
    }

    @Override
    public void uponClose()
    {
        if (event != null)
        {
            event.radioFile = audioText.getText();
        }
    }

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

        jLabel4 = new javax.swing.JLabel();
        audioText = new javax.swing.JTextField();
        addDispatchButton = new javax.swing.JButton();
        deleteSelectedButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        addFieldButton = new javax.swing.JButton();

        jLabel4.setText("Radio File");

        audioText.setToolTipText("The radio audio file");
        audioText.setEnabled(false);

        addDispatchButton.setText("Add Dispatch");
        addDispatchButton.setToolTipText("Adds a row for dialog by the dispatch operator in the table");

        deleteSelectedButton.setText("Delete Selected");
        deleteSelectedButton.setToolTipText("Deletes the selected row from the table");

        addFieldButton.setText("Add Field");
        addFieldButton.setToolTipText("Adds a row for dialog by the field operator in the table");
        addFieldButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addFieldButtonActionPerformed(evt);
            }
        });

        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(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 730, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(addDispatchButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(3, 3, 3)
                        .add(addFieldButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(deleteSelectedButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 331, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(audioText)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(audioText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addDispatchButton)
                    .add(addFieldButton)
                    .add(deleteSelectedButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDispatchButton;
    private javax.swing.JButton addFieldButton;
    private javax.swing.JTextField audioText;
    private javax.swing.JButton deleteSelectedButton;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
