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 @ 191

Revision 191, 19.2 KB checked in by sdanthin, 6 years ago (diff)

incidentPaletteFrame.java closing window triggers an update for the parent ScriptBuilderFrame?. Same behavior as clicking the "Done" button. See ticket #210

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