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

Revision 127, 17.3 KB checked in by bmcguffin, 9 years ago (diff)

Location of incidents folder now settable in application.properties file.

if the program fails to locate the incidents folder, an error message is displayed to the user with suggestions for fixing the error.

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