source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/UnitStatus.java @ 475

Revision 475, 39.6 KB checked in by jdalbey, 7 years ago (diff)

Coordinator.java, AssignedIncidents?.java, UnitStatus?.java, CADMenu.java: Add comments and error messages. Add revision number to CAD Menu.

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.StringSelection;
10import java.awt.datatransfer.Transferable;
11import java.awt.event.ActionEvent;
12import java.awt.event.ActionListener;
13import java.awt.event.ComponentEvent;
14import java.awt.event.ComponentListener;
15import java.awt.event.MouseEvent;
16import java.awt.event.MouseListener;
17import java.awt.event.MouseMotionListener;
18import java.rmi.RemoteException;
19import java.util.List;
20import java.util.logging.Level;
21import java.util.logging.Logger;
22import javax.swing.Box;
23import javax.swing.BoxLayout;
24import javax.swing.JButton;
25import javax.swing.JComponent;
26import javax.swing.JFrame;
27import javax.swing.JLabel;
28import javax.swing.JScrollPane;
29import javax.swing.JSeparator;
30import javax.swing.JTable;
31import javax.swing.ListSelectionModel;
32import javax.swing.RowSorter.SortKey;
33import javax.swing.SwingUtilities;
34import javax.swing.TransferHandler;
35import javax.swing.table.DefaultTableModel;
36import javax.swing.table.TableCellRenderer;
37import tmcsim.client.cadclientgui.enums.CADDataEnums;
38import tmcsim.client.cadclientgui.enums.CADScriptTags.UNIT_TAGS;
39import tmcsim.client.cadclientgui.enums.TableHeaders;
40import tmcsim.client.cadclientgui.enums.UnitStatusEnums;
41
42/**
43 * This class contains the view and controller for the UnitStatus screen. The
44 * view was not built using a GUI builder plug-in (but may want to consider
45 * doing so in the future), and the controller uses listeners to control how the
46 * view and data act.
47 *
48 * @author Vincent
49 */
50public class UnitStatus extends JFrame
51{
52    private final static int ONE_SECOND = 1000;
53    private static Logger clientLogger = Logger.getLogger("tmcsim.client");
54    private final String SCREEN_TITLE = "(Shift + F4)  Unit Status";
55    private final Dimension SCREEN_DIMENSIONS = new Dimension(1400, 250);
56    private final Dimension DROP_DOWN_MENU_LABEL_DIMENSIONS = new Dimension(
57            170, 20);
58    private final Dimension DROP_DOWN_MENU_DIMENSIONS = new Dimension(170, 300);
59    private final Dimension BUTTON_DIMENSIONS = new Dimension(100, 25);
60    private final int COLUMN_WIDTH = 120;
61    private final String[] LABELS =
62    {
63        "10-8", "OFC", "OOS",
64        "Open Unit Details", "Open Unit Activity Log", "Map",
65        "Change Vehicle", "Unit Poll", "Quick Note...",
66        "Activate User Timer...", "Filters", "Page...", "Roster System",
67        "10-10"
68    };
69    private final String[] WITH_ASSIGNED_INC_LABELS =
70    {
71        "STAGE", "10-97",
72        "10-8", "ASSIGNED ALT...", "ENRT ALT...", "10-97 ALT...",
73        "Open Incident", "Recall Incident", "Open Unit Details",
74        "Open Unit Activity Log", "Map", "Recall Linked Incidents",
75        "Cancel", "Reassign", "Duplicate", "Unit Poll", "Quick Note...",
76        "Activate User Timer...", "Filters", "Page...", "Fax..."
77    };
78    private final String LABEL_SPACING = "     ";
79    // this box holds both the table and the buttons below
80    private Box unitStatusFrame;
81    private JTable unitStatusTable;
82    private JFrame unitStatusMenu;
83    private JFrame unitStatusWithAssignedIncMenu;
84    // labels for the drop down menu
85    private JLabel[] dropDownLabels = new JLabel[LABELS.length];
86    private JLabel[] dropDownWithAssignedIncLabels = new JLabel[WITH_ASSIGNED_INC_LABELS.length];
87    private JButton buttonEnrt;
88    private JButton buttonStage;
89    private JButton button1097;
90    private JButton buttonCode4;
91    private JButton buttonDash1;
92    private JButton buttonDash2;
93    private JButton buttonDash3;
94    private JButton button108;
95    private JButton buttonOFC;
96    private JButton buttonOOS;
97
98    public UnitStatus()
99    {
100        initComponents();
101    }
102
103    private void initComponents()
104    {
105        initializeTable();
106        initControllers();
107        initializeDropDownMenus();
108
109        JScrollPane scrollpane = new JScrollPane(unitStatusTable);
110        scrollpane.getViewport().setBackground(Color.WHITE);
111
112        Box bottomButtons = new Box(BoxLayout.X_AXIS);
113        initializeBottomButtons(bottomButtons);
114
115        unitStatusFrame = new Box(BoxLayout.Y_AXIS);
116        unitStatusFrame.add(bottomButtons);
117
118        unitStatusFrame.add(scrollpane);// holds the table
119        unitStatusFrame.add(bottomButtons);
120
121        setTitle(SCREEN_TITLE);
122        setPreferredSize(SCREEN_DIMENSIONS);
123        getContentPane().add(unitStatusFrame);
124        setResizable(true);
125        pack();
126        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
127        setLocation(0, (int) (dim.getHeight() * 2 / 4));
128        setDefaultCloseOperation(HIDE_ON_CLOSE);
129        setVisible(false);
130    }
131
132    /**
133     * Initializes the table and prepares the cell renderer for color
134     * management. It initializes the default settings and handles the drag and
135     * drop feature.
136     */
137    private void initializeTable()
138    {
139        unitStatusTable = new JTable()
140        {
141            /**
142             * Custom renderer to set different background/foreground colors
143             *
144             * @see
145             * javax.swing.JTable#prepareRenderer(javax.swing.table.TableCellRenderer,
146             * int, int)
147             */
148            public Component prepareRenderer(TableCellRenderer renderer,
149                    int row, int column)
150            {
151                Component comp;
152                // In a rare odd circumstance of timing, this exception is thrown:
153                // http://pastebin.com/f5wtSMHN or
154                // http://pastebin.com/DDJNLVac
155                try
156                {
157                    comp = super.prepareRenderer(renderer, row, column);
158                } catch (Exception ex)
159                {
160                    clientLogger.logp(Level.INFO, "UnitStatus",
161                            "prepareRenderer", "row=" + row + " col=" + column, ex);
162                    // Our workaround is to just return a dummy value.
163                    // It will be erased in the next one second refresh
164                    return new JLabel("?");
165                }
166
167                // Set the cell colors depending on the unit's status
168                int unitNumColumn = 2;
169                try
170                {
171                    String unitNum = (String) unitStatusTable.getValueAt(row, unitNumColumn);
172                   
173                    UnitStatusEnums status = ScreenManager.theCoordinator
174                            .getCadDataUnitStatus(unitNum);
175                    switch (status)
176                    {
177                        case Assignable:
178                            comp.setForeground(Color.GREEN);
179                            comp.setBackground(Color.BLACK);
180                            break;
181                        case Arrived:
182                            comp.setForeground(Color.YELLOW);
183                            comp.setBackground(Color.BLACK);
184                            break;
185                        case NotAssignable:
186                            comp.setForeground(Color.BLACK);
187                            comp.setBackground(Color.GRAY);
188                            break;
189                        case Enroute:
190                            comp.setForeground(Color.CYAN);
191                            comp.setBackground(Color.BLACK);
192                            break;
193                    }
194                } catch (RemoteException e)
195                {
196                    e.printStackTrace();
197                }
198
199                if (getSelectedRow() == row)
200                {
201                    comp.setForeground(Color.WHITE);
202                    comp.setBackground(Color.BLUE);
203                }
204
205                return comp;
206            }
207        };
208
209        unitStatusTable.setOpaque(true);
210        unitStatusTable.setIntercellSpacing(new Dimension(1, 0));
211        unitStatusTable.setGridColor(Color.WHITE);
212        unitStatusTable.getTableHeader().setReorderingAllowed(false);
213        unitStatusTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
214        unitStatusTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
215        unitStatusTable.setAutoCreateRowSorter(true);
216        unitStatusTable.setDragEnabled(true);
217
218        unitStatusTable.setModel(new DefaultTableModel());
219       
220        ((DefaultTableModel) unitStatusTable.getModel())
221                .setColumnIdentifiers(TableHeaders.UNIT_STATUS_HEADERS);
222
223        unitStatusTable.setTransferHandler(new TransferHandler()
224        {
225            public boolean canImport(TransferHandler.TransferSupport info)
226            {
227                if (!info.isDataFlavorSupported(DataFlavor.stringFlavor))
228                {
229                    return false;
230                }
231                return true;
232            }
233
234            public int getSourceActions(JComponent c)
235            {
236                return TransferHandler.COPY_OR_MOVE;
237            }
238
239            protected Transferable createTransferable(JComponent c)
240            {
241                return new StringSelection((String) unitStatusTable.getValueAt(
242                        unitStatusTable.getSelectedRow(), 2));
243            }
244        });
245
246        for (int i = 0; i < unitStatusTable.getColumnCount(); i++)
247        {
248            unitStatusTable.getColumnModel().getColumn(i)
249                    .setPreferredWidth(COLUMN_WIDTH);
250        }
251    }
252
253    /*
254     * Adds the key and mouse listeners for the table and component listener for
255     * screen.
256     */
257//TODO: JD    If one unit has been selected, but you right-click on a different one.
258//Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
259//      at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:514)
260//      at javax.swing.JTable.convertRowIndexToModel(JTable.java:2642)
261//      at javax.swing.JTable.getValueAt(JTable.java:2717)
262//      at tmcsim.client.cadclientgui.screens.UnitStatus$3.mouseClicked(UnitStatus.java:234)
263    private void initControllers()
264    {
265        unitStatusTable.addMouseListener(new MouseListener()
266        {
267            public void mouseClicked(MouseEvent e)
268            {
269                int unitColumn = 2;
270                // Fixed to force right click to cause the row to be selected. JD
271                // get the coordinates of the mouse click
272                Point p = e.getPoint();
273                // get the row index that contains that coordinate
274                int rowNumber = unitStatusTable.rowAtPoint(p);
275                // Get the ListSelectionModel of the JTable
276                ListSelectionModel model = unitStatusTable.getSelectionModel();
277                // set the selected interval of rows. Using the "rowNumber"
278                // variable for the beginning and end selects only that one row.
279                model.setSelectionInterval(rowNumber, rowNumber);
280
281                String unitNum = (String) unitStatusTable.getValueAt(
282                        rowNumber, unitColumn);
283                // Validity check for non-empty unit number
284                if (unitNum.equals(""))
285                {
286                    return;
287                }
288                if (SwingUtilities.isRightMouseButton(e))
289                {
290                    try
291                    {
292                        if (ScreenManager.theCoordinator
293                                .getCadDataUnitStatus(unitNum) == UnitStatusEnums.Enroute
294                                || ScreenManager.theCoordinator
295                                .getCadDataUnitStatus(unitNum) == UnitStatusEnums.Arrived)
296                        {
297                            openDropDownWithAssignedIncMenu(e);
298                        }
299                        else
300                        {
301                            openDropDownMenu(e);
302                        }
303                    } catch (RemoteException e1)
304                    {
305                        e1.printStackTrace();
306                    }
307                }
308                else
309                {
310                    closeDropDownMenu();
311                    closeDropDownWithAssignedIncMenu();
312                }
313            }
314
315            public void mouseEntered(MouseEvent e)
316            {
317            }
318
319            public void mouseExited(MouseEvent e)
320            {
321            }
322
323            public void mousePressed(MouseEvent e)
324            {
325            }
326
327            public void mouseReleased(MouseEvent e)
328            {
329            }
330        });
331
332        addComponentListener(new ComponentListener()
333        {
334            public void componentHidden(ComponentEvent e)
335            {
336            }
337
338            public void componentMoved(ComponentEvent e)
339            {
340                closeDropDownMenu();
341            }
342
343            public void componentResized(ComponentEvent e)
344            {
345            }
346
347            public void componentShown(ComponentEvent e)
348            {
349            }
350        });
351    }
352
353    /*
354     * Creates the drop down menu that appears when a right click is performed
355     * on the table.
356     */
357    private void initializeDropDownMenus()
358    {
359        Box menu = new Box(BoxLayout.Y_AXIS);
360        initializeDropDownLabels();
361        addLabelsToBox(menu);
362
363        setMenuHighlightedBackground(Color.BLUE);
364
365        unitStatusMenu = new JFrame();
366        unitStatusMenu.getContentPane().add(menu);
367        unitStatusMenu.setPreferredSize(DROP_DOWN_MENU_DIMENSIONS);
368        unitStatusMenu.setUndecorated(true);
369        unitStatusMenu.pack();
370        unitStatusMenu.setVisible(false);
371
372        menu = new Box(BoxLayout.Y_AXIS);
373        initializeDropDownWithAssignedIncLabels();
374        addWithAssignedIncLabelsToBox(menu);
375
376        setWithAssignedIncMenuHighlightedBackground(Color.BLUE);
377
378        unitStatusWithAssignedIncMenu = new JFrame();
379        unitStatusWithAssignedIncMenu.getContentPane().add(menu);
380        unitStatusWithAssignedIncMenu
381                .setPreferredSize(DROP_DOWN_MENU_DIMENSIONS);
382        unitStatusWithAssignedIncMenu.setUndecorated(true);
383        unitStatusWithAssignedIncMenu.pack();
384        unitStatusWithAssignedIncMenu.setVisible(false);
385    }
386
387    /*
388     * Sets the text and size and adds a listener to each label. Currently,
389     * labels are not implemented so no listeners are added.
390     */
391    private void initializeDropDownLabels()
392    {
393        for (int i = 0; i < dropDownLabels.length; i++)
394        {
395            dropDownLabels[i] = new JLabel(LABEL_SPACING + LABELS[i]);
396            dropDownLabels[i].setMaximumSize(DROP_DOWN_MENU_LABEL_DIMENSIONS);
397            dropDownLabels[i].setForeground(Color.GRAY);
398        }
399    }
400
401    /*
402     * Sets the text and size and adds a listener to each activated label.
403     */
404    private void initializeDropDownWithAssignedIncLabels()
405    {
406        for (int i = 0; i < dropDownWithAssignedIncLabels.length; i++)
407        {
408            dropDownWithAssignedIncLabels[i] = new JLabel(LABEL_SPACING
409                    + WITH_ASSIGNED_INC_LABELS[i]);
410            dropDownWithAssignedIncLabels[i]
411                    .setMaximumSize(DROP_DOWN_MENU_LABEL_DIMENSIONS);
412            dropDownWithAssignedIncLabels[i].setForeground(Color.GRAY);
413        }
414        dropDownWithAssignedIncLabels[6].setForeground(Color.BLACK);
415        addMouseListenersToWithAssignedIncLabel(dropDownWithAssignedIncLabels[6]);
416
417    }
418
419    /*
420     * Add the labels to the box in order with separators and spacings in
421     * between.
422     */
423    private void addLabelsToBox(Box menu)
424    {
425        menu.add(dropDownLabels[0]);
426        menu.add(dropDownLabels[1]);
427        menu.add(dropDownLabels[2]);
428
429        menu.add(Box.createVerticalStrut(5));
430        menu.add(new JSeparator());
431        menu.add(Box.createVerticalStrut(5));
432
433        menu.add(dropDownLabels[3]);
434        menu.add(dropDownLabels[4]);
435        menu.add(dropDownLabels[5]);
436
437        menu.add(Box.createVerticalStrut(5));
438        menu.add(new JSeparator());
439        menu.add(Box.createVerticalStrut(5));
440
441        menu.add(dropDownLabels[6]);
442        menu.add(dropDownLabels[7]);
443        menu.add(dropDownLabels[8]);
444
445        menu.add(Box.createVerticalStrut(5));
446        menu.add(new JSeparator());
447        menu.add(Box.createVerticalStrut(5));
448
449        menu.add(dropDownLabels[9]);
450        menu.add(dropDownLabels[10]);
451
452        menu.add(Box.createVerticalStrut(5));
453        menu.add(new JSeparator());
454        menu.add(Box.createVerticalStrut(5));
455
456        menu.add(dropDownLabels[11]);
457
458        menu.add(Box.createVerticalStrut(5));
459        menu.add(new JSeparator());
460        menu.add(Box.createVerticalStrut(5));
461
462        menu.add(dropDownLabels[12]);
463
464        menu.add(Box.createVerticalStrut(5));
465        menu.add(new JSeparator());
466        menu.add(Box.createVerticalStrut(5));
467
468        menu.add(dropDownLabels[13]);
469
470        menu.add(Box.createVerticalStrut(5));
471    }
472
473    /*
474     * Add the labels to the box in order with separators and spacings in
475     * between.
476     */
477    private void addWithAssignedIncLabelsToBox(Box menu)
478    {
479        menu.add(dropDownWithAssignedIncLabels[0]);
480        menu.add(dropDownWithAssignedIncLabels[1]);
481        menu.add(dropDownWithAssignedIncLabels[2]);
482        menu.add(dropDownWithAssignedIncLabels[3]);
483        menu.add(dropDownWithAssignedIncLabels[4]);
484        menu.add(dropDownWithAssignedIncLabels[5]);
485
486        menu.add(Box.createVerticalStrut(5));
487        menu.add(new JSeparator());
488        menu.add(Box.createVerticalStrut(5));
489
490        menu.add(dropDownWithAssignedIncLabels[6]);
491        menu.add(dropDownWithAssignedIncLabels[7]);
492        menu.add(dropDownWithAssignedIncLabels[8]);
493        menu.add(dropDownWithAssignedIncLabels[9]);
494        menu.add(dropDownWithAssignedIncLabels[10]);
495        menu.add(dropDownWithAssignedIncLabels[11]);
496
497        menu.add(Box.createVerticalStrut(5));
498        menu.add(new JSeparator());
499        menu.add(Box.createVerticalStrut(5));
500
501        menu.add(dropDownWithAssignedIncLabels[12]);
502        menu.add(dropDownWithAssignedIncLabels[13]);
503        menu.add(dropDownWithAssignedIncLabels[14]);
504
505        menu.add(Box.createVerticalStrut(5));
506        menu.add(new JSeparator());
507        menu.add(Box.createVerticalStrut(5));
508
509        menu.add(dropDownWithAssignedIncLabels[15]);
510        menu.add(dropDownWithAssignedIncLabels[16]);
511
512        menu.add(Box.createVerticalStrut(5));
513        menu.add(new JSeparator());
514        menu.add(Box.createVerticalStrut(5));
515
516        menu.add(dropDownWithAssignedIncLabels[17]);
517        menu.add(dropDownWithAssignedIncLabels[18]);
518
519        menu.add(Box.createVerticalStrut(5));
520        menu.add(new JSeparator());
521        menu.add(Box.createVerticalStrut(5));
522
523        menu.add(dropDownWithAssignedIncLabels[19]);
524        menu.add(dropDownWithAssignedIncLabels[20]);
525
526        menu.add(Box.createVerticalStrut(5));
527    }
528
529    /*
530     * Creates each button and handles the action performed by the button.
531     */
532    public void initializeBottomButtons(Box bottomButtons)
533    {
534        bottomButtons.add(buttonEnrt = makeButton("ENRT", new ActionListener()
535        {
536            public void actionPerformed(ActionEvent arg0)
537            {
538                try
539                {
540                    ScreenManager.theCoordinator.setCadDataUnitValue(
541                            (String) unitStatusTable.getValueAt(
542                            unitStatusTable.getSelectedRow(), 2),
543                            UNIT_TAGS.STATUS, "ENRT");
544                    ScreenManager.theCoordinator.setCadDataUnitValue(
545                            (String) unitStatusTable.getValueAt(
546                            unitStatusTable.getSelectedRow(), 2),
547                            UNIT_TAGS.UNIT_STATUS, UnitStatusEnums.Enroute);
548                } catch (RemoteException e)
549                {
550                    e.printStackTrace();
551                }
552                refreshTable();
553            }
554        }));
555        bottomButtons.add(buttonStage = makeButton("STAGE",
556                new ActionListener()
557        {
558            public void actionPerformed(ActionEvent arg0)
559            {
560                try
561                {
562                    ScreenManager.theCoordinator.setCadDataUnitValue(
563                            (String) unitStatusTable
564                            .getValueAt(unitStatusTable
565                            .getSelectedRow(), 2),
566                            UNIT_TAGS.STATUS, "STAGE");
567                } catch (RemoteException e)
568                {
569                    e.printStackTrace();
570                }
571                refreshTable();
572            }
573        }));
574        bottomButtons.add(button1097 = makeButton("10-97",
575                new ActionListener()
576        {
577            public void actionPerformed(ActionEvent arg0)
578            {
579                try
580                {
581                    ScreenManager.theCoordinator.setCadDataUnitValue(
582                            (String) unitStatusTable
583                            .getValueAt(unitStatusTable
584                            .getSelectedRow(), 2),
585                            UNIT_TAGS.STATUS, "10-97");
586                    ScreenManager.theCoordinator.setCadDataUnitValue(
587                            (String) unitStatusTable
588                            .getValueAt(unitStatusTable
589                            .getSelectedRow(), 2),
590                            UNIT_TAGS.UNIT_STATUS,
591                            UnitStatusEnums.Arrived);
592                } catch (RemoteException e)
593                {
594                    e.printStackTrace();
595                }
596                refreshTable();
597            }
598        }));
599        bottomButtons.add(buttonCode4 = makeButton("CODE 4",
600                new ActionListener()
601        {
602            public void actionPerformed(ActionEvent arg0)
603            {
604                try
605                {
606                    ScreenManager.theCoordinator.setCadDataUnitValue(
607                            (String) unitStatusTable
608                            .getValueAt(unitStatusTable
609                            .getSelectedRow(), 2),
610                            UNIT_TAGS.STATUS, "CODE 4");
611                } catch (RemoteException e)
612                {
613                    e.printStackTrace();
614                }
615                refreshTable();
616            }
617        }));
618        bottomButtons.add(buttonDash1 = makeButton("-", null));
619        bottomButtons.add(buttonDash2 = makeButton("-", null));
620        bottomButtons.add(buttonDash3 = makeButton("-", null));
621        bottomButtons.add(button108 = makeButton("10-8", new ActionListener()
622        {
623            public void actionPerformed(ActionEvent arg0)
624            {
625                try
626                {
627                    ScreenManager.theCoordinator.setCadDataUnitValue(
628                            (String) unitStatusTable.getValueAt(
629                            unitStatusTable.getSelectedRow(), 2),
630                            UNIT_TAGS.STATUS, "10-8");
631                    ScreenManager.theCoordinator.setCadDataUnitValue(
632                            (String) unitStatusTable.getValueAt(
633                            unitStatusTable.getSelectedRow(), 2),
634                            UNIT_TAGS.UNIT_STATUS, UnitStatusEnums.Assignable);
635                } catch (RemoteException e)
636                {
637                    e.printStackTrace();
638                }
639                refreshTable();
640            }
641        }));
642        bottomButtons.add(buttonOFC = makeButton("OFC", new ActionListener()
643        {
644            public void actionPerformed(ActionEvent arg0)
645            {
646                try
647                {
648                    ScreenManager.theCoordinator.setCadDataUnitValue(
649                            (String) unitStatusTable.getValueAt(
650                            unitStatusTable.getSelectedRow(), 2),
651                            UNIT_TAGS.STATUS, "OFC");
652                    ScreenManager.theCoordinator.setCadDataUnitValue(
653                            (String) unitStatusTable.getValueAt(
654                            unitStatusTable.getSelectedRow(), 2),
655                            UNIT_TAGS.UNIT_STATUS, UnitStatusEnums.Arrived);
656                } catch (RemoteException e)
657                {
658                    e.printStackTrace();
659                }
660                refreshTable();
661            }
662        }));
663        bottomButtons.add(buttonOOS = makeButton("OOS", new ActionListener()
664        {
665            public void actionPerformed(ActionEvent arg0)
666            {
667                try
668                {
669                    ScreenManager.theCoordinator.setCadDataUnitValue(
670                            (String) unitStatusTable.getValueAt(
671                            unitStatusTable.getSelectedRow(), 2),
672                            UNIT_TAGS.STATUS, "OOS");
673                    ScreenManager.theCoordinator.setCadDataUnitValue(
674                            (String) unitStatusTable.getValueAt(
675                            unitStatusTable.getSelectedRow(), 2),
676                            UNIT_TAGS.UNIT_STATUS,
677                            UnitStatusEnums.NotAssignable);
678                } catch (RemoteException e)
679                {
680                    e.printStackTrace();
681                }
682                refreshTable();
683            }
684        }));
685    }
686
687    /*
688     * Makes a JButton with an text and listener.
689     *
690     * @param text the text to be displayed
691     *
692     * @param listener the action listener for this button.
693     *
694     * @return the JButton.
695     */
696    public JButton makeButton(String text, ActionListener listener)
697    {
698        JButton button = new JButton(text);
699        button.setBackground(Color.GRAY);
700        button.setOpaque(false);
701        Dimension size = BUTTON_DIMENSIONS;
702        button.setPreferredSize(size);
703        button.setMinimumSize(size);
704        button.setMaximumSize(size);
705        button.addActionListener(listener);
706        button.setFocusable(false);
707
708        return button;
709    }
710
711    /*
712     * Sets the highlighted color(when the mouse is over it) of the JLabels.
713     * Note: the color is not shown until .setOpaque(true) is called.
714     *
715     * @param color the highlighted color
716     */
717    public void setMenuHighlightedBackground(Color color)
718    {
719        for (int i = 0; i < dropDownLabels.length; i++)
720        {
721            dropDownLabels[i].setBackground(color);
722        }
723    }
724
725    /*
726     * Sets the highlighted color(when the mouse is over it) of the JLabels.
727     * Note: the color is not shown until .setOpaque(true) is called.
728     *
729     * @param color the highlighted color
730     */
731    public void setWithAssignedIncMenuHighlightedBackground(Color color)
732    {
733        for (int i = 0; i < dropDownWithAssignedIncLabels.length; i++)
734        {
735            dropDownWithAssignedIncLabels[i].setBackground(color);
736        }
737    }
738
739    /*
740     * Sets all JLabels to not display a highlighted background
741     */
742    public void unSelectAllLabels()
743    {
744        for (int i = 0; i < dropDownLabels.length; i++)
745        {
746            dropDownLabels[i].setOpaque(false);
747        }
748    }
749
750    /*
751     * Sets all JLabels to not display a highlighted background
752     */
753    public void unSelectAllWithAssignedIncLabels()
754    {
755        for (int i = 0; i < dropDownWithAssignedIncLabels.length; i++)
756        {
757            dropDownWithAssignedIncLabels[i].setOpaque(false);
758        }
759    }
760
761    /*
762     * Sets the label to have a highlighted background.
763     *
764     * @param label the label that is selected/highlighted
765     */
766    public void selectLabel(Object label)
767    {
768        ((JLabel) label).setOpaque(true);
769    }
770
771    /*
772     * Performs the label action depending on which label was clicked.
773     */
774    public void performLabelAction(Object label)
775    {
776        if (label.equals(dropDownLabels[0]))
777        { // 10-8
778        }
779        else if (label.equals(dropDownLabels[1]))
780        { // OFC
781        }
782        else if (label.equals(dropDownLabels[2]))
783        { // OOS
784        }
785        else if (label.equals(dropDownLabels[3]))
786        { // Open Unit Details
787        }
788        else if (label.equals(dropDownLabels[4]))
789        { // Open Unit Activity Log
790        }
791        else if (label.equals(dropDownLabels[5]))
792        { // Map
793        }
794        else if (label.equals(dropDownLabels[6]))
795        { // Change Vehicle
796        }
797        else if (label.equals(dropDownLabels[7]))
798        { // Unit Poll
799        }
800        else if (label.equals(dropDownLabels[8]))
801        { // Quick Notes
802        }
803        else if (label.equals(dropDownLabels[9]))
804        { // Activate User Timer
805        }
806        else if (label.equals(dropDownLabels[10]))
807        { // Filters
808        }
809        else if (label.equals(dropDownLabels[11]))
810        { // Page
811        }
812        else if (label.equals(dropDownLabels[12]))
813        { // Roster System
814        }
815        else if (label.equals(dropDownLabels[13]))
816        { // 10-10
817        }
818    }
819
820    /*
821     * Performs the label action depending on which label was clicked.
822     */
823    public void performWithAssignedIncLabelAction(Object label)
824    {
825        if (label.equals(dropDownWithAssignedIncLabels[0]))
826        { // STAGE
827        }
828        else if (label.equals(dropDownWithAssignedIncLabels[1]))
829        { // 10-97
830        }
831        else if (label.equals(dropDownWithAssignedIncLabels[2]))
832        { // 10-8
833        }
834        else if (label.equals(dropDownWithAssignedIncLabels[3]))
835        { // ASSIGN
836            // ALT...
837        }
838        else if (label.equals(dropDownWithAssignedIncLabels[4]))
839        { // ENRT
840            // ALT...
841        }
842        else if (label.equals(dropDownWithAssignedIncLabels[5]))
843        { // 10-97
844            // ALT...
845        }
846        else if (label.equals(dropDownWithAssignedIncLabels[6]))
847        { // Open
848            // Incident
849            int idColumn = 0;
850            ScreenManager.openIncidentViewer(Integer
851                    .parseInt((String) unitStatusTable.getValueAt(
852                    unitStatusTable.getSelectedRow(), idColumn)));
853        }
854        else if (label.equals(dropDownWithAssignedIncLabels[7]))
855        { // Recall
856            // Incident
857        }
858        else if (label.equals(dropDownWithAssignedIncLabels[8]))
859        { // Open
860            // Unit
861            // Details
862        }
863        else if (label.equals(dropDownWithAssignedIncLabels[9]))
864        { // Open
865            // Unit
866            // Activity
867            // Log
868        }
869        else if (label.equals(dropDownWithAssignedIncLabels[10]))
870        { // Map
871        }
872        else if (label.equals(dropDownWithAssignedIncLabels[11]))
873        { // Recall
874            // Linked
875            // Incidents
876        }
877        else if (label.equals(dropDownWithAssignedIncLabels[12]))
878        { // Cancel
879        }
880        else if (label.equals(dropDownWithAssignedIncLabels[13]))
881        { // Reassign
882        }
883        else if (label.equals(dropDownWithAssignedIncLabels[14]))
884        { // Duplicate
885        }
886        else if (label.equals(dropDownWithAssignedIncLabels[15]))
887        { // Unit
888            // Poll
889        }
890        else if (label.equals(dropDownWithAssignedIncLabels[16]))
891        { // Quick
892            // Note...
893        }
894        else if (label.equals(dropDownWithAssignedIncLabels[17]))
895        { // Activate
896            // User
897            // Timer...
898        }
899        else if (label.equals(dropDownWithAssignedIncLabels[18]))
900        { // Filters
901        }
902        else if (label.equals(dropDownWithAssignedIncLabels[19]))
903        { // Page...
904        }
905        else if (label.equals(dropDownWithAssignedIncLabels[20]))
906        { // Fax...
907        }
908    }
909
910    /*
911     * Factory method. Adds a mouse listeners to the label. The
912     * MouseMotionListener detects the mouse's location to highlight the label.
913     * The MouseListener detects for clicks and performs the action of the label
914     * designates.
915     */
916    public void addMouseListenersToLabel(JLabel label)
917    {
918        label.addMouseMotionListener(new MouseMotionListener()
919        {
920            public void mouseDragged(MouseEvent e)
921            {
922            }
923
924            public void mouseMoved(MouseEvent e)
925            {
926                unSelectAllLabels();
927                selectLabel(e.getSource());
928                unitStatusMenu.revalidate();
929                unitStatusMenu.repaint();
930            }
931        });
932        label.addMouseListener(new MouseListener()
933        {
934            public void mouseClicked(MouseEvent e)
935            {
936                performLabelAction(e.getSource());
937                unSelectAllLabels();
938                unitStatusMenu.revalidate();
939                unitStatusMenu.repaint();
940                unitStatusMenu.setVisible(false);
941            }
942
943            public void mouseEntered(MouseEvent e)
944            {
945            }
946
947            public void mouseExited(MouseEvent e)
948            {
949            }
950
951            public void mousePressed(MouseEvent e)
952            {
953            }
954
955            public void mouseReleased(MouseEvent e)
956            {
957            }
958        });
959    }
960
961    /*
962     * Factory method. Adds a mouse listeners to the label. The
963     * MouseMotionListener detects the mouse's location to highlight the label.
964     * The MouseListener detects for clicks and performs the action of the label
965     * designates.
966     */
967    public void addMouseListenersToWithAssignedIncLabel(JLabel label)
968    {
969        label.addMouseMotionListener(new MouseMotionListener()
970        {
971            public void mouseDragged(MouseEvent e)
972            {
973            }
974
975            public void mouseMoved(MouseEvent e)
976            {
977                unSelectAllLabels();
978                selectLabel(e.getSource());
979                unitStatusWithAssignedIncMenu.revalidate();
980                unitStatusWithAssignedIncMenu.repaint();
981            }
982        });
983        label.addMouseListener(new MouseListener()
984        {
985            public void mouseClicked(MouseEvent e)
986            {
987                performWithAssignedIncLabelAction(e.getSource());
988                unSelectAllLabels();
989                unitStatusWithAssignedIncMenu.revalidate();
990                unitStatusWithAssignedIncMenu.repaint();
991                unitStatusWithAssignedIncMenu.setVisible(false);
992            }
993
994            public void mouseEntered(MouseEvent e)
995            {
996            }
997
998            public void mouseExited(MouseEvent e)
999            {
1000            }
1001
1002            public void mousePressed(MouseEvent e)
1003            {
1004            }
1005
1006            public void mouseReleased(MouseEvent e)
1007            {
1008            }
1009        });
1010    }
1011
1012    /*
1013     * Displays the menu where the right click occurred.
1014     */
1015    public void openDropDownMenu(MouseEvent e)
1016    {
1017        unitStatusMenu.setLocation(e.getX() + this.getX(),
1018                e.getY() + this.getY());
1019        unitStatusMenu.setVisible(true);
1020    }
1021
1022    /*
1023     * Displays the menu where the right click occurred.
1024     */
1025    public void openDropDownWithAssignedIncMenu(MouseEvent e)
1026    {
1027        unitStatusWithAssignedIncMenu.setLocation(e.getX() + this.getX(),
1028                e.getY() + this.getY());
1029        unitStatusWithAssignedIncMenu.setVisible(true);
1030    }
1031
1032    /*
1033     * Hides the menu.
1034     */
1035    public void closeDropDownMenu()
1036    {
1037        unSelectAllLabels();
1038        unitStatusMenu.revalidate();
1039        unitStatusMenu.repaint();
1040        unitStatusMenu.setVisible(false);
1041    }
1042
1043    /*
1044     * Hides the menu.
1045     */
1046    public void closeDropDownWithAssignedIncMenu()
1047    {
1048        unSelectAllLabels();
1049        unitStatusWithAssignedIncMenu.revalidate();
1050        unitStatusWithAssignedIncMenu.repaint();
1051        unitStatusWithAssignedIncMenu.setVisible(false);
1052    }
1053
1054    /*
1055     * Refreshes the data in the table by updating all data and repainting the
1056     * screen. It saves user preferences(like column sizes, selected row, sorted preferences)
1057     * and applies them to the updated model it receives from the server.
1058     */
1059    public void refreshTable()
1060    {
1061        if (unitStatusTable.getTableHeader().getResizingColumn() == null)
1062        {//only update info if resize not in progress
1063               
1064                // Perhaps this needs to be done in Swing's Event Dispatch Thread.
1065                // See https://stackoverflow.com/questions/1732503/java-lang-arrayindexoutofboundsexception-0-0-attempting-to-populate-jtable
1066                SwingUtilities.invokeLater(new Runnable(){public void run(){
1067                    int index = unitStatusTable.getSelectedRow();
1068                    List<? extends SortKey> keys = unitStatusTable.getRowSorter().getSortKeys();
1069                    // Save column widths prior to updating table
1070                    int[] columnWidths = new int[20];
1071                    for (int i = 0; i < unitStatusTable.getColumnCount(); i++)
1072                    {
1073                        columnWidths[i] = unitStatusTable.getColumnModel().getColumn(i).getWidth();
1074                    }
1075                    //Update the model here
1076                    try
1077                    {
1078                        // Update the table with current Coordinator data
1079                        unitStatusTable.setModel(ScreenManager.theCoordinator.getCadDataTable(CADDataEnums.TABLE.UNIT_STATUS));
1080                    } catch (RemoteException ex)
1081                    {
1082                        System.out.println("RemoteException in refreshTable method of UnitStatus.java");
1083                        System.out.println(ex.getMessage());
1084                    }
1085                    // Set preferred column widths
1086                    for (int i = 0; i < unitStatusTable.getColumnCount(); i++)
1087                    {
1088                        unitStatusTable.getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
1089                    }
1090                    unitStatusTable.getRowSorter().setSortKeys(keys);
1091                    unitStatusTable.getSelectionModel().setSelectionInterval(index, index);
1092                }});
1093            // Perhaps these aren't needed since changing the model should cause auto repaint.
1094            revalidate();
1095            repaint();
1096        }
1097    }
1098
1099    /*
1100     * Makes screen visible.
1101     */
1102    public void open()
1103    {
1104        setVisible(true);
1105    }
1106
1107    /*
1108     * Hides screen.
1109     */
1110    public void close()
1111    {
1112        setVisible(false);
1113    }
1114
1115    /**
1116     * This method is called every second in ScreenManger to update the display
1117     * time every second. Seems redundant: refreshTable already takes care of
1118     * it. JD
1119     */
1120//    public void handleUpdateTime()
1121//    {
1122//        int timerColumn = 13;
1123//        int unitColumn = 2;
1124//        for (int i = 0; i < unitStatusTable.getModel().getRowCount(); i++)
1125//        {
1126//            try
1127//            {
1128//                unitStatusTable.getModel().setValueAt(
1129//                        ScreenManager.theCoordinator.getCadDataUnitValue(
1130//                        (String) unitStatusTable.getModel().getValueAt(
1131//                        i, unitColumn), UNIT_TAGS.TIMER), i,
1132//                        timerColumn);
1133//            } catch (RemoteException e)
1134//            {
1135//                e.printStackTrace();
1136//            }
1137//        }
1138//    }
1139//
1140//    /**
1141//     * Replaces handleUpdateTime
1142//     */
1143//    public void updateTimeColumn()
1144//    {
1145//        int unitColumn = 2;
1146//        try
1147//        {
1148//            DefaultTableModel dm = ScreenManager.theCoordinator.getCadDataTable(CADDataEnums.TABLE.UNIT_STATUS);
1149//            int size = dm.getRowCount();
1150//            for (int i = 0; i < size; i++)
1151//            {
1152//                String cell = (String) dm.getValueAt(i, unitColumn);
1153//                String currTime = ScreenManager.theCoordinator.getCadDataUnitValue(cell, UNIT_TAGS.TIMER);
1154//            }
1155//        } catch (RemoteException e)
1156//        {
1157//            e.printStackTrace();
1158//        }
1159//    }
1160    /**
1161     * Removes drag and drop feature/button clicking.
1162     */
1163    public void removeDispatcherAuthority()
1164    {
1165        unitStatusTable.setTransferHandler(new TransferHandler(""));
1166        buttonEnrt.setEnabled(false);
1167        buttonStage.setEnabled(false);
1168        button1097.setEnabled(false);
1169        buttonCode4.setEnabled(false);
1170        buttonDash1.setEnabled(false);
1171        buttonDash2.setEnabled(false);
1172        buttonDash3.setEnabled(false);
1173        button108.setEnabled(false);
1174        buttonOFC.setEnabled(false);
1175        buttonOOS.setEnabled(false);
1176    }
1177}
Note: See TracBrowser for help on using the repository browser.