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

Revision 136, 18.2 KB checked in by jdalbey, 9 years ago (diff)

IncidentPalleteFrame?: Hard code name of incident directory to "Incidents".

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