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

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