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

Revision 63, 14.1 KB checked in by bmcguffin, 9 years ago (diff)

Created Incident Selection Palette window. Incident files in a given directory (currently hard-coded in) are read into the window and displayed. They can be added to the script, with offsets provided by the user.

RevLine 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package scriptbuilder.gui;
7
8import java.awt.GridLayout;
9import java.io.File;
10import java.io.FileFilter;
11import java.text.SimpleDateFormat;
12import java.time.LocalDate;
13import java.util.ArrayList;
14import java.util.Date;
15import java.util.TimeZone;
16import javax.swing.JPanel;
17import javax.swing.JScrollPane;
18import javax.swing.JViewport;
19import scriptbuilder.gui.panels.IncidentPaletteAddPanel;
20import scriptbuilder.structures.*;
21
22/**
23 *
24 * @author Bryan McGuffin
25 */
26public class IncidentPaletteFrame extends javax.swing.JFrame
27{
28
29    private SimulationScript script;
30
31    private SimulationScript dummyScript;
32
33    public ArrayList<ScriptIncident> incidentList;
34
35    JPanel panelAdd;
36
37    private int incidentCount;
38
39    /**
40     * Creates new form IncidentPaletteFrame
41     */
42    public IncidentPaletteFrame(SimulationScript s)
43    {
44        script = s;
45
46        panelAdd = new JPanel();
47
48        dummyScript = new SimulationScript();
49
50        incidentList = new ArrayList<ScriptIncident>();
51
52        incidentCount = 0;
53
54        panelAdd.setLayout(new GridLayout(0, 1));
55
56        initComponents();
57
58        incidentList = loadIncidentsFromFiles("Incidents");
59
60        recalculateList();
61
62    }
63
64    private ArrayList<ScriptIncident> loadIncidentsFromFiles(String directoryName)
65    {
66
67        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
68        File folder = new File(directoryName);
69
70        File[] incidentFiles;
71
72        ExtensionFileFilter filter = new ExtensionFileFilter("Script Incident (.xml)", new String[]
73        {
74            "xml"
75        });
76
77        incidentFiles = folder.listFiles();
78
79        for (File file : incidentFiles)
80        {
81            if (filter.accept(file))
82            {
83                dummyScript = new SimulationScript();
84                dummyScript.loadScriptFromFile(file);
85
86                newList.add(dummyScript.incidents.get(0));
87            }
88        }
89
90        return newList;
91    }
92
93    private ArrayList<ScriptIncident> filterIncidentsInScript(ArrayList<ScriptIncident> list)
94    {
95        ArrayList<ScriptIncident> newList = new ArrayList<ScriptIncident>();
96
97        for (ScriptIncident inc : list)
98        {
99            if (!scriptContainsLogNum(script, inc.number))
100            {
101                newList.add(inc);
102            }
103        }
104
105        return newList;
106    }
107
108    private boolean scriptContainsLogNum(SimulationScript script, int num)
109    {
110        for (ScriptIncident incident : script.incidents)
111        {
112            if (incident != null && incident.number == num)
113            {
114                return true;
115            }
116        }
117
118        return false;
119    }
120
121    private void loadPanels(ArrayList<ScriptIncident> incList)
122    {
123        incidentCount = 0;
124        scrollAddPanels.setViewport(new JViewport());
125        panelAdd.removeAll();
126        for (ScriptIncident inc : incList)
127        {
128            ++incidentCount;
129
130            panelAdd.add(new IncidentPaletteAddPanel(inc, this));
131
132            System.out.println(panelAdd.getComponents().length);
133
134        }
135        scrollAddPanels.getViewport().add(panelAdd);
136    }
137
138    private void recalculateList()
139    {
140
141        incidentList = filterIncidentsInScript(incidentList);
142        loadPanels(incidentList);
143        getCurrentlyLoadedIncidents();
144    }
145
146    public void addIncidentToScript(IncidentPaletteAddPanel incPanel)
147    {
148        if (script.addIncident(incPanel.incident))
149        {
150            recalculateList();
151        }
152    }
153
154    private void getCurrentlyLoadedIncidents()
155    {
156        tableCurrentIncidents.removeAll();
157        int currentRow = 0;
158        for (ScriptIncident inc : script.incidents)
159        {
160            if (inc != null)
161            {
162                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
163                df.setTimeZone(TimeZone.getTimeZone("GMT"));
164                String incStart = df.format(new Date(inc.offset * 1000));
165                tableCurrentIncidents.getModel().setValueAt(inc.number, currentRow, 0);
166                tableCurrentIncidents.getModel().setValueAt(incStart, currentRow, 1);
167                tableCurrentIncidents.getModel().setValueAt(inc.name, currentRow, 2);
168                currentRow++;
169            }
170        }
171    }
172
173    /**
174     * This method is called from within the constructor to initialize the form.
175     * WARNING: Do NOT modify this code. The content of this method is always
176     * regenerated by the Form Editor.
177     */
178    @SuppressWarnings("unchecked")
179    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
180    private void initComponents()
181    {
182
183        jPanel1 = new javax.swing.JPanel();
184        btnCreateIncident = new javax.swing.JButton();
185        txtSearchFilter = new javax.swing.JTextField();
186        jPanel2 = new javax.swing.JPanel();
187        jScrollPane2 = new javax.swing.JScrollPane();
188        tableCurrentIncidents = new javax.swing.JTable();
189        labelIncidentCount = new javax.swing.JLabel();
190        scrollAddPanels = new javax.swing.JScrollPane();
191        jPanel3 = new javax.swing.JPanel();
192        btnClosePalette = new javax.swing.JButton();
193
194        btnCreateIncident.setText("Create New...");
195        btnCreateIncident.addActionListener(new java.awt.event.ActionListener()
196        {
197            public void actionPerformed(java.awt.event.ActionEvent evt)
198            {
199                btnCreateIncidentActionPerformed(evt);
200            }
201        });
202
203        txtSearchFilter.setText("Search...");
204
205        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
206        jPanel1.setLayout(jPanel1Layout);
207        jPanel1Layout.setHorizontalGroup(
208            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
209            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
210                .addContainerGap()
211                .addComponent(txtSearchFilter)
212                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
213                .addComponent(btnCreateIncident, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
214                .addGap(23, 23, 23))
215        );
216        jPanel1Layout.setVerticalGroup(
217            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218            .addGroup(jPanel1Layout.createSequentialGroup()
219                .addContainerGap()
220                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
221                    .addComponent(txtSearchFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
222                    .addComponent(btnCreateIncident))
223                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
224        );
225
226        tableCurrentIncidents.setModel(new javax.swing.table.DefaultTableModel(
227            new Object [][]
228            {
229                {null, null, null},
230                {null, null, null},
231                {null, null, null},
232                {null, null, null}
233            },
234            new String []
235            {
236                "Incident #", "Start Time", "Title"
237            }
238        )
239        {
240            Class[] types = new Class []
241            {
242                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
243            };
244            boolean[] canEdit = new boolean []
245            {
246                false, false, false
247            };
248
249            public Class getColumnClass(int columnIndex)
250            {
251                return types [columnIndex];
252            }
253
254            public boolean isCellEditable(int rowIndex, int columnIndex)
255            {
256                return canEdit [columnIndex];
257            }
258        });
259        tableCurrentIncidents.setToolTipText("");
260        tableCurrentIncidents.getTableHeader().setReorderingAllowed(false);
261        jScrollPane2.setViewportView(tableCurrentIncidents);
262
263        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
264        jPanel2.setLayout(jPanel2Layout);
265        jPanel2Layout.setHorizontalGroup(
266            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267            .addGroup(jPanel2Layout.createSequentialGroup()
268                .addContainerGap()
269                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
270                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
271                .addComponent(labelIncidentCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
272                .addContainerGap())
273        );
274        jPanel2Layout.setVerticalGroup(
275            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
276            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
277            .addComponent(labelIncidentCount, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
278        );
279
280        scrollAddPanels.setMaximumSize(new java.awt.Dimension(99999, 99999));
281        scrollAddPanels.setMinimumSize(new java.awt.Dimension(566, 387));
282        scrollAddPanels.setPreferredSize(new java.awt.Dimension(566, 387));
283
284        btnClosePalette.setText("Done");
285        btnClosePalette.addActionListener(new java.awt.event.ActionListener()
286        {
287            public void actionPerformed(java.awt.event.ActionEvent evt)
288            {
289                btnClosePaletteActionPerformed(evt);
290            }
291        });
292
293        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
294        jPanel3.setLayout(jPanel3Layout);
295        jPanel3Layout.setHorizontalGroup(
296            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
297            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
298                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
299                .addComponent(btnClosePalette)
300                .addContainerGap())
301        );
302        jPanel3Layout.setVerticalGroup(
303            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
304            .addComponent(btnClosePalette)
305        );
306
307        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
308        getContentPane().setLayout(layout);
309        layout.setHorizontalGroup(
310            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
311            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
312            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
313            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
314            .addGroup(layout.createSequentialGroup()
315                .addContainerGap()
316                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
317                .addContainerGap())
318        );
319        layout.setVerticalGroup(
320            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
321            .addGroup(layout.createSequentialGroup()
322                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
323                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
324                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
325                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
326                .addComponent(scrollAddPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
327                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
328                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
329        );
330
331        pack();
332    }// </editor-fold>//GEN-END:initComponents
333
334    private void btnClosePaletteActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnClosePaletteActionPerformed
335    {//GEN-HEADEREND:event_btnClosePaletteActionPerformed
336        script.update();
337        this.dispose();
338    }//GEN-LAST:event_btnClosePaletteActionPerformed
339
340    private void btnCreateIncidentActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCreateIncidentActionPerformed
341    {//GEN-HEADEREND:event_btnCreateIncidentActionPerformed
342        int newLogNum = 100;
343        boolean found = false;
344        while (!found)
345        {
346            newLogNum++;
347            if (!scriptContainsLogNum(script, newLogNum))
348            {
349                found = true;
350                for (ScriptIncident incident : incidentList)
351                {
352                    if (incident.number == newLogNum)
353                    {
354                        found = false;
355                    }
356                }
357            }
358        }
359
360        ScriptIncident newInc = new ScriptIncident(newLogNum, "New Incident", LocalDate.now().toString(), script);
361        if (script.addIncident(newInc))
362        {
363            new IncidentEditorFrame(newInc).setVisible(true);
364            this.dispose();
365        }
366
367    }//GEN-LAST:event_btnCreateIncidentActionPerformed
368
369
370    // Variables declaration - do not modify//GEN-BEGIN:variables
371    private javax.swing.JButton btnClosePalette;
372    private javax.swing.JButton btnCreateIncident;
373    private javax.swing.JPanel jPanel1;
374    private javax.swing.JPanel jPanel2;
375    private javax.swing.JPanel jPanel3;
376    private javax.swing.JScrollPane jScrollPane2;
377    private javax.swing.JLabel labelIncidentCount;
378    private javax.swing.JScrollPane scrollAddPanels;
379    private javax.swing.JTable tableCurrentIncidents;
380    private javax.swing.JTextField txtSearchFilter;
381    // End of variables declaration//GEN-END:variables
382
383}
Note: See TracBrowser for help on using the repository browser.