source: tmcsimulator-scriptbuilder/trunk/src/event/editor/CHPRadioPanel.java @ 140

Revision 140, 8.4 KB checked in by bmcguffin, 8 years ago (diff)

Fixed defect #67. Removing an event and then closing the event editor window no longer causes the program to hang; a null check for the event is encountered before taking action upon close of the window.

Line 
1package event.editor;
2
3import java.awt.event.*;
4import javax.swing.*;
5import java.util.*;
6import javax.swing.event.TableModelEvent;
7import javax.swing.event.TableModelListener;
8import scriptbuilder.structures.events.*;
9
10/**
11 *
12 * @author nathaniellehrer
13 */
14public class CHPRadioPanel extends javax.swing.JPanel implements I_ScriptEventEditorPanel
15{
16
17    private ActionListener removeListener;
18    private CHPRadioEvent event;
19    private JTable dialogTable;
20
21    /**
22     * Creates new form CHPRadioPanel
23     */
24    public CHPRadioPanel()
25    {
26        initComponents();
27        HashMap<JButton, String> buttonMap = new HashMap<JButton, String>();
28        buttonMap.put(addDispatchButton, "Dispatch");
29        buttonMap.put(addFieldButton, "Field");
30        dialogTable = GenericTable.genericizeTable(jScrollPane1, buttonMap, deleteSelectedButton);
31    }
32
33    @Override
34    public void getEventObject(I_ScriptEvent sei)
35    {
36        event = (CHPRadioEvent) sei;
37        audioText.setText(event.radioFile);
38
39        for (int i = 0; i < event.lines.size(); i++)
40        {
41            ((MyTableModel) dialogTable.getModel()).addRow(event.roles.get(i), event.lines.get(i));
42        }
43        addDispatchButton.addActionListener(new ActionListener()
44        {
45
46            public void actionPerformed(ActionEvent e)
47            {
48                int i = event.roles.size();
49                event.roles.add("Dispatch");
50                event.lines.add("");
51            }
52        });
53        addFieldButton.addActionListener(new ActionListener()
54        {
55
56            public void actionPerformed(ActionEvent e)
57            {
58                int i = event.roles.size();
59                event.roles.add("Field");
60                event.lines.add("");
61            }
62        });
63        dialogTable.getModel().addTableModelListener(new TableModelListener()
64        {
65
66            public void tableChanged(TableModelEvent e)
67            {
68                if (e.getType() == TableModelEvent.UPDATE)
69                {
70                    event.roles.set(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 0).toString());
71                    event.lines.set(e.getLastRow(), dialogTable.getModel().getValueAt(e.getLastRow(), 1).toString());
72                }
73                if (e.getType() == TableModelEvent.DELETE)
74                {
75                    event.roles.remove(e.getLastRow());
76                    event.lines.remove(e.getLastRow());
77                }
78            }
79        });
80
81    }
82
83    public void update(Observable o, Object arg)
84    {
85        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
86    }
87
88    @Override
89    public boolean removeAssociatedEvent()
90    {
91        ((I_ScriptEvent) event).removeThis();
92        event = null;
93        return true;
94    }
95
96    @Override
97    public void uponClose()
98    {
99        if (event != null)
100        {
101            event.radioFile = audioText.getText();
102        }
103    }
104
105    /**
106     * This method is called from within the constructor to initialize the form.
107     * WARNING: Do NOT modify this code. The content of this method is always
108     * regenerated by the Form Editor.
109     */
110    @SuppressWarnings("unchecked")
111    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
112    private void initComponents()
113    {
114
115        jLabel4 = new javax.swing.JLabel();
116        audioText = new javax.swing.JTextField();
117        jButton2 = new javax.swing.JButton();
118        addDispatchButton = new javax.swing.JButton();
119        deleteSelectedButton = new javax.swing.JButton();
120        jScrollPane1 = new javax.swing.JScrollPane();
121        addFieldButton = new javax.swing.JButton();
122
123        jLabel4.setText("Radio File");
124
125        audioText.setToolTipText("The radio audio file");
126
127        jButton2.setText("Browse");
128        jButton2.setToolTipText("Browse for the radio audio file");
129        jButton2.addMouseListener(new java.awt.event.MouseAdapter()
130        {
131            public void mouseClicked(java.awt.event.MouseEvent evt)
132            {
133                browse(evt);
134            }
135        });
136
137        addDispatchButton.setText("Add Dispatch");
138        addDispatchButton.setToolTipText("Adds a row for dialog by the dispatch operator in the table");
139
140        deleteSelectedButton.setText("Delete Selected");
141        deleteSelectedButton.setToolTipText("Deletes the selected row from the table");
142
143        addFieldButton.setText("Add Field");
144        addFieldButton.setToolTipText("Adds a row for dialog by the field operator in the table");
145        addFieldButton.addActionListener(new java.awt.event.ActionListener()
146        {
147            public void actionPerformed(java.awt.event.ActionEvent evt)
148            {
149                addFieldButtonActionPerformed(evt);
150            }
151        });
152
153        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
154        this.setLayout(layout);
155        layout.setHorizontalGroup(
156            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
157            .add(layout.createSequentialGroup()
158                .addContainerGap()
159                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
160                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 730, Short.MAX_VALUE)
161                    .add(layout.createSequentialGroup()
162                        .add(addDispatchButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
163                        .add(3, 3, 3)
164                        .add(addFieldButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
165                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
166                        .add(deleteSelectedButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 131, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
167                    .add(layout.createSequentialGroup()
168                        .add(jLabel4)
169                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
170                        .add(audioText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
171                        .add(18, 18, 18)
172                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
173                .addContainerGap())
174        );
175        layout.setVerticalGroup(
176            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
177            .add(layout.createSequentialGroup()
178                .addContainerGap()
179                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
180                    .add(audioText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
181                    .add(jLabel4)
182                    .add(jButton2))
183                .add(18, 18, 18)
184                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
185                    .add(addDispatchButton)
186                    .add(addFieldButton)
187                    .add(deleteSelectedButton))
188                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
189                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
190                .addContainerGap())
191        );
192    }// </editor-fold>//GEN-END:initComponents
193
194    private void browse(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_browse
195        JFileChooser browser = new JFileChooser();
196        int returnVal = browser.showOpenDialog(this);
197        if (returnVal == JFileChooser.APPROVE_OPTION)
198        {
199            audioText.setText(browser.getSelectedFile().getPath());
200        }
201    }//GEN-LAST:event_browse
202
203    private void addFieldButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFieldButtonActionPerformed
204        // TODO add your handling code here:
205    }//GEN-LAST:event_addFieldButtonActionPerformed
206
207    // Variables declaration - do not modify//GEN-BEGIN:variables
208    private javax.swing.JButton addDispatchButton;
209    private javax.swing.JButton addFieldButton;
210    private javax.swing.JTextField audioText;
211    private javax.swing.JButton deleteSelectedButton;
212    private javax.swing.JButton jButton2;
213    private javax.swing.JLabel jLabel4;
214    private javax.swing.JScrollPane jScrollPane1;
215    // End of variables declaration//GEN-END:variables
216
217}
Note: See TracBrowser for help on using the repository browser.