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

Revision 3, 18.3 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files - cadclientgui

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