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

Revision 130, 8.4 KB checked in by bmcguffin, 9 years ago (diff)

Changed implementation of save function for event editor panels. Instead of updating their model objects whenever the enter key is pressed, panels are now notified when the window is closed and update their information then.

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