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

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