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

Revision 132, 18.4 KB checked in by bmcguffin, 9 years ago (diff)

Name of current working file is now visible in the title bar of the main window. If the user is working on a new file, "untitled1.xml" is displayed.

Default save file name for new files is "untitled1.xml". If the user names a file such that it doesn't end in .xml, the ".xml" suffix is added automatically.

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