source: tmcsimulator-scriptbuilder/trunk/src/event/editor/frame/Editor.java @ 97

Revision 97, 20.2 KB checked in by bmcguffin, 9 years ago (diff)

Fixed bug preventing the last incident in a timeslice from being deleted from its respective editor window. Also fixed a bug that caused an index-out-of-bounds error when deletions occurred too quickly, due to multithreading conflicts.

Line 
1package event.editor.frame;
2
3import event.editor.I_ScriptEventEditorPanel;
4import java.awt.BorderLayout;
5import java.awt.Color;
6import java.awt.FlowLayout;
7import javax.swing.*;
8import java.util.*;
9import java.awt.event.*;
10import scriptbuilder.gui.IncidentEditorFrame;
11import scriptbuilder.structures.ScriptIncident;
12import scriptbuilder.structures.events.*;
13
14public class Editor extends javax.swing.JFrame implements Observer
15{
16
17    IncidentEditorFrame topFrame = null;
18
19    private PropertyModel model = new PropertyModel();
20
21//    public PropertyModel getPropertyModel()
22//    {
23//        return model;
24//    }
25    public void addProperty(Properties property, I_ScriptEvent se)
26    {
27        model.addProperty(property, se);
28    }
29
30    private ActionListener optionalChangeListener = new ActionListener()
31    {
32        public void actionPerformed(ActionEvent evt)
33        {
34            JCheckBoxMenuItem src = (JCheckBoxMenuItem) evt.getSource();
35            Properties property = Properties.valueOf(src.getText().replaceAll(" ", ""));
36
37            if (src.isSelected())
38            {
39                model.addProperty(property, new CCTVEvent());
40            }
41            else
42            {
43                model.removeProperty(property);
44            }
45        }
46    };
47
48    private ActionListener multipleChangeListener = new ActionListener()
49    {
50        public void actionPerformed(ActionEvent evt)
51        {
52            JMenuItem src = (JMenuItem) evt.getSource();
53            model.addProperty(Properties.valueOf(src.getText().replaceAll(" ", "")), new CCTVEvent());
54        }
55    };
56
57    /**
58     * Blank constructor for a new editor.
59     */
60    public Editor(IncidentEditorFrame frame)
61    {
62        topFrame = frame;
63        initComponents();
64
65        model.addObserver(this);
66
67        // For each menu
68        for (int menuCtr = 0; menuCtr < jMenuBar1.getMenuCount(); menuCtr++)
69        {
70            // for each menu item
71            for (java.awt.Component comp : jMenuBar1.getMenu(menuCtr).getMenuComponents())
72            {
73                JMenuItem item = (JMenuItem) comp;
74
75                String itemName = item.getText().replaceAll(" ", "");
76
77                Properties property = Properties.valueOf(itemName);
78
79                if (property.getType() == PropertyTypes.Multiple)
80                {
81                    item.addActionListener(multipleChangeListener);
82                }
83                else if (property.getType() == PropertyTypes.Optional)
84                {
85                    item.addActionListener(optionalChangeListener);
86                }
87                else
88                {
89                    throw new RuntimeException("Property type not accounted for");
90                }
91            }
92        }
93    }
94
95    /**
96     * This method is called from within the constructor to initialize the form.
97     * WARNING: Do NOT modify this code. The content of this method is always
98     * regenerated by the Form Editor.
99     */
100    @SuppressWarnings("unchecked")
101    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
102    private void initComponents()
103    {
104
105        jPanel1 = new javax.swing.JPanel();
106        jTabbedPane1 = new javax.swing.JTabbedPane();
107        jPanel2 = new javax.swing.JPanel();
108        btnRemoveCurrentEvent = new javax.swing.JButton();
109        jMenuBar1 = new javax.swing.JMenuBar();
110        jMenu1 = new javax.swing.JMenu();
111        ATMS = new javax.swing.JCheckBoxMenuItem();
112        ActivityLog = new javax.swing.JCheckBoxMenuItem();
113        CAD = new javax.swing.JCheckBoxMenuItem();
114        CMS = new javax.swing.JMenuItem();
115        Facilitator = new javax.swing.JCheckBoxMenuItem();
116        Radio = new javax.swing.JCheckBoxMenuItem();
117        JMenu2 = new javax.swing.JMenu();
118        MaintenanceRadio = new javax.swing.JCheckBoxMenuItem();
119        TMTRadio = new javax.swing.JCheckBoxMenuItem();
120        Telephone = new javax.swing.JCheckBoxMenuItem();
121        jMenu3 = new javax.swing.JMenu();
122        Audio = new javax.swing.JMenuItem();
123        CADLog = new javax.swing.JMenuItem();
124        CCTV = new javax.swing.JMenuItem();
125        CHPRadio = new javax.swing.JCheckBoxMenuItem();
126        Paramics = new javax.swing.JMenuItem();
127        Tow = new javax.swing.JMenuItem();
128        Unit = new javax.swing.JMenuItem();
129        Witness = new javax.swing.JMenuItem();
130
131        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
132        jPanel1.setLayout(jPanel1Layout);
133        jPanel1Layout.setHorizontalGroup(
134            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
135            .add(0, 100, Short.MAX_VALUE)
136        );
137        jPanel1Layout.setVerticalGroup(
138            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
139            .add(0, 100, Short.MAX_VALUE)
140        );
141
142        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
143        setTitle("Event Editor");
144
145        btnRemoveCurrentEvent.setText("Remove This Event");
146        btnRemoveCurrentEvent.addActionListener(new java.awt.event.ActionListener()
147        {
148            public void actionPerformed(java.awt.event.ActionEvent evt)
149            {
150                btnRemoveCurrentEventActionPerformed(evt);
151            }
152        });
153
154        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
155        jPanel2.setLayout(jPanel2Layout);
156        jPanel2Layout.setHorizontalGroup(
157            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
158            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
159                .addContainerGap(701, Short.MAX_VALUE)
160                .add(btnRemoveCurrentEvent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
161                .addContainerGap())
162        );
163        jPanel2Layout.setVerticalGroup(
164            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
165            .add(btnRemoveCurrentEvent)
166        );
167
168        jMenu1.setText("Evaluations");
169
170        ATMS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
171        ATMS.setText("ATMS");
172        ATMS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ATMSEval.png"))); // NOI18N
173        ATMS.addActionListener(new java.awt.event.ActionListener()
174        {
175            public void actionPerformed(java.awt.event.ActionEvent evt)
176            {
177                multipleChange(evt);
178            }
179        });
180        jMenu1.add(ATMS);
181
182        ActivityLog.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
183        ActivityLog.setText("Activity Log");
184        ActivityLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ActivityLogEval.png"))); // NOI18N
185        ActivityLog.addActionListener(new java.awt.event.ActionListener()
186        {
187            public void actionPerformed(java.awt.event.ActionEvent evt)
188            {
189                multipleChange(evt);
190            }
191        });
192        jMenu1.add(ActivityLog);
193
194        CAD.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
195        CAD.setText("CAD");
196        CAD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CADEval.png"))); // NOI18N
197        CAD.addActionListener(new java.awt.event.ActionListener()
198        {
199            public void actionPerformed(java.awt.event.ActionEvent evt)
200            {
201                multipleChange(evt);
202            }
203        });
204        jMenu1.add(CAD);
205
206        CMS.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
207        CMS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CMSEval.png"))); // NOI18N
208        CMS.setText("CMS");
209        CMS.addMouseListener(new java.awt.event.MouseAdapter()
210        {
211            public void mouseClicked(java.awt.event.MouseEvent evt)
212            {
213                multipleChangeListener(evt);
214            }
215        });
216        CMS.addActionListener(new java.awt.event.ActionListener()
217        {
218            public void actionPerformed(java.awt.event.ActionEvent evt)
219            {
220                multipleChange(evt);
221            }
222        });
223        jMenu1.add(CMS);
224
225        Facilitator.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
226        Facilitator.setText("Facilitator");
227        Facilitator.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/FacilitatorEval.png"))); // NOI18N
228        Facilitator.addActionListener(new java.awt.event.ActionListener()
229        {
230            public void actionPerformed(java.awt.event.ActionEvent evt)
231            {
232                optionalChange(evt);
233            }
234        });
235        jMenu1.add(Facilitator);
236
237        Radio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
238        Radio.setText("Radio");
239        Radio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/RadioEval.png"))); // NOI18N
240        Radio.addActionListener(new java.awt.event.ActionListener()
241        {
242            public void actionPerformed(java.awt.event.ActionEvent evt)
243            {
244                optionalChange(evt);
245            }
246        });
247        jMenu1.add(Radio);
248
249        jMenuBar1.add(jMenu1);
250
251        JMenu2.setText("Instructor Actions");
252
253        MaintenanceRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
254        MaintenanceRadio.setText("Maintenance Radio");
255        MaintenanceRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/MaintenanceRadio.png"))); // NOI18N
256        MaintenanceRadio.addActionListener(new java.awt.event.ActionListener()
257        {
258            public void actionPerformed(java.awt.event.ActionEvent evt)
259            {
260                optionalChange(evt);
261            }
262        });
263        JMenu2.add(MaintenanceRadio);
264
265        TMTRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
266        TMTRadio.setText("TMT Radio");
267        TMTRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/TMTRadio.png"))); // NOI18N
268        TMTRadio.addActionListener(new java.awt.event.ActionListener()
269        {
270            public void actionPerformed(java.awt.event.ActionEvent evt)
271            {
272                optionalChange(evt);
273            }
274        });
275        JMenu2.add(TMTRadio);
276
277        Telephone.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
278        Telephone.setText("Telephone");
279        Telephone.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Telephone.png"))); // NOI18N
280        Telephone.addActionListener(new java.awt.event.ActionListener()
281        {
282            public void actionPerformed(java.awt.event.ActionEvent evt)
283            {
284                optionalChange(evt);
285            }
286        });
287        JMenu2.add(Telephone);
288
289        jMenuBar1.add(JMenu2);
290
291        jMenu3.setText("Automated Data");
292
293        Audio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
294        Audio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Audio.png"))); // NOI18N
295        Audio.setText("Audio");
296        jMenu3.add(Audio);
297
298        CADLog.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
299        CADLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CAD.png"))); // NOI18N
300        CADLog.setText("CAD Log");
301        jMenu3.add(CADLog);
302
303        CCTV.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
304        CCTV.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CCTV.png"))); // NOI18N
305        CCTV.setText("CCTV");
306        jMenu3.add(CCTV);
307
308        CHPRadio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
309        CHPRadio.setText("CHP Radio");
310        CHPRadio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CHPRadio.png"))); // NOI18N
311        CHPRadio.addActionListener(new java.awt.event.ActionListener()
312        {
313            public void actionPerformed(java.awt.event.ActionEvent evt)
314            {
315                optionalChange(evt);
316            }
317        });
318        jMenu3.add(CHPRadio);
319
320        Paramics.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
321        Paramics.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Paramics.png"))); // NOI18N
322        Paramics.setText("Paramics");
323        jMenu3.add(Paramics);
324
325        Tow.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
326        Tow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Tow.png"))); // NOI18N
327        Tow.setText("Tow");
328        jMenu3.add(Tow);
329
330        Unit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
331        Unit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Unit.png"))); // NOI18N
332        Unit.setText("Unit");
333        jMenu3.add(Unit);
334
335        Witness.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
336        Witness.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Witness.png"))); // NOI18N
337        Witness.setText("Witness");
338        jMenu3.add(Witness);
339
340        jMenuBar1.add(jMenu3);
341
342        setJMenuBar(jMenuBar1);
343
344        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
345        getContentPane().setLayout(layout);
346        layout.setHorizontalGroup(
347            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
348            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPane1)
349            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
350        );
351        layout.setVerticalGroup(
352            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
353            .add(layout.createSequentialGroup()
354                .addContainerGap()
355                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
356                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
357                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
358        );
359
360        pack();
361    }// </editor-fold>//GEN-END:initComponents
362
363    private void multipleChangeListener(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_multipleChangeListener
364
365    }//GEN-LAST:event_multipleChangeListener
366
367    private void multipleChange(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_multipleChange
368
369    }//GEN-LAST:event_multipleChange
370
371    private void optionalChange(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optionalChange
372
373    }//GEN-LAST:event_optionalChange
374
375    private void btnRemoveCurrentEventActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnRemoveCurrentEventActionPerformed
376    {//GEN-HEADEREND:event_btnRemoveCurrentEventActionPerformed
377
378        int index = jTabbedPane1.getSelectedIndex();
379
380        if (index >= 0 && jTabbedPane1.getTabComponentAt(index) != null)
381        {
382            JPanel removable = (JPanel) jTabbedPane1
383                    .getSelectedComponent();
384            this.model.properties.removeProperty(removable);
385        }
386
387    }//GEN-LAST:event_btnRemoveCurrentEventActionPerformed
388
389    /**
390     * @param args the command line arguments
391     */
392    public static void main(String args[])
393    {
394        java.awt.EventQueue.invokeLater(new Runnable()
395        {
396            public void run()
397            {
398                new Editor(new IncidentEditorFrame(new ScriptIncident(100, null, null, null))).setVisible(true);
399            }
400        });
401    }
402
403    // Variables declaration - do not modify//GEN-BEGIN:variables
404    private javax.swing.JCheckBoxMenuItem ATMS;
405    private javax.swing.JCheckBoxMenuItem ActivityLog;
406    private javax.swing.JMenuItem Audio;
407    private javax.swing.JCheckBoxMenuItem CAD;
408    private javax.swing.JMenuItem CADLog;
409    private javax.swing.JMenuItem CCTV;
410    private javax.swing.JCheckBoxMenuItem CHPRadio;
411    private javax.swing.JMenuItem CMS;
412    private javax.swing.JCheckBoxMenuItem Facilitator;
413    private javax.swing.JMenu JMenu2;
414    private javax.swing.JCheckBoxMenuItem MaintenanceRadio;
415    private javax.swing.JMenuItem Paramics;
416    private javax.swing.JCheckBoxMenuItem Radio;
417    private javax.swing.JCheckBoxMenuItem TMTRadio;
418    private javax.swing.JCheckBoxMenuItem Telephone;
419    private javax.swing.JMenuItem Tow;
420    private javax.swing.JMenuItem Unit;
421    private javax.swing.JMenuItem Witness;
422    private javax.swing.JButton btnRemoveCurrentEvent;
423    private javax.swing.JMenu jMenu1;
424    private javax.swing.JMenu jMenu3;
425    private javax.swing.JMenuBar jMenuBar1;
426    private javax.swing.JPanel jPanel1;
427    private javax.swing.JPanel jPanel2;
428    private javax.swing.JTabbedPane jTabbedPane1;
429    // End of variables declaration//GEN-END:variables
430
431    @Override
432    public void update(Observable o, Object arg)
433    {
434
435        final PropertyUpdate update = (PropertyUpdate) arg;
436        final ImageIcon image = update.getPanel().getProperty().getImage();
437        final String caption = update.getPanel().title();
438
439        final JLabel title = new JLabel(caption, image, JLabel.CENTER);
440
441        /*
442         final BorderLayout layout = new BorderLayout();
443         final JPanel title = new JPanel(layout);
444         title.setOpaque(false);
445         title.add(new JLabel(image), BorderLayout.WEST);
446         title.add(new JLabel(caption), BorderLayout.EAST);
447         */
448        if (update.getType() == UpdateType.Add)
449        {
450            jTabbedPane1.insertTab(null, null,
451                    update.getPanel().getPanel(), null, update.getPosition());
452            jTabbedPane1.setTabComponentAt(update.getPosition(), title);
453            jTabbedPane1.setSelectedIndex(update.getPosition());
454            topFrame.repaint();
455        }
456        else if (update.getType() == UpdateType.Remove)
457        {
458            jTabbedPane1.remove(update.getPanel().getPanel());
459            if (update.getPanel().getPanel() instanceof I_ScriptEventEditorPanel)
460            {
461                ((I_ScriptEventEditorPanel) update.getPanel().getPanel()).removeAssociatedEvent();
462            }
463            topFrame.repaint();
464        }
465        else if (update.getType() == UpdateType.TitleChange)
466        {
467            final int index = jTabbedPane1.indexOfComponent(
468                    update.getPanel().getPanel());
469
470            new Thread(new Runnable()
471            {
472                public void run()
473                {
474                    Color c = jTabbedPane1.getForegroundAt(index);
475                    jTabbedPane1.setForegroundAt(index, Color.blue);
476                    try
477                    {
478                        Thread.sleep(350);
479                    }
480                    catch (Exception e)
481                    {
482                    }
483                    if (index < jTabbedPane1.getTabCount())
484                    {
485                        jTabbedPane1.setTabComponentAt(index, title);
486                    }
487                    try
488                    {
489                        Thread.sleep(350);
490                    }
491                    catch (Exception e)
492                    {
493                    }
494                    if (index < jTabbedPane1.getTabCount())
495                    {
496                        jTabbedPane1.setForegroundAt(index, c);
497                    }
498                }
499            }).start();
500        }
501        else
502        {
503            throw new RuntimeException("UpdateType not accounted for");
504        }
505    }
506
507}
Note: See TracBrowser for help on using the repository browser.