source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/PendingIncidents.java @ 445

Revision 445, 18.5 KB checked in by jdalbey, 7 years ago (diff)

Changed restart dialog in CADClientView to a non-modal one to fix #159. Shortened exception log messages in other client modules.

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