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

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

IncidentPaletteFrame?.java now re-sizes the ScriptBuilderFrame?'s timeline panel when the new incident is added. See #214

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        parent.returnFocus();
171        this.dispose();
172    }
173    private ArrayList<ScriptIncident> loadIncidentsFromFiles(String directoryName)
174    {
175        String fs = System.getProperty("file.separator");
176        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
177        File folder = new File(""+System.getProperty("user.dir")+fs+directoryName);
178
179        File[] incidentFiles;
180
181        ExtensionFileFilter filter = new ExtensionFileFilter("Script Incident (.xml)", new String[]
182        {
183            "xml"
184        });
185
186        //If the folder isn't correct then just hand back an empty list
187        if (!folder.exists())
188        {
189            JOptionPane.showMessageDialog(this,
190                    "Unable to locate folder of existing incidents.\n"
191                    + "Folder \"" + incDir + "\" should have been included in the\n"
192                    + "ScriptBuilder.zip file you downloaded.\n"
193                    + "TO FIX: Close the ScriptBuilder program and run it from\n"
194                    + "the same directory where you opened the .zip file.\n\n"
195                    + "(If the problem persists, contact the developers.)", "Missing Folder: \"" + incDir + "\"", JOptionPane.ERROR_MESSAGE);
196            return newList;
197        }
198
199        incidentFiles = folder.listFiles();
200
201        for (File file : incidentFiles)
202        {
203            if (filter.accept(file))
204            {
205                dummyScript = new SimulationScript();
206                dummyScript.loadScriptFromFile(file);
207
208                newList.add(dummyScript.incidents.get(0));
209            }
210        }
211
212        return newList;
213    }
214
215    private ArrayList<ScriptIncident> filterIncidentsInScript(ArrayList<ScriptIncident> list)
216    {
217        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
218
219        for (ScriptIncident inc : list)
220        {
221            if (!scriptContainsLogNum(script, inc.number))
222            {
223                newList.add(inc);
224            }
225        }
226
227        return newList;
228    }
229
230    private boolean scriptContainsLogNum(SimulationScript script, int num)
231    {
232        for (ScriptIncident incident : script.incidents)
233        {
234            if (incident != null && incident.number == num)
235            {
236                return true;
237            }
238        }
239
240        return false;
241    }
242
243    private void loadPanels(ArrayList<ScriptIncident> incList)
244    {
245        scrollAddPanels.setViewport(new JViewport());
246        panelAdd.removeAll();
247        labelFileCount.setText("Found " + incList.size() + " available incidents.");
248        for (ScriptIncident inc : incList)
249        {
250
251            panelAdd.add(new IncidentPaletteAddPanel(inc, this));
252        }
253        scrollAddPanels.getViewport().add(panelAdd);
254    }
255
256    private void refresh()
257    {
258
259        incidentList = filterIncidentsInScript(incidentList);
260        loadPanels(incidentList);
261        getCurrentlyLoadedIncidents();
262    }
263
264    /**
265     * Attempt to add an incident to the script. If successful, refresh the
266     * palette.
267     *
268     * @param incPanel the Add Panel which holds the incident to be added.
269     */
270    public void addIncidentToScript(IncidentPaletteAddPanel incPanel)
271    {
272        if (script.addIncident(incPanel.incident))
273        {
274            refresh();
275        }
276    }
277
278    private void getCurrentlyLoadedIncidents()
279    {
280        tableCurrentIncidents.removeAll();
281        int currentRow = 0;
282        for (ScriptIncident inc : script.incidents)
283        {
284            if (inc != null)
285            {
286                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
287                df.setTimeZone(TimeZone.getTimeZone("GMT"));
288                String incStart = df.format(new Date(inc.offset * 1000));
289                tableCurrentIncidents.getModel().setValueAt(inc.number, currentRow, 0);
290                tableCurrentIncidents.getModel().setValueAt(incStart, currentRow, 1);
291                tableCurrentIncidents.getModel().setValueAt(inc.name, currentRow, 2);
292                currentRow++;
293            }
294        }
295        labelIncidentCount.setText(currentRow + " incidents currently in script.");
296    }
297
298    /**
299     * This method is called from within the constructor to initialize the form.
300     * WARNING: Do NOT modify this code. The content of this method is always
301     * regenerated by the Form Editor.
302     */
303    @SuppressWarnings("unchecked")
304    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
305    private void initComponents() {
306
307        jPanel1 = new javax.swing.JPanel();
308        btnCreateIncident = new javax.swing.JButton();
309        jPanel2 = new javax.swing.JPanel();
310        jScrollPane2 = new javax.swing.JScrollPane();
311        tableCurrentIncidents = new javax.swing.JTable();
312        labelIncidentCount = new javax.swing.JLabel();
313        scrollAddPanels = new javax.swing.JScrollPane();
314        jPanel3 = new javax.swing.JPanel();
315        btnClosePalette = new javax.swing.JButton();
316        labelFileCount = new javax.swing.JLabel();
317
318        setTitle("Incident Palette");
319
320        btnCreateIncident.setText("Create New Incident...");
321        btnCreateIncident.addActionListener(new java.awt.event.ActionListener() {
322            public void actionPerformed(java.awt.event.ActionEvent evt) {
323                btnCreateIncidentActionPerformed(evt);
324            }
325        });
326
327        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
328        jPanel1.setLayout(jPanel1Layout);
329        jPanel1Layout.setHorizontalGroup(
330            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
331            .addGroup(jPanel1Layout.createSequentialGroup()
332                .addContainerGap()
333                .addComponent(btnCreateIncident)
334                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
335        );
336        jPanel1Layout.setVerticalGroup(
337            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
338            .addGroup(jPanel1Layout.createSequentialGroup()
339                .addContainerGap()
340                .addComponent(btnCreateIncident)
341                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
342        );
343
344        tableCurrentIncidents.setModel(new javax.swing.table.DefaultTableModel(
345            new Object [][] {
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                {null, null, null}
357            },
358            new String [] {
359                "Incident #", "Start Time", "Title"
360            }
361        ) {
362            Class[] types = new Class [] {
363                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
364            };
365            boolean[] canEdit = new boolean [] {
366                false, false, false
367            };
368
369            public Class getColumnClass(int columnIndex) {
370                return types [columnIndex];
371            }
372
373            public boolean isCellEditable(int rowIndex, int columnIndex) {
374                return canEdit [columnIndex];
375            }
376        });
377        tableCurrentIncidents.setToolTipText("");
378        tableCurrentIncidents.getTableHeader().setReorderingAllowed(false);
379        jScrollPane2.setViewportView(tableCurrentIncidents);
380
381        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
382        jPanel2.setLayout(jPanel2Layout);
383        jPanel2Layout.setHorizontalGroup(
384            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
385            .addGroup(jPanel2Layout.createSequentialGroup()
386                .addContainerGap()
387                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
388                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
389                .addComponent(labelIncidentCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
390                .addContainerGap())
391        );
392        jPanel2Layout.setVerticalGroup(
393            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
394            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
395            .addComponent(labelIncidentCount, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
396        );
397
398        scrollAddPanels.setMaximumSize(new java.awt.Dimension(99999, 99999));
399        scrollAddPanels.setMinimumSize(new java.awt.Dimension(566, 387));
400        scrollAddPanels.setPreferredSize(new java.awt.Dimension(566, 387));
401
402        btnClosePalette.setText("Done");
403        btnClosePalette.addActionListener(new java.awt.event.ActionListener() {
404            public void actionPerformed(java.awt.event.ActionEvent evt) {
405                btnClosePaletteActionPerformed(evt);
406            }
407        });
408
409        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
410        jPanel3.setLayout(jPanel3Layout);
411        jPanel3Layout.setHorizontalGroup(
412            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
413            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
414                .addContainerGap()
415                .addComponent(labelFileCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
416                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
417                .addComponent(btnClosePalette)
418                .addContainerGap())
419        );
420        jPanel3Layout.setVerticalGroup(
421            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
422            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
423                .addComponent(btnClosePalette)
424                .addComponent(labelFileCount))
425        );
426
427        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
428        getContentPane().setLayout(layout);
429        layout.setHorizontalGroup(
430            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
431            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
432            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
433            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
434            .addGroup(layout.createSequentialGroup()
435                .addContainerGap()
436                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
437                .addContainerGap())
438        );
439        layout.setVerticalGroup(
440            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
441            .addGroup(layout.createSequentialGroup()
442                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
443                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
444                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
445                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
446                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
447                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
448                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
449        );
450
451        pack();
452    }// </editor-fold>//GEN-END:initComponents
453
454    private void btnCreateIncidentActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateIncidentActionPerformed
455    {//GEN-HEADEREND:event_btnCreateIncidentActionPerformed
456       parent.openIncidentCreateWindow();
457       this.dispose();
458//        int newLogNum = 100;
459//        boolean found = false;
460//        while (!found)
461//        {
462//            newLogNum++;
463//            if (!scriptContainsLogNum(script, newLogNum))
464//            {
465//                found = true;
466//                for (ScriptIncident incident : incidentList)
467//                {
468//                    if (incident.number == newLogNum)
469//                    {
470//                        found = false;
471//                    }
472//                }
473//            }
474//        }
475//        parent.
476//
477//        ScriptIncident newInc = new ScriptIncident(newLogNum, "New Incident", LocalDate.now().toString(), script);
478//        if (script.addIncident(newInc))
479//        {
480//            new IncidentEditorFrame(newInc, parent).setVisible(true);
481//            this.dispose();
482//        }
483
484    }//GEN-LAST:event_btnCreateIncidentActionPerformed
485
486    private void btnClosePaletteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClosePaletteActionPerformed
487    {//GEN-HEADEREND:event_btnClosePaletteActionPerformed
488        updateScriptBuilderFrame();
489    }//GEN-LAST:event_btnClosePaletteActionPerformed
490
491
492    // Variables declaration - do not modify//GEN-BEGIN:variables
493    private javax.swing.JButton btnClosePalette;
494    private javax.swing.JButton btnCreateIncident;
495    private javax.swing.JPanel jPanel1;
496    private javax.swing.JPanel jPanel2;
497    private javax.swing.JPanel jPanel3;
498    private javax.swing.JScrollPane jScrollPane2;
499    private javax.swing.JLabel labelFileCount;
500    private javax.swing.JLabel labelIncidentCount;
501    private javax.swing.JScrollPane scrollAddPanels;
502    private javax.swing.JTable tableCurrentIncidents;
503    // End of variables declaration//GEN-END:variables
504
505}
Note: See TracBrowser for help on using the repository browser.