source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/IncidentEditor.java @ 459

Revision 459, 20.3 KB checked in by jdalbey, 7 years ago (diff)

addIncidentTab in SimulationManagerView? modified to be on EDT to fix #154.

Line 
1package tmcsim.client.cadclientgui.screens;
2
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.MouseEvent;
9import java.awt.event.MouseListener;
10import java.rmi.RemoteException;
11import java.text.DateFormat;
12import java.text.SimpleDateFormat;
13import java.util.Calendar;
14import java.util.Date;
15import javax.swing.BorderFactory;
16import javax.swing.DefaultListModel;
17import javax.swing.JLabel;
18import javax.swing.JPanel;
19import javax.swing.JScrollBar;
20import javax.swing.JTable;
21import javax.swing.ListSelectionModel;
22import javax.swing.SwingConstants;
23import javax.swing.border.LineBorder;
24import javax.swing.table.DefaultTableCellRenderer;
25import javax.swing.table.DefaultTableModel;
26import javax.swing.table.TableCellRenderer;
27import tmcsim.client.cadclientgui.enums.CADDataEnums.TABLE;
28import tmcsim.client.cadclientgui.enums.TableHeaders;
29
30/**
31 * This class contains the view and controller for the IncidentEditor screen.
32 * The view is built using a GUI builder plug-in, and the controller uses
33 * listeners to control how the view and data act.  The IncidentEditor is
34 * opened from the Inform CAD main menu.
35 *
36 * @author Stuart
37 */
38public class IncidentEditor extends javax.swing.JFrame
39{
40    private final String[] monthNames =
41    {
42        "January", "February",
43        "March", "April", "May", "June", "July", "August", "September",
44        "October", "November", "December"
45    };
46    private final String[] dayNames =
47    {
48        "Sun", "Mon", "Tue", "Wed",
49        "Thu", "Fri", "Sat"
50    };
51    private DefaultTableModel blankTable;
52    private JTable calendarTable;
53    private int todayRow;
54    private int todayCol;
55
56    /**
57     * Creates new form IncidentEditor
58     */
59    public IncidentEditor()
60    {
61        declareComponents();
62        initControllers();
63        initLayout();
64    }
65
66    /**
67     * This method is called from within the constructor to initialize the form.
68     * WARNING: Do NOT modify this code. The content of this method is always
69     * regenerated by the Form Editor.
70     */
71    @SuppressWarnings("unchecked")
72    // <editor-fold defaultstate="collapsed"
73    // desc="Generated Code">//GEN-BEGIN:initComponents
74    private void declareComponents()
75    {
76
77        incidentLogScroll = new javax.swing.JScrollPane();
78        incidentLogTable = new javax.swing.JTable();
79        agencyTypeLabel = new javax.swing.JLabel();
80        jScrollPane1 = new javax.swing.JScrollPane();
81        agencyTypeList = new javax.swing.JList();
82        currentDatabaseLabel = new javax.swing.JLabel();
83        currentDatabaseCombo = new javax.swing.JComboBox();
84        searchLimitLabel = new javax.swing.JLabel();
85        reopenButton = new javax.swing.JButton();
86        duplicateCellButton = new javax.swing.JButton();
87        searchButton = new javax.swing.JButton();
88        printButton = new javax.swing.JButton();
89        viewButton = new javax.swing.JButton();
90        refreshButton = new javax.swing.JButton();
91        exitButton = new javax.swing.JButton();
92        fromLabel = new javax.swing.JLabel();
93        toLabel = new javax.swing.JLabel();
94        fromCalendar = new javax.swing.JPanel();
95        toCalendar = new javax.swing.JPanel();
96    }
97
98    public MouseListener newMouseListener()
99    {
100        return new MouseListener()
101        {
102            public void mouseClicked(MouseEvent e)
103            {
104                viewButton.setEnabled(true);
105            }
106
107            public void mouseEntered(MouseEvent e)
108            {
109            }
110
111            public void mouseExited(MouseEvent e)
112            {
113            }
114
115            public void mousePressed(MouseEvent e)
116            {
117            }
118
119            public void mouseReleased(MouseEvent e)
120            {
121            }
122        };
123    }
124
125    public ActionListener newSearchActionListener()
126    {
127        return new ActionListener()
128        {
129            public void actionPerformed(ActionEvent e)
130            {
131                ScreenManager.openSearch();
132            }
133        };
134    }
135
136    public ActionListener newViewActionListener()
137    {
138        return new ActionListener()
139        {
140            public void actionPerformed(ActionEvent e)
141            {
142                String masterInc = (String) incidentLogTable.getValueAt(
143                        incidentLogTable.getSelectedRow(), 5);
144                int incidentId = 0;
145                try
146                {
147                    incidentId = ScreenManager.theCoordinator
148                            .getIncidentId(masterInc);
149                } catch (RemoteException e1)
150                {
151                    e1.printStackTrace();
152                }
153                ScreenManager.openIncidentViewer(incidentId);
154            }
155        };
156    }
157
158    public ActionListener newRefreshActionListener()
159    {
160        return new ActionListener()
161        {
162            public void actionPerformed(ActionEvent e)
163            {
164                refreshInformation();
165            }
166        };
167    }
168
169    public ActionListener newExitActionListener()
170    {
171        return new ActionListener()
172        {
173            public void actionPerformed(ActionEvent arg0)
174            {
175                close();
176            }
177        };
178    }
179
180    private void initControllers()
181    {
182
183        incidentLogTable.addMouseListener(newMouseListener());
184
185        searchButton.addActionListener(newSearchActionListener());
186        viewButton.addActionListener(newViewActionListener());
187        refreshButton.addActionListener(newRefreshActionListener());
188        exitButton.addActionListener(newExitActionListener());
189    }
190
191    private void initLayout()
192    {
193        blankTable = new DefaultTableModel();
194        blankTable.setColumnIdentifiers(TableHeaders.INCIDENT_EDITOR_HEADERS);
195
196        setTitle("Incident Editor");
197        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
198        setPreferredSize(new java.awt.Dimension(800, 600));
199
200        incidentLogTable.setOpaque(true);
201        incidentLogTable.setIntercellSpacing(new Dimension(1, 0));
202        incidentLogTable.setGridColor(Color.WHITE);
203        incidentLogTable.getTableHeader().setReorderingAllowed(false);
204        incidentLogTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
205        incidentLogTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
206        incidentLogTable.setAutoCreateRowSorter(true);
207        incidentLogScroll.setViewportView(incidentLogTable);
208
209        agencyTypeLabel.setText("Agency Type:");
210
211        DefaultListModel<String> dlm = new DefaultListModel();
212        dlm.addElement("CHP");
213        agencyTypeList.setModel(dlm);
214        agencyTypeList.setEnabled(false);
215
216        jScrollPane1.setViewportView(agencyTypeList);
217
218        currentDatabaseLabel.setText("Current Database:");
219
220        currentDatabaseCombo.setModel(new javax.swing.DefaultComboBoxModel(
221                new String[]
222        {
223            "System"
224        }));
225        currentDatabaseCombo.setEnabled(false);
226
227        searchLimitLabel.setText("Search Limit On Live System: 30 Days");
228
229        fromCalendar = createCalendar();
230        toCalendar = createCalendar();
231
232        reopenButton.setText("ReOpen");
233        reopenButton.setEnabled(false);
234
235        duplicateCellButton.setText("Duplicate Cell");
236        duplicateCellButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
237        duplicateCellButton.setEnabled(false);
238
239        searchButton.setText("Search");
240
241        printButton.setText("Print");
242        printButton.setEnabled(false);
243
244        viewButton.setText("View");
245        viewButton.setEnabled(false);
246
247        refreshButton.setText("Refresh");
248        exitButton.setText("Exit");
249
250        fromLabel.setText("From...");
251
252        toLabel.setText("To...");
253
254        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
255        getContentPane().setLayout(layout);
256        layout.setHorizontalGroup(
257                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
258                .addComponent(incidentLogScroll)
259                .addGroup(layout.createSequentialGroup()
260                .addContainerGap()
261                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
262                .addComponent(agencyTypeLabel)
263                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
264                .addGroup(layout.createSequentialGroup()
265                .addGap(10, 10, 10)
266                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267                .addComponent(currentDatabaseLabel)
268                .addComponent(currentDatabaseCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
269                .addComponent(searchLimitLabel))
270                .addGap(32, 32, 32)
271                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
272                .addComponent(fromLabel)
273                .addComponent(fromCalendar, 200, 200, 200))
274                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
275                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
276                .addComponent(toLabel)
277                .addComponent(toCalendar, 200, 200, 200))
278                .addGap(25, 25, 25)
279                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
280                .addComponent(reopenButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
281                .addComponent(duplicateCellButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
282                .addComponent(searchButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
283                .addComponent(printButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
284                .addComponent(viewButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
285                .addComponent(refreshButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
286                .addComponent(exitButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
287        layout.setVerticalGroup(
288                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
289                .addGroup(layout.createSequentialGroup()
290                .addComponent(incidentLogScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
291                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
292                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
293                .addComponent(agencyTypeLabel)
294                .addComponent(reopenButton)
295                .addComponent(fromLabel)
296                .addComponent(toLabel))
297                .addGap(2, 2, 2)
298                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
299                .addGroup(layout.createSequentialGroup()
300                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
301                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
302                .addComponent(currentDatabaseLabel)
303                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
304                .addComponent(currentDatabaseCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
305                .addGap(18, 18, 18)
306                .addComponent(searchLimitLabel))
307                .addGroup(layout.createSequentialGroup()
308                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
309                .addComponent(fromCalendar, 150, 150, 150)
310                .addComponent(toCalendar, 150, 150, 150)))
311                .addGroup(layout.createSequentialGroup()
312                .addComponent(duplicateCellButton)
313                .addGap(2, 2, 2)
314                .addComponent(searchButton)
315                .addGap(2, 2, 2)
316                .addComponent(printButton)
317                .addGap(2, 2, 2)
318                .addComponent(viewButton)
319                .addGap(2, 2, 2)
320                .addComponent(refreshButton)
321                .addGap(2, 2, 2)
322                .addComponent(exitButton)))
323                .addGap(0, 41, Short.MAX_VALUE)));
324
325        pack();
326    }// </editor-fold>//GEN-END:initComponents
327
328    public JPanel createCalendar()
329    {
330
331        JPanel calendarPanel = new JPanel();
332        Calendar now = Calendar.getInstance();
333        calendarPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
334        calendarPanel.setLayout(null);
335
336        JLabel monthLabel = new JLabel("New label");
337        monthLabel.setBounds(0, 0, 140, 20);
338        monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
339        monthLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
340        monthLabel.setText(monthNames[now.get(Calendar.MONTH)]);
341        calendarPanel.add(monthLabel);
342
343        JLabel yearLabel = new JLabel("New label");
344        yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
345        yearLabel.setBounds(140, 0, 60, 20);
346        yearLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
347        yearLabel.setText(Integer.toString(now.get(Calendar.YEAR)));
348        calendarPanel.add(yearLabel);
349
350        Date date = new Date();
351        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
352        String dateString = dateFormat.format(date);
353
354        JLabel dateLabel1 = new JLabel("New label");
355        dateLabel1.setBounds(0, 130, 60, 20);
356        dateLabel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
357        dateLabel1.setText(dateString);
358        dateLabel1.setHorizontalAlignment(JLabel.CENTER);
359        calendarPanel.add(dateLabel1);
360
361        JLabel dateLabel2 = new JLabel("New label");
362        dateLabel2.setBounds(140, 130, 60, 20);
363        dateLabel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
364        dateLabel2.setText(dateString);
365        dateLabel2.setHorizontalAlignment(JLabel.CENTER);
366        calendarPanel.add(dateLabel2);
367
368        JScrollBar scrollBar = new JScrollBar();
369        scrollBar.setOrientation(JScrollBar.HORIZONTAL);
370        scrollBar.setBounds(60, 130, 80, 20);
371        scrollBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));
372        scrollBar.setEnabled(false);
373        calendarPanel.add(scrollBar);
374
375        calendarTable = new JTable()
376        {
377            /*
378             * Custom renderer to set different background/foreground colors
379             * @see javax.swing.JTable#prepareRenderer(javax.swing.table.TableCellRenderer, int, int)
380             */
381            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
382            {
383                Component comp = super.prepareRenderer(renderer, row, column);
384                if (todayRow == row && todayCol == column)
385                {
386                    comp.setBackground(Color.BLUE);
387                    comp.setForeground(Color.WHITE);
388                }
389                else
390                {
391                    comp.setBackground(new Color(235, 235, 235));
392                    comp.setForeground(Color.BLACK);
393                }
394                return comp;
395            }
396        };
397        calendarTable.setModel(createDaysModel());
398        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
399        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
400        for (int i = 0; i < dayNames.length; i++)
401        {
402            calendarTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
403        }
404        calendarTable.setEnabled(false);
405        calendarTable.setBounds(0, 20, 200, 110);
406        calendarPanel.add(calendarTable);
407
408        return calendarPanel;
409    }
410
411    protected DefaultTableModel createDaysModel()
412    {
413
414        String[][] emptyModel =
415        {
416            {
417                "", "", "", "", "", "", ""
418            },
419            {
420                "", "", "", "", "", "", ""
421            },
422            {
423                "", "", "", "", "", "", ""
424            },
425            {
426                "", "", "", "", "", "", ""
427            },
428            {
429                "", "", "", "", "", "", ""
430            },
431            {
432                "", "", "", "", "", "", ""
433            },
434            {
435                "", "", "", "", "", "", ""
436            },
437            {
438                "", "", "", "", "", "", ""
439            }
440        };
441
442        String[] emptyHeader =
443        {
444            "", "", "", "", "", "", ""
445        };
446        DefaultTableModel calendarModel = new DefaultTableModel(emptyModel, emptyHeader);
447        int row = 0;
448        int col = 0;
449
450        Calendar today = Calendar.getInstance();
451        int tMonth = today.get(Calendar.MONTH);
452        int tYear = today.get(Calendar.YEAR);
453        int tDay = today.get(Calendar.DAY_OF_MONTH);
454
455        Calendar calendar = Calendar.getInstance();
456        calendar.set(Calendar.MONTH, tMonth);
457        calendar.set(Calendar.YEAR, tYear);
458        calendar.set(Calendar.DAY_OF_MONTH, 1);
459
460        Calendar iterator = (Calendar) calendar.clone();
461        iterator.add(Calendar.DAY_OF_MONTH,
462                -(iterator.get(Calendar.DAY_OF_WEEK) - 1));
463
464        Calendar maximum = (Calendar) calendar.clone();
465        maximum.add(Calendar.MONTH, +1);
466
467        for (int i = 0; i < dayNames.length; i++)
468        {
469            calendarModel.setValueAt(dayNames[i], row, col);
470            col++;
471        }
472        col = 0;
473        row = 1;
474
475        while (iterator.getTimeInMillis() < maximum.getTimeInMillis())
476        {
477            int lMonth = iterator.get(Calendar.MONTH);
478            int lYear = iterator.get(Calendar.YEAR);
479
480            if ((lMonth == tMonth) && (lYear == tYear))
481            {
482                int lDay = iterator.get(Calendar.DAY_OF_MONTH);
483                calendarModel.setValueAt(Integer.toString(lDay), row, col);
484                if ((tMonth == lMonth) && (tYear == lYear) && (tDay == lDay))
485                {
486                    todayRow = row;
487                    todayCol = col;
488                }
489            }
490            iterator.add(Calendar.DAY_OF_YEAR, +1);
491
492            col++;
493            if (col >= calendarModel.getColumnCount())
494            {
495                row++;
496                col = 0;
497            }
498        }
499
500
501        return calendarModel;
502    }
503
504    public void refreshInformation()
505    {
506        try
507        {
508            incidentLogTable.setModel(ScreenManager.theCoordinator
509                    .getCadDataTable(TABLE.INCIDENT_EDITOR));
510        } catch (RemoteException e)
511        {
512            e.printStackTrace();
513        }
514    }
515
516    /*
517     * Makes screen visible.
518     */
519    public void open()
520    {
521        incidentLogTable.setModel(blankTable);
522        viewButton.setEnabled(false);
523        setVisible(true);
524    }
525
526    /*
527     * Hides screen.
528     */
529    public void close()
530    {
531        setVisible(false);
532    }
533    // Variables declaration - do not modify//GEN-BEGIN:variables
534    private javax.swing.JLabel agencyTypeLabel;
535    private javax.swing.JList agencyTypeList;
536    private javax.swing.JComboBox currentDatabaseCombo;
537    private javax.swing.JLabel currentDatabaseLabel;
538    private javax.swing.JButton duplicateCellButton;
539    private javax.swing.JButton exitButton;
540    private javax.swing.JPanel fromCalendar;
541    private javax.swing.JLabel fromLabel;
542    private javax.swing.JScrollPane incidentLogScroll;
543    private javax.swing.JTable incidentLogTable;
544    private javax.swing.JPanel toCalendar;
545    private javax.swing.JScrollPane jScrollPane1;
546    private javax.swing.JButton printButton;
547    private javax.swing.JButton refreshButton;
548    private javax.swing.JButton reopenButton;
549    private javax.swing.JButton searchButton;
550    private javax.swing.JLabel searchLimitLabel;
551    private javax.swing.JLabel toLabel;
552    private javax.swing.JButton viewButton;
553    // End of variables declaration//GEN-END:variables
554}
Note: See TracBrowser for help on using the repository browser.