source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/AssignedIncidents.java @ 59

Revision 59, 20.8 KB checked in by jdalbey, 9 years ago (diff)

Merge CAD Client updates for multiple incident view windows.

Line 
1package tmcsim.client.cadclientgui.screens;
2
3import java.awt.Color;
4import java.awt.Component;
5import java.awt.Dimension;
6import java.awt.Point;
7import java.awt.Toolkit;
8import java.awt.datatransfer.DataFlavor;
9import java.awt.datatransfer.Transferable;
10import java.awt.event.ComponentEvent;
11import java.awt.event.ComponentListener;
12import java.awt.event.MouseEvent;
13import java.awt.event.MouseListener;
14import java.awt.event.MouseMotionListener;
15import java.rmi.RemoteException;
16import java.util.List;
17import javax.swing.Box;
18import javax.swing.BoxLayout;
19import javax.swing.JFrame;
20import javax.swing.JLabel;
21import javax.swing.JScrollPane;
22import javax.swing.JSeparator;
23import javax.swing.JTable;
24import javax.swing.ListSelectionModel;
25import javax.swing.RowSorter.SortKey;
26import javax.swing.SwingUtilities;
27import javax.swing.TransferHandler;
28import javax.swing.table.DefaultTableModel;
29import javax.swing.table.TableCellRenderer;
30import tmcsim.client.cadclientgui.enums.CADDataEnums;
31import tmcsim.client.cadclientgui.enums.CADScriptTags.UNIT_TAGS;
32import tmcsim.client.cadclientgui.enums.IncidentEnums;
33import tmcsim.client.cadclientgui.enums.TableHeaders;
34import tmcsim.client.cadclientgui.enums.UnitStatusEnums;
35
36/**
37 * This class contains the view and controller for the AssignedIncidents screen.
38 * The view was not built using a GUI builder plug-in (but may want to consider
39 * doing so in the future), and the controller uses listeners to control how the
40 * view and data act.
41 *
42 * @author Vincent
43 */
44public class AssignedIncidents extends JFrame
45{
46    private final String SCREEN_TITLE = "(Shift + F3)  Assigned Incidents";
47    private final Dimension SCREEN_DIMENSIONS = new Dimension(1400, 250);
48    private final Dimension DROP_DOWN_MENU_LABEL_DIMENSIONS = new Dimension(
49            170, 20);
50    private final Dimension DROP_DOWN_MENU_DIMENSIONS = new Dimension(170, 230);
51    private final int COLUMN_WIDTH = 120;
52    private final String[] LABELS =
53    {
54        "Add Resources...", "Greater Alarm...",
55        "Reconfigure", "Open", "Recall Incident", "Cancel", "Reassign",
56        "Link/Append", "Map", "Recall Linked Incidents", "Read Notes",
57        "Page...", "Mail...", "Fax..."
58    };
59    private final String LABEL_SPACING = "     ";
60    private JTable assignedIncidentsTable;
61    private JFrame assignedIncidentsMenu;
62    // labels for the drop down menu
63    private JLabel[] dropDownLabels = new JLabel[LABELS.length];
64    private long lastLeftClick;// used for double clicking feature
65
66    public AssignedIncidents()
67    {
68        initComponents();
69    }
70
71    private void initComponents()
72    {
73        initializeTable();
74        initController();
75        initializeDropDownMenu();
76
77        JScrollPane scrollpane = new JScrollPane(assignedIncidentsTable);
78        scrollpane.getViewport().setBackground(Color.WHITE);
79
80        setTitle(SCREEN_TITLE);
81        setPreferredSize(SCREEN_DIMENSIONS);
82        getContentPane().add(scrollpane);
83        setResizable(true);
84        pack();
85        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
86        setLocation(0, (int) (dim.getHeight() / 4));
87        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
88        setVisible(false);
89    }
90
91    /*
92     * Initializes the table and prepares the cell renderer for color
93     * management. It initializes the default settings and handles the drag and
94     * drop feature.
95     */
96    private void initializeTable()
97    {
98        assignedIncidentsTable = new JTable()
99        {
100            /*
101             * Custom renderer to set different background/foreground colors
102             * @see javax.swing.JTable#prepareRenderer(javax.swing.table.TableCellRenderer, int, int)
103             */
104            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
105            {
106                Component comp = super.prepareRenderer(renderer, row, column);
107
108                comp.setForeground(Color.BLACK);
109                comp.setBackground(Color.CYAN);
110                if (assignedIncidentsTable.getModel().getColumnName(column).equals("Unit/s"))
111                {//4 is the column for "Unit/s"
112                    //System.out.println("Diagnostic: in AssignedIncidents.prepareRenderer()"); //Commenting this line breaks the client
113                    comp.setBackground(Color.BLACK);
114                    int primaryColumn = 3;
115                    try
116                    {
117                        // get the unit name from the table   JD
118                        String unitname = (String) assignedIncidentsTable.getValueAt(row, primaryColumn);
119                        // Validate that unitname isn't blank
120                        if (!unitname.equals(""))
121                        {
122                            // Fetch the unit's current status from server
123                            UnitStatusEnums ustatus =  ScreenManager.theCoordinator.getCadDataUnitStatus(unitname);
124                            // Decide which color to use for displaying the status
125                            switch (ustatus)
126                            {
127                                case Assignable:
128                                    comp.setForeground(Color.GREEN);
129                                    break;
130                                case Arrived:
131                                    comp.setForeground(Color.YELLOW);
132                                    break;
133                                case Enroute:
134                                    comp.setForeground(Color.CYAN);
135                                    break;
136                            }
137                        }
138                    } catch (RemoteException e)
139                    {
140                        e.printStackTrace();
141                    }
142                }
143
144                if (getSelectedRow() == row)
145                {
146                    comp.setForeground(Color.WHITE);
147                    comp.setBackground(Color.BLUE);
148                }
149
150                return comp;
151            }
152
153            public Class getColumnClass(int c)
154            {
155                return getValueAt(0, c).getClass();
156            }
157        };
158
159        assignedIncidentsTable.setOpaque(true);
160        assignedIncidentsTable.setIntercellSpacing(new Dimension(1, 0));
161        assignedIncidentsTable.setGridColor(Color.WHITE);
162        assignedIncidentsTable.getTableHeader().setReorderingAllowed(false);
163        assignedIncidentsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
164        assignedIncidentsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
165        assignedIncidentsTable.setAutoCreateRowSorter(true);
166        assignedIncidentsTable.setModel(new DefaultTableModel());
167
168        ((DefaultTableModel) assignedIncidentsTable.getModel()).setColumnIdentifiers(TableHeaders.ASSIGNED_INCIDENTS_HEADERS);
169
170        assignedIncidentsTable.setTransferHandler(new TransferHandler()
171        {
172            public boolean canImport(TransferHandler.TransferSupport info)
173            {
174                // Check for String flavor
175                if (!info.isDataFlavorSupported(DataFlavor.stringFlavor))
176                {
177                    return false;
178                }
179                return true;
180            }
181
182            public boolean importData(TransferHandler.TransferSupport info)
183            {
184                if (!info.isDrop())
185                {
186                    return false;
187                }
188
189                DefaultTableModel tableModel = (DefaultTableModel) assignedIncidentsTable.getModel();
190                JTable.DropLocation dl = (JTable.DropLocation) info.getDropLocation();
191                int index = dl.getRow();
192
193                // Get the string that is being dropped.
194                Transferable t = info.getTransferable();
195                String data;
196                try
197                {
198                    data = (String) t.getTransferData(DataFlavor.stringFlavor);
199                } catch (Exception e)
200                {
201                    return false;
202                }
203
204                // Perform the actual import
205                int incidentId = (Integer) assignedIncidentsTable.getValueAt(dl.getRow(), 0);
206                try
207                {
208                    ScreenManager.theCoordinator.setCadDataUnitAssignedId(data, incidentId);
209                    ScreenManager.theCoordinator.setCadDataUnitValue(data,
210                            UNIT_TAGS.UNIT_STATUS, UnitStatusEnums.Arrived);
211                    ScreenManager.theCoordinator.addCadDataIncidentAssignedUnitNum(incidentId, data);
212                    ScreenManager.theCoordinator.setCadDataIncidentStatus(incidentId, IncidentEnums.Assigned);
213                } catch (RemoteException e)
214                {
215                    e.printStackTrace();
216                }
217                ScreenManager.refreshScreens();
218                return true;
219            }
220        });
221
222        for (int i = 0; i < assignedIncidentsTable.getColumnCount(); i++)
223        {
224            assignedIncidentsTable.getColumnModel().getColumn(i).setPreferredWidth(120);
225        }
226    }
227
228    /*
229     * Adds the key and mouse listeners for the table and component listener for
230     * screen.
231     */
232    private void initController()
233    {
234        assignedIncidentsTable.addMouseListener(new MouseListener()
235        {
236            public void mouseClicked(MouseEvent e)
237            {
238                if (SwingUtilities.isLeftMouseButton(e))
239                {
240                    // TODO:  Use e.getClickCount() == 2
241                    if (System.currentTimeMillis() - lastLeftClick < 1000)
242                    {
243                        int idColumn = 0;
244                        int selectedRow = assignedIncidentsTable.getSelectedRow();
245                        try
246                        {
247                            int selectedValue = (Integer) assignedIncidentsTable.getValueAt(selectedRow, idColumn);
248                            ScreenManager.openIncidentViewer(selectedValue);
249                        } catch (IndexOutOfBoundsException ex)
250                        {
251                            ex.printStackTrace();
252                        }
253                    }
254                    else
255                    {
256                        lastLeftClick = System.currentTimeMillis();
257                    }
258                }
259                if (SwingUtilities.isRightMouseButton(e))
260                {
261                    // Fixed to force right click to cause the row to be selected. JD
262                    // get the coordinates of the mouse click
263                    Point p = e.getPoint();
264                    // get the row index that contains that coordinate
265                    int rowNumber = assignedIncidentsTable.rowAtPoint(p);
266                    // Get the ListSelectionModel of the JTable
267                    ListSelectionModel model = assignedIncidentsTable.getSelectionModel();
268                    // set the selected interval of rows. Using the "rowNumber"
269                    // variable for the beginning and end selects only that one row.
270                    model.setSelectionInterval(rowNumber, rowNumber);
271                    // go open the drop down menu
272                    openDropDownMenu(e);
273                }
274                else
275                {
276                    closeDropDownMenu();
277                }
278            }
279
280            public void mouseEntered(MouseEvent e)
281            {
282            }
283
284            public void mouseExited(MouseEvent e)
285            {
286            }
287
288            public void mousePressed(MouseEvent e)
289            {
290            }
291
292            public void mouseReleased(MouseEvent e)
293            {
294            }
295        });
296
297        addComponentListener(new ComponentListener()
298        {
299            public void componentHidden(ComponentEvent e)
300            {
301            }
302
303            public void componentMoved(ComponentEvent e)
304            {
305                closeDropDownMenu();
306            }
307
308            public void componentResized(ComponentEvent e)
309            {
310            }
311
312            public void componentShown(ComponentEvent e)
313            {
314            }
315        });
316    }
317
318    /*
319     * Creates the drop down menu that appears when a right click is performed
320     * on the table.
321     */
322    private void initializeDropDownMenu()
323    {
324        Box menu = new Box(BoxLayout.Y_AXIS);
325        initializeDropDownLabels();
326        addLabelsToBox(menu);
327
328        // Sets the highlighted background color, note that it does not become
329        // "Highlighted" until opaque(true) is called
330        setMenuHighlightedBackground(Color.BLUE);
331
332        assignedIncidentsMenu = new JFrame();
333        assignedIncidentsMenu.getContentPane().add(menu);
334        assignedIncidentsMenu.setPreferredSize(DROP_DOWN_MENU_DIMENSIONS);
335        assignedIncidentsMenu.setUndecorated(true);
336        assignedIncidentsMenu.pack();
337        assignedIncidentsMenu.setVisible(false);
338    }
339
340    /*
341     * Sets the text and size and adds a listener to each activated label.
342     */
343    private void initializeDropDownLabels()
344    {
345        for (int i = 0; i < dropDownLabels.length; i++)
346        {
347            dropDownLabels[i] = new JLabel(LABEL_SPACING + LABELS[i]);
348            dropDownLabels[i].setMaximumSize(DROP_DOWN_MENU_LABEL_DIMENSIONS);
349            dropDownLabels[i].setForeground(Color.GRAY);
350        }
351        dropDownLabels[3].setForeground(Color.BLACK);
352        addMouseListenersToLabel(dropDownLabels[3]);
353        dropDownLabels[10].setForeground(Color.BLACK);
354        addMouseListenersToLabel(dropDownLabels[10]);
355    }
356
357    /*
358     * Add the labels to the box in order with separators and spacings in
359     * between.
360     */
361    private void addLabelsToBox(Box menu)
362    {
363        menu.add(dropDownLabels[0]);
364        menu.add(dropDownLabels[1]);
365        menu.add(dropDownLabels[2]);
366
367        menu.add(Box.createVerticalStrut(5));
368        menu.add(new JSeparator());
369        menu.add(Box.createVerticalStrut(5));
370
371        menu.add(dropDownLabels[3]);
372        menu.add(dropDownLabels[4]);
373        menu.add(dropDownLabels[5]);
374        menu.add(dropDownLabels[6]);
375        menu.add(dropDownLabels[7]);
376
377        menu.add(Box.createVerticalStrut(5));
378        menu.add(new JSeparator());
379        menu.add(Box.createVerticalStrut(5));
380
381        menu.add(dropDownLabels[8]);
382        menu.add(dropDownLabels[9]);
383        menu.add(dropDownLabels[10]);
384
385        menu.add(Box.createVerticalStrut(5));
386        menu.add(new JSeparator());
387        menu.add(Box.createVerticalStrut(5));
388
389        menu.add(dropDownLabels[11]);
390        menu.add(dropDownLabels[12]);
391        menu.add(dropDownLabels[13]);
392
393        menu.add(Box.createVerticalStrut(5));
394    }
395
396    /*
397     * Sets the highlighted color(when the mouse is over it) of the JLabels.
398     * Note: the color is not shown until .setOpaque(true) is called.
399     *
400     * @param color the highlighted color
401     */
402    public void setMenuHighlightedBackground(Color color)
403    {
404        for (int i = 0; i < dropDownLabels.length; i++)
405        {
406            dropDownLabels[i].setBackground(color);
407        }
408    }
409
410    /*
411     * Sets all JLabels to not display a highlighted background
412     */
413    public void unSelectAllLabels()
414    {
415        for (int i = 0; i < dropDownLabels.length; i++)
416        {
417            dropDownLabels[i].setOpaque(false);
418        }
419    }
420
421    /*
422     * Sets the label to have a highlighted background.
423     *
424     * @param label the label that is selected/highlighted
425     */
426    public void selectLabel(Object label)
427    {
428        ((JLabel) label).setOpaque(true);
429    }
430
431    /*
432     * Performs the label action depending on which label was clicked.
433     */
434    public void performLabelAction(Object label)
435    {
436        if (label.equals(dropDownLabels[0]))
437        {// Add Resources
438        }
439        else if (label.equals(dropDownLabels[1]))
440        {// Greater Alarm
441        }
442        else if (label.equals(dropDownLabels[2]))
443        {// Reconfigure
444        }
445        else if (label.equals(dropDownLabels[3]))
446        {// Open
447            int idColumn = 0;
448            int selectedRow = assignedIncidentsTable.getSelectedRow();
449            try
450            {
451                int selectedValue = (Integer) assignedIncidentsTable.getValueAt(selectedRow, idColumn);
452                ScreenManager.openIncidentViewer(selectedValue);
453            } catch (IndexOutOfBoundsException ex)
454            {
455                ex.printStackTrace();
456            }
457        }
458        else if (label.equals(dropDownLabels[4]))
459        {// Recall Incident
460        }
461        else if (label.equals(dropDownLabels[5]))
462        {// Cancel
463        }
464        else if (label.equals(dropDownLabels[6]))
465        {// Reassign
466        }
467        else if (label.equals(dropDownLabels[7]))
468        {// Link append
469        }
470        else if (label.equals(dropDownLabels[8]))
471        {// Map
472        }
473        else if (label.equals(dropDownLabels[9]))
474        {// Recall Linked Incidents
475        }
476        else if (label.equals(dropDownLabels[10]))
477        {// Read Notes
478            int idColumn = 0;
479            ScreenManager.openIncidentViewer((Integer) assignedIncidentsTable
480                    .getValueAt(assignedIncidentsTable.getSelectedRow(),
481                    idColumn));
482        }
483        else if (label.equals(dropDownLabels[11]))
484        {// Page
485        }
486        else if (label.equals(dropDownLabels[12]))
487        {// Mail
488        }
489        else if (label.equals(dropDownLabels[13]))
490        {// Fax
491        }
492    }
493
494    /*
495     * Factory method. Adds a mouse listeners to the label. The
496     * MouseMotionListener detects the mouse's location to highlight the label.
497     * The MouseListener detects for clicks and performs the action of the label
498     * designates.
499     */
500    public void addMouseListenersToLabel(JLabel label)
501    {
502        label.addMouseMotionListener(new MouseMotionListener()
503        {
504            public void mouseDragged(MouseEvent e)
505            {
506            }
507
508            public void mouseMoved(MouseEvent e)
509            {
510                unSelectAllLabels();
511                selectLabel(e.getSource());
512                assignedIncidentsMenu.revalidate();
513                assignedIncidentsMenu.repaint();
514            }
515        });
516        label.addMouseListener(new MouseListener()
517        {
518            public void mouseClicked(MouseEvent e)
519            {
520                performLabelAction(e.getSource());
521                unSelectAllLabels();
522                assignedIncidentsMenu.revalidate();
523                assignedIncidentsMenu.repaint();
524                assignedIncidentsMenu.setVisible(false);
525            }
526
527            public void mouseEntered(MouseEvent e)
528            {
529            }
530
531            public void mouseExited(MouseEvent e)
532            {
533            }
534
535            public void mousePressed(MouseEvent e)
536            {
537            }
538
539            public void mouseReleased(MouseEvent e)
540            {
541            }
542        });
543    }
544
545    /*
546     * Displays the menu where the right click occurred.
547     */
548    public void openDropDownMenu(MouseEvent e)
549    {
550        assignedIncidentsMenu.setLocation(e.getX() + this.getX(), e.getY()
551                + this.getY());
552        assignedIncidentsMenu.setVisible(true);
553    }
554
555    /*
556     * Hides the menu.
557     */
558    public void closeDropDownMenu()
559    {
560        unSelectAllLabels();
561        assignedIncidentsMenu.revalidate();
562        assignedIncidentsMenu.repaint();
563        assignedIncidentsMenu.setVisible(false);
564    }
565
566    /*
567     * Refreshes the data in the table by updating all data and repainting the
568     * screen. It saves user preferences(like column sizes, selected row, sorted preferences)
569     * and applies them to the updated model it receives from the server.
570     */
571    public void refreshTable()
572    {
573
574        if (assignedIncidentsTable.getTableHeader().getResizingColumn() == null)
575        {//only update info if resize not in progress
576            try
577            {
578                int index = assignedIncidentsTable.getSelectedRow();
579                int[] columnWidths = new int[20];
580                List<? extends SortKey> keys = assignedIncidentsTable.getRowSorter().getSortKeys();
581                for (int i = 0; i < assignedIncidentsTable.getColumnCount(); i++)
582                {
583                    columnWidths[i] = assignedIncidentsTable.getColumnModel().getColumn(i).getWidth();
584                }
585
586                assignedIncidentsTable.setModel(ScreenManager.theCoordinator.getCadDataTable(CADDataEnums.TABLE.ASSIGNED_INCIDENTS));
587
588                for (int i = 0; i < assignedIncidentsTable.getColumnCount(); i++)
589                {
590                    assignedIncidentsTable.getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
591                }
592                assignedIncidentsTable.getRowSorter().setSortKeys(keys);
593                assignedIncidentsTable.getSelectionModel().setSelectionInterval(index, index);
594            } catch (RemoteException e)
595            {
596                e.printStackTrace();
597            }
598            revalidate();
599            repaint();
600        }
601    }
602
603    /*
604     * Makes screen visible.
605     */
606    public void open()
607    {
608        setVisible(true);
609    }
610
611    /*
612     * Hides screen.
613     */
614    public void close()
615    {
616        setVisible(false);
617    }
618}
Note: See TracBrowser for help on using the repository browser.