source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/IncidentPaletteFrame.java @ 103

Revision 103, 15.3 KB checked in by bmcguffin, 9 years ago (diff)

Individual incident editor window now shows the relevant incident as though it started at 00:00:00. When window is closed, the incident is re-inserted into the correct position.

Line 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package scriptbuilder.gui;
7
8import java.awt.GridLayout;
9import java.io.File;
10import java.io.FileFilter;
11import java.text.SimpleDateFormat;
12import java.time.LocalDate;
13import java.util.ArrayList;
14import java.util.Date;
15import java.util.TimeZone;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.JViewport;
19import scriptbuilder.gui.panels.IncidentPaletteAddPanel;
20import scriptbuilder.structures.*;
21
22/**
23 *
24 * @author Bryan McGuffin
25 */
26public class IncidentPaletteFrame extends javax.swing.JFrame
27{
28
29    private SimulationScript script;
30
31    private ScriptBuilderFrame parent;
32
33    private SimulationScript dummyScript;
34
35    /**
36     * The list of incidents to be displayed in the palette.
37     */
38    public ArrayList<ScriptIncident> incidentList;
39
40    JPanel panelAdd;
41
42    /**
43     * Create new IncidentPaletteFrame from the form.
44     *
45     * @param scr The currently running script, to which new incidents will be
46     * added.
47     */
48    public IncidentPaletteFrame(SimulationScript scr, ScriptBuilderFrame frame)
49    {
50        script = scr;
51
52        parent = frame;
53
54        panelAdd = new JPanel();
55
56        dummyScript = new SimulationScript();
57
58        incidentList = new ArrayList<ScriptIncident>();
59
60        panelAdd.setLayout(new GridLayout(0, 1));
61
62        initComponents();
63
64        incidentList = loadIncidentsFromFiles("Incidents");
65
66        refresh();
67
68    }
69
70    private ArrayList<ScriptIncident> loadIncidentsFromFiles(String directoryName)
71    {
72
73        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
74        File folder = new File(directoryName);
75
76        File[] incidentFiles;
77
78        ExtensionFileFilter filter = new ExtensionFileFilter("Script Incident (.xml)", new String[]
79        {
80            "xml"
81        });
82
83        incidentFiles = folder.listFiles();
84
85        for (File file : incidentFiles)
86        {
87            if (filter.accept(file))
88            {
89                dummyScript = new SimulationScript();
90                dummyScript.loadScriptFromFile(file);
91
92                newList.add(dummyScript.incidents.get(0));
93            }
94        }
95
96        return newList;
97    }
98
99    private ArrayList<ScriptIncident> filterIncidentsInScript(ArrayList<ScriptIncident> list)
100    {
101        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
102
103        for (ScriptIncident inc : list)
104        {
105            if (!scriptContainsLogNum(script, inc.number))
106            {
107                newList.add(inc);
108            }
109        }
110
111        return newList;
112    }
113
114    private boolean scriptContainsLogNum(SimulationScript script, int num)
115    {
116        for (ScriptIncident incident : script.incidents)
117        {
118            if (incident != null && incident.number == num)
119            {
120                return true;
121            }
122        }
123
124        return false;
125    }
126
127    private void loadPanels(ArrayList<ScriptIncident> incList)
128    {
129        scrollAddPanels.setViewport(new JViewport());
130        panelAdd.removeAll();
131        labelFileCount.setText("Found " + incList.size() + " available incidents.");
132        for (ScriptIncident inc : incList)
133        {
134
135            panelAdd.add(new IncidentPaletteAddPanel(inc, this));
136        }
137        scrollAddPanels.getViewport().add(panelAdd);
138    }
139
140    private void refresh()
141    {
142
143        incidentList = filterIncidentsInScript(incidentList);
144        loadPanels(incidentList);
145        getCurrentlyLoadedIncidents();
146    }
147
148    /**
149     * Attempt to add an incident to the script. If successful, refresh the
150     * palette.
151     *
152     * @param incPanel the Add Panel which holds the incident to be added.
153     */
154    public void addIncidentToScript(IncidentPaletteAddPanel incPanel)
155    {
156        if (script.addIncident(incPanel.incident))
157        {
158            refresh();
159        }
160    }
161
162    private void getCurrentlyLoadedIncidents()
163    {
164        tableCurrentIncidents.removeAll();
165        int currentRow = 0;
166        for (ScriptIncident inc : script.incidents)
167        {
168            if (inc != null)
169            {
170                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
171                df.setTimeZone(TimeZone.getTimeZone("GMT"));
172                String incStart = df.format(new Date(inc.offset * 1000));
173                tableCurrentIncidents.getModel().setValueAt(inc.number, currentRow, 0);
174                tableCurrentIncidents.getModel().setValueAt(incStart, currentRow, 1);
175                tableCurrentIncidents.getModel().setValueAt(inc.name, currentRow, 2);
176                currentRow++;
177            }
178        }
179        labelIncidentCount.setText(currentRow + " incidents currently in script.");
180    }
181
182    /**
183     * This method is called from within the constructor to initialize the form.
184     * WARNING: Do NOT modify this code. The content of this method is always
185     * regenerated by the Form Editor.
186     */
187    @SuppressWarnings("unchecked")
188    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
189    private void initComponents()
190    {
191
192        jPanel1 = new javax.swing.JPanel();
193        btnCreateIncident = new javax.swing.JButton();
194        txtSearchFilter = new javax.swing.JTextField();
195        jPanel2 = new javax.swing.JPanel();
196        jScrollPane2 = new javax.swing.JScrollPane();
197        tableCurrentIncidents = new javax.swing.JTable();
198        labelIncidentCount = new javax.swing.JLabel();
199        scrollAddPanels = new javax.swing.JScrollPane();
200        jPanel3 = new javax.swing.JPanel();
201        btnClosePalette = new javax.swing.JButton();
202        labelFileCount = new javax.swing.JLabel();
203
204        btnCreateIncident.setText("Create New...");
205        btnCreateIncident.addActionListener(new java.awt.event.ActionListener()
206        {
207            public void actionPerformed(java.awt.event.ActionEvent evt)
208            {
209                btnCreateIncidentActionPerformed(evt);
210            }
211        });
212
213        txtSearchFilter.setText("Search...");
214
215        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
216        jPanel1.setLayout(jPanel1Layout);
217        jPanel1Layout.setHorizontalGroup(
218            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
219            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
220                .addContainerGap()
221                .addComponent(txtSearchFilter)
222                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
223                .addComponent(btnCreateIncident, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
224                .addGap(23, 23, 23))
225        );
226        jPanel1Layout.setVerticalGroup(
227            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
228            .addGroup(jPanel1Layout.createSequentialGroup()
229                .addContainerGap()
230                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
231                    .addComponent(txtSearchFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
232                    .addComponent(btnCreateIncident))
233                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
234        );
235
236        tableCurrentIncidents.setModel(new javax.swing.table.DefaultTableModel(
237            new Object [][]
238            {
239                {null, null, null},
240                {null, null, null},
241                {null, null, null},
242                {null, null, null},
243                {null, null, null},
244                {null, null, null},
245                {null, null, null},
246                {null, null, null},
247                {null, null, null},
248                {null, null, null},
249                {null, null, null}
250            },
251            new String []
252            {
253                "Incident #", "Start Time", "Title"
254            }
255        )
256        {
257            Class[] types = new Class []
258            {
259                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
260            };
261            boolean[] canEdit = new boolean []
262            {
263                false, false, false
264            };
265
266            public Class getColumnClass(int columnIndex)
267            {
268                return types [columnIndex];
269            }
270
271            public boolean isCellEditable(int rowIndex, int columnIndex)
272            {
273                return canEdit [columnIndex];
274            }
275        });
276        tableCurrentIncidents.setToolTipText("");
277        tableCurrentIncidents.getTableHeader().setReorderingAllowed(false);
278        jScrollPane2.setViewportView(tableCurrentIncidents);
279
280        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
281        jPanel2.setLayout(jPanel2Layout);
282        jPanel2Layout.setHorizontalGroup(
283            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
284            .addGroup(jPanel2Layout.createSequentialGroup()
285                .addContainerGap()
286                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
287                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
288                .addComponent(labelIncidentCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
289                .addContainerGap())
290        );
291        jPanel2Layout.setVerticalGroup(
292            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
294            .addComponent(labelIncidentCount, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
295        );
296
297        scrollAddPanels.setMaximumSize(new java.awt.Dimension(99999, 99999));
298        scrollAddPanels.setMinimumSize(new java.awt.Dimension(566, 387));
299        scrollAddPanels.setPreferredSize(new java.awt.Dimension(566, 387));
300
301        btnClosePalette.setText("Done");
302        btnClosePalette.addActionListener(new java.awt.event.ActionListener()
303        {
304            public void actionPerformed(java.awt.event.ActionEvent evt)
305            {
306                btnClosePaletteActionPerformed(evt);
307            }
308        });
309
310        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
311        jPanel3.setLayout(jPanel3Layout);
312        jPanel3Layout.setHorizontalGroup(
313            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
314            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
315                .addContainerGap()
316                .addComponent(labelFileCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
317                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
318                .addComponent(btnClosePalette)
319                .addContainerGap())
320        );
321        jPanel3Layout.setVerticalGroup(
322            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
323            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
324                .addComponent(btnClosePalette)
325                .addComponent(labelFileCount))
326        );
327
328        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
329        getContentPane().setLayout(layout);
330        layout.setHorizontalGroup(
331            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
332            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
333            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
334            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
335            .addGroup(layout.createSequentialGroup()
336                .addContainerGap()
337                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
338                .addContainerGap())
339        );
340        layout.setVerticalGroup(
341            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
342            .addGroup(layout.createSequentialGroup()
343                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
344                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
345                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
346                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
347                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
348                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
349                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
350        );
351
352        pack();
353    }// </editor-fold>//GEN-END:initComponents
354
355    private void btnCreateIncidentActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateIncidentActionPerformed
356    {//GEN-HEADEREND:event_btnCreateIncidentActionPerformed
357        int newLogNum = 100;
358        boolean found = false;
359        while (!found)
360        {
361            newLogNum++;
362            if (!scriptContainsLogNum(script, newLogNum))
363            {
364                found = true;
365                for (ScriptIncident incident : incidentList)
366                {
367                    if (incident.number == newLogNum)
368                    {
369                        found = false;
370                    }
371                }
372            }
373        }
374
375        ScriptIncident newInc = new ScriptIncident(newLogNum, "New Incident", LocalDate.now().toString(), script);
376        if (script.addIncident(newInc))
377        {
378            new IncidentEditorFrame(newInc, parent).setVisible(true);
379            this.dispose();
380        }
381
382    }//GEN-LAST:event_btnCreateIncidentActionPerformed
383
384    private void btnClosePaletteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClosePaletteActionPerformed
385    {//GEN-HEADEREND:event_btnClosePaletteActionPerformed
386        script.update();
387        parent.update(script, script);
388        parent.repaint();
389        this.dispose();
390    }//GEN-LAST:event_btnClosePaletteActionPerformed
391
392
393    // Variables declaration - do not modify//GEN-BEGIN:variables
394    private javax.swing.JButton btnClosePalette;
395    private javax.swing.JButton btnCreateIncident;
396    private javax.swing.JPanel jPanel1;
397    private javax.swing.JPanel jPanel2;
398    private javax.swing.JPanel jPanel3;
399    private javax.swing.JScrollPane jScrollPane2;
400    private javax.swing.JLabel labelFileCount;
401    private javax.swing.JLabel labelIncidentCount;
402    private javax.swing.JScrollPane scrollAddPanels;
403    private javax.swing.JTable tableCurrentIncidents;
404    private javax.swing.JTextField txtSearchFilter;
405    // End of variables declaration//GEN-END:variables
406
407}
Note: See TracBrowser for help on using the repository browser.