Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/gui/IncidentPaletteFrame.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 76, 15.1 KB checked in by bmcguffin, 9 years ago (diff)

Added javadoc for several files.

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