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

Revision 190, 18.7 KB checked in by sdanthin, 6 years ago (diff)

incidentPaletteFrame.java Fixed GUI relating to Create new button. See ticket #219

RevLine 
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//This is a deprecated feature that originally was used to search for text.
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        jPanel1 = new javax.swing.JPanel();
286        btnCreateIncident = new javax.swing.JButton();
287        jPanel2 = new javax.swing.JPanel();
288        jScrollPane2 = new javax.swing.JScrollPane();
289        tableCurrentIncidents = new javax.swing.JTable();
290        labelIncidentCount = new javax.swing.JLabel();
291        scrollAddPanels = new javax.swing.JScrollPane();
292        jPanel3 = new javax.swing.JPanel();
293        btnClosePalette = new javax.swing.JButton();
294        labelFileCount = new javax.swing.JLabel();
295
296        setTitle("Incident Palette");
297
298        btnCreateIncident.setText("Create New Incident...");
299        btnCreateIncident.addActionListener(new java.awt.event.ActionListener() {
300            public void actionPerformed(java.awt.event.ActionEvent evt) {
301                btnCreateIncidentActionPerformed(evt);
302            }
303        });
304
305        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
306        jPanel1.setLayout(jPanel1Layout);
307        jPanel1Layout.setHorizontalGroup(
308            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
309            .addGroup(jPanel1Layout.createSequentialGroup()
310                .addContainerGap()
311                .addComponent(btnCreateIncident)
312                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
313        );
314        jPanel1Layout.setVerticalGroup(
315            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316            .addGroup(jPanel1Layout.createSequentialGroup()
317                .addContainerGap()
318                .addComponent(btnCreateIncident)
319                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
320        );
321
322        tableCurrentIncidents.setModel(new javax.swing.table.DefaultTableModel(
323            new Object [][] {
324                {null, null, null},
325                {null, null, null},
326                {null, null, null},
327                {null, null, null},
328                {null, null, null},
329                {null, null, null},
330                {null, null, null},
331                {null, null, null},
332                {null, null, null},
333                {null, null, null},
334                {null, null, null}
335            },
336            new String [] {
337                "Incident #", "Start Time", "Title"
338            }
339        ) {
340            Class[] types = new Class [] {
341                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
342            };
343            boolean[] canEdit = new boolean [] {
344                false, false, false
345            };
346
347            public Class getColumnClass(int columnIndex) {
348                return types [columnIndex];
349            }
350
351            public boolean isCellEditable(int rowIndex, int columnIndex) {
352                return canEdit [columnIndex];
353            }
354        });
355        tableCurrentIncidents.setToolTipText("");
356        tableCurrentIncidents.getTableHeader().setReorderingAllowed(false);
357        jScrollPane2.setViewportView(tableCurrentIncidents);
358
359        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
360        jPanel2.setLayout(jPanel2Layout);
361        jPanel2Layout.setHorizontalGroup(
362            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
363            .addGroup(jPanel2Layout.createSequentialGroup()
364                .addContainerGap()
365                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
366                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
367                .addComponent(labelIncidentCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
368                .addContainerGap())
369        );
370        jPanel2Layout.setVerticalGroup(
371            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
372            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
373            .addComponent(labelIncidentCount, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
374        );
375
376        scrollAddPanels.setMaximumSize(new java.awt.Dimension(99999, 99999));
377        scrollAddPanels.setMinimumSize(new java.awt.Dimension(566, 387));
378        scrollAddPanels.setPreferredSize(new java.awt.Dimension(566, 387));
379
380        btnClosePalette.setText("Done");
381        btnClosePalette.addActionListener(new java.awt.event.ActionListener() {
382            public void actionPerformed(java.awt.event.ActionEvent evt) {
383                btnClosePaletteActionPerformed(evt);
384            }
385        });
386
387        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
388        jPanel3.setLayout(jPanel3Layout);
389        jPanel3Layout.setHorizontalGroup(
390            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
391            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
392                .addContainerGap()
393                .addComponent(labelFileCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
394                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
395                .addComponent(btnClosePalette)
396                .addContainerGap())
397        );
398        jPanel3Layout.setVerticalGroup(
399            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
400            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
401                .addComponent(btnClosePalette)
402                .addComponent(labelFileCount))
403        );
404
405        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
406        getContentPane().setLayout(layout);
407        layout.setHorizontalGroup(
408            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
409            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
410            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
411            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
412            .addGroup(layout.createSequentialGroup()
413                .addContainerGap()
414                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
415                .addContainerGap())
416        );
417        layout.setVerticalGroup(
418            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
419            .addGroup(layout.createSequentialGroup()
420                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
421                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
422                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
423                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
424                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
425                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
426                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
427        );
428
429        pack();
430    }// </editor-fold>//GEN-END:initComponents
431
432    private void btnCreateIncidentActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateIncidentActionPerformed
433    {//GEN-HEADEREND:event_btnCreateIncidentActionPerformed
434       parent.openIncidentCreateWindow();
435       this.dispose();
436//        int newLogNum = 100;
437//        boolean found = false;
438//        while (!found)
439//        {
440//            newLogNum++;
441//            if (!scriptContainsLogNum(script, newLogNum))
442//            {
443//                found = true;
444//                for (ScriptIncident incident : incidentList)
445//                {
446//                    if (incident.number == newLogNum)
447//                    {
448//                        found = false;
449//                    }
450//                }
451//            }
452//        }
453//        parent.
454//
455//        ScriptIncident newInc = new ScriptIncident(newLogNum, "New Incident", LocalDate.now().toString(), script);
456//        if (script.addIncident(newInc))
457//        {
458//            new IncidentEditorFrame(newInc, parent).setVisible(true);
459//            this.dispose();
460//        }
461
462    }//GEN-LAST:event_btnCreateIncidentActionPerformed
463
464    private void btnClosePaletteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClosePaletteActionPerformed
465    {//GEN-HEADEREND:event_btnClosePaletteActionPerformed
466        script.update();
467        parent.update(script, script);
468        parent.repaint();
469        this.dispose();
470    }//GEN-LAST:event_btnClosePaletteActionPerformed
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    // End of variables declaration//GEN-END:variables
485
486}
Note: See TracBrowser for help on using the repository browser.