Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/client/cadclientgui/screens/AssignedIncidents.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 64, 21.6 KB checked in by jdalbey, 9 years ago (diff)

Renamed to ClockClient?, added build target for it.

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