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

Revision 189, 19.8 KB checked in by sdanthin, 6 years ago (diff)

IncidentPaletteFrame?.java changed behavior of create new in the incident selector to show the incidentFrame window instead of just bringing up the IncidentEditorFrame? itself
ScriptBuilderFrame?.java created separate method to bring up the IncidentFrame? window so that it can be shown/initialized outside of ScriptBuilderFrame? scope

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