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

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

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

Merge CAD Client updates for multiple incident view windows.

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