source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/screens/CADMenu.java @ 40

Revision 40, 20.4 KB checked in by jdalbey, 10 years ago (diff)

Remove some log messages from ParamicsFileReader? and Writer to address memory issues

Line 
1package tmcsim.client.cadclientgui.screens;
2
3import java.awt.Color;
4import java.awt.Dimension;
5import java.awt.Font;
6import java.awt.Image;
7import java.awt.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
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.io.File;
16import java.io.IOException;
17import java.net.MalformedURLException;
18import java.net.URL;
19import java.text.DateFormat;
20import java.text.SimpleDateFormat;
21import java.util.Date;
22import java.util.logging.Level;
23import java.util.logging.Logger;
24import javax.imageio.ImageIO;
25
26import javax.swing.Box;
27import javax.swing.BoxLayout;
28import javax.swing.ImageIcon;
29import javax.swing.JButton;
30import javax.swing.JFrame;
31import javax.swing.JLabel;
32import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;
33
34/**
35 * The main CADMenu with buttons to open up other screens. This class uses
36 * mostly images to build from, and java gui does not allow those images to be
37 * editable here, so all the images' pixels are predefined in the images
38 * themselves. The images can be found in the images folder. An image called
39 * "CADMenuLayout" can also be found in the same folder showing how this class's
40 * looks were made.
41 *
42 * @author Vincent
43 *
44 */
45public class CADMenu extends JFrame {
46
47    private final int ONE_SECOND = 1000;
48
49    private DateFormat dateFormat;
50
51    private Box mainPanel;
52    private Box bottomRightIcons;
53    private Box bottomRightButtonsGrayed;
54    private Box bottomRightButtonsColored;
55    private JLabel dateAndTime;
56    private JLabel name;
57    private JLabel userListName1;
58
59    private JButton button1;
60    private JButton button2;
61    private JButton button3;
62    private JButton button4;
63    private JButton button5;
64    private JButton button6;
65    private JButton buttonTool;
66    private JButton buttonMore;
67   
68    // the drop down menu when the "more button" is clicked.
69    private JFrame moreMenu;
70
71    // the drop down menu when the "tool button" is clicked.
72    private JFrame toolMenu;
73
74    private JButton buttonCheckmark;
75    private JButton buttonMinus;
76
77    private JLabel position;
78
79    /**
80     * Constructor call. Creates the CADMenu.
81     */
82    public CADMenu() {
83        initialize();
84        createTopPanel();
85        createBottomPanel();
86        createDropDownMenus();
87       
88        initControllers();
89       
90        getContentPane().add(mainPanel);
91
92        setTitle("Inform CAD");
93        URL url = null;
94        try
95        {
96            url = new URL("http://i1030.photobucket.com/albums/y362/sendeva66/antclipart2.jpg");
97        } catch (MalformedURLException ex)
98        {
99            Logger.getLogger(CADMenu.class.getName()).log(Level.SEVERE, "can't access url", ex);
100        }
101        Image image = null;
102        try
103        {
104            image = ImageIO.read(url);
105            setIconImage(image);
106        } catch (IOException ex)
107        {
108            Logger.getLogger(CADMenu.class.getName()).log(Level.SEVERE, null, ex);
109        }
110        // Doesn't work: file path problem?
111//        ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource("images/CADicon.png"));
112//        setIconImage(myIcon.getImage());
113        setPreferredSize(new Dimension(1195, 178));
114        setResizable(true);
115        setFocusable(true);
116        pack();
117        //  Unsure why this was "do nothing" because it prevents the Menu frame
118        //  from closing so the user can't terminate the application.
119        //        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
120        setDefaultCloseOperation(EXIT_ON_CLOSE);
121        setVisible(false);
122    }
123
124    /**
125     * Initializes variables and connects the CADMenu to the CADMenuListener.
126     */
127    public void initialize() {
128        mainPanel = new Box(BoxLayout.Y_AXIS);
129        mainPanel.setAlignmentX(LEFT_ALIGNMENT);
130        dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
131    }
132
133    public void addListeners() {
134        addComponentListener(new ComponentListener() {
135            public void componentHidden(ComponentEvent e) {
136            }
137
138            public void componentMoved(ComponentEvent e) {
139                closeToolMenu();
140                closeMoreMenu();
141                setLocation(getLocation());
142            }
143
144            public void componentResized(ComponentEvent e) {
145            }
146
147            public void componentShown(ComponentEvent e) {
148            }
149        });
150        addMouseListener(new MouseListener() {
151            public void mouseClicked(MouseEvent arg0) {
152                closeToolMenu();
153                closeMoreMenu();
154            }
155
156            public void mouseEntered(MouseEvent arg0) {
157            }
158
159            public void mouseExited(MouseEvent arg0) {
160            }
161
162            public void mousePressed(MouseEvent arg0) {
163            }
164
165            public void mouseReleased(MouseEvent arg0) {
166            }
167        });
168    }
169   
170    public void initToolMenuListeners(){
171        toolMenu.addMouseMotionListener(new MouseMotionListener() {
172            public void mouseDragged(MouseEvent e) {
173            }
174
175            public void mouseMoved(MouseEvent e) {
176            }
177        });
178
179        toolMenu.addMouseListener(new MouseListener() {
180            public void mouseEntered(MouseEvent e) {
181            }
182
183            public void mouseExited(MouseEvent e) {
184            }
185
186            public void mousePressed(MouseEvent e) {
187            }
188
189            public void mouseReleased(MouseEvent e) {
190            }
191
192            public void mouseClicked(MouseEvent e) {
193            }
194        });
195    }
196   
197    public void initMoreMenuListeners(){
198        moreMenu.addMouseMotionListener(new MouseMotionListener() {
199            public void mouseDragged(MouseEvent e) {
200            }
201
202            public void mouseMoved(MouseEvent e) {
203                ImageIcon image = new ImageIcon(
204                        "images/MoreMenuImages/moreMenu.png");
205                JLabel menu;
206                if (e.getLocationOnScreen().getY() >= moreMenu.getY()
207                        && e.getLocationOnScreen().getY() <= moreMenu.getY()
208                                + moreMenu.getHeight() / 8) {
209                    image = new ImageIcon(
210                            "images/MoreMenuImages/moreMenuHighlighted1.png");
211                } else if (e.getLocationOnScreen().getY() >= moreMenu.getY()
212                        + moreMenu.getHeight() * 2 / 8
213                        && e.getLocationOnScreen().getY() <= moreMenu.getY()
214                                + moreMenu.getHeight() * 3 / 8) {
215                    image = new ImageIcon(
216                            "images/MoreMenuImages/moreMenuHighlighted3.png");
217                }
218                menu = new JLabel(image);
219                moreMenu.getContentPane().removeAll();
220                moreMenu.getContentPane().add(menu);
221                moreMenu.validate();
222            }
223        });
224
225        moreMenu.addMouseListener(new MouseListener() {
226            public void mouseEntered(MouseEvent e) {
227            }
228
229            public void mouseExited(MouseEvent e) {
230            }
231
232            public void mousePressed(MouseEvent e) {
233            }
234
235            public void mouseReleased(MouseEvent e) {
236            }
237
238            public void mouseClicked(MouseEvent e) {
239                if (e.getLocationOnScreen().getY() >= moreMenu.getY()
240                        && e.getLocationOnScreen().getY() <= moreMenu.getY()
241                                + moreMenu.getHeight() / 8) {
242                    ScreenManager.openIncidentEditor();
243                    closeMoreMenu();
244                } else if (e.getLocationOnScreen().getY() >= moreMenu.getY()
245                        + moreMenu.getHeight() * 2 / 8
246                        && e.getLocationOnScreen().getY() <= moreMenu.getY()
247                                + moreMenu.getHeight() * 3 / 8) {
248                    ScreenManager.openCardfile();
249                    closeMoreMenu();
250                }
251            }
252        });
253    }
254   
255    public void initControllers(){
256        addListeners();
257
258        buttonTool.addActionListener(newToolActionListener());
259        buttonMore.addActionListener(newMoreActionListener());
260       
261        initToolMenuListeners();
262        initMoreMenuListeners();   
263    }
264
265    public ActionListener newToolActionListener(){
266        return new ActionListener() {
267            public void actionPerformed(ActionEvent arg0) {
268                if (moreMenu.isVisible()) {
269                    closeMoreMenu();
270                }
271                if (toolMenu.isVisible()) {
272                    closeToolMenu();
273                } else {
274                    toolMenu.setLocation(new Point(getX() + getWidth()
275                            - toolMenu.getWidth(), getY() + 85));
276                    toolMenu.setVisible(true);
277                }
278            }
279        };
280    }
281   
282    public ActionListener newMoreActionListener(){
283        return new ActionListener() {
284            public void actionPerformed(ActionEvent arg0) {
285                if (toolMenu.isVisible()) {
286                    closeToolMenu();
287                }
288                if (moreMenu.isVisible()) {
289                    closeMoreMenu();
290                } else {
291                    moreMenu.setLocation(new Point(getX() + getWidth()
292                            - moreMenu.getWidth(), getY() + 85));
293                    moreMenu.setVisible(true);
294                }
295            }
296        };
297    }
298   
299    /*
300     * Creates the topPanel for the CADMenu.
301     */
302    public void createTopPanel() {
303
304        Box topPanel = new Box(BoxLayout.X_AXIS);
305        topPanel.setAlignmentX(LEFT_ALIGNMENT);
306
307        ImageIcon image = new ImageIcon("images/CADMenuImages/tritech.png");
308        JLabel tritech = new JLabel(image);
309        topPanel.add(tritech);
310
311        image = new ImageIcon("images/CADMenuImages/empty.png");
312        JLabel empty = new JLabel(image);
313        topPanel.add(empty);
314
315        // center holds the User's Name, user's position, and date/time
316        Box center = new Box(BoxLayout.Y_AXIS);
317        image = new ImageIcon("images/CADMenuImages/top.png");
318
319        name = new JLabel(image);
320        name.setText("User");
321        name.setFont(new Font("SanSerif", Font.BOLD, 16));
322        name.setForeground(Color.WHITE);
323        name.setHorizontalTextPosition(JLabel.CENTER);
324        center.add(name);
325
326        Box centerBottom = new Box(BoxLayout.X_AXIS);
327        centerBottom.setAlignmentX(Box.LEFT_ALIGNMENT);
328
329        image = new ImageIcon("images/CADMenuImages/bottom.png");
330        position = new JLabel(image);
331        position.setText("Public Safety Dispatch Sup");
332        position.setFont(new Font("Arial", Font.PLAIN, 12));
333        position.setForeground(new Color(190, 210, 255));
334        position.setHorizontalTextPosition(JLabel.CENTER);
335        centerBottom.add(position);
336
337        image = new ImageIcon("images/CADMenuImages/bottom.png");
338        Date date = new Date();
339        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
340        String dateTime = dateFormat.format(date);
341        dateAndTime = new JLabel(image);
342        dateAndTime.setText(dateTime);
343        dateAndTime.setForeground(Color.WHITE);
344        dateAndTime.setHorizontalTextPosition(JLabel.CENTER);
345        centerBottom.add(dateAndTime);
346        center.add(centerBottom);
347
348        topPanel.add(center);
349
350        image = new ImageIcon("images/CADMenuImages/empty2.png");
351        JLabel empty2 = new JLabel(image);
352        topPanel.add(empty2);
353
354        image = new ImageIcon("images/CADMenuImages/button1.png");
355        button1 = makeButton(image);
356        topPanel.add(button1);
357
358        image = new ImageIcon("images/CADMenuImages/button2.png");
359        button2 = makeButton(image);
360        topPanel.add(button2);
361
362        image = new ImageIcon("images/CADMenuImages/button3.png");
363        button3 = makeButton(image);
364        topPanel.add(button3);
365
366        image = new ImageIcon("images/CADMenuImages/button4.png");
367        button4 = makeButton(image);
368        topPanel.add(button4);
369
370        image = new ImageIcon("images/CADMenuImages/button5.png");
371        button5 = makeButton(image);
372        topPanel.add(button5);
373
374        image = new ImageIcon("images/CADMenuImages/button6.png");
375        button6 = makeButton(image);
376        topPanel.add(button6);
377
378        image = new ImageIcon("images/CADMenuImages/button7.png");
379        buttonTool = makeButton(image);
380        topPanel.add(buttonTool);
381
382        image = new ImageIcon("images/CADMenuImages/button8.png");
383        buttonMore = makeButton(image);
384        topPanel.add(buttonMore);
385
386        mainPanel.add(topPanel);
387    }
388
389    /**
390     * Creates the bottomPanel of the CADMenu.
391     */
392    public void createBottomPanel() {
393        Color grayBackground = new Color(100, 100, 100);
394
395        Box userList = new Box(BoxLayout.Y_AXIS);
396        userList.setAlignmentX(LEFT_ALIGNMENT);
397
398        Dimension size = new Dimension(400, 85);
399        userList.setPreferredSize(size);
400        userList.setMaximumSize(size);
401        userList.setMinimumSize(size);
402        userList.setBackground(grayBackground);
403        userList.setOpaque(true);
404
405        Box user1 = new Box(BoxLayout.X_AXIS);
406        user1.setAlignmentX(LEFT_ALIGNMENT);
407
408        ImageIcon image = new ImageIcon("images/CADMenuImages/mailClose.png");
409        JLabel userListIcon1 = new JLabel(image);
410        userListName1 = new JLabel("User");
411        userListName1.setForeground(Color.WHITE);
412        userListName1.setBackground(grayBackground);
413        userListName1.setOpaque(true);
414        JLabel userListNumbers = new JLabel(
415                "<html><font color=white>(</font><font color=red>0</font><font color=white>,0,0)</font></html>");
416        userListNumbers.setBackground(grayBackground);
417        userListNumbers.setOpaque(true);
418
419        user1.add(userListIcon1);
420        user1.add(userListName1);
421        user1.add(userListNumbers);
422        userList.add(user1);
423
424        Box user2 = new Box(BoxLayout.X_AXIS);
425        user2.setAlignmentX(LEFT_ALIGNMENT);
426
427        image = new ImageIcon("images/CADMenuImages/mailClose.png");
428        JLabel userListIcon2 = new JLabel(image);
429        JLabel userListName2 = new JLabel("  SL007  ");
430        userListName2.setForeground(Color.WHITE);
431        userListName2.setBackground(grayBackground);
432        userListName2.setOpaque(true);
433        userListNumbers = new JLabel(
434                "<html><font color=white>(</font><font color=red>0</font><font color=white>,0,0)</font></html>");
435        userListNumbers.setBackground(grayBackground);
436        userListNumbers.setOpaque(true);
437
438        user2.add(userListIcon2);
439        user2.add(userListName2);
440        user2.add(userListNumbers);
441        userList.add(user2);
442
443        JLabel currentAgency = new JLabel("  Current Agency is CHP");
444        currentAgency.setForeground(Color.WHITE);
445        currentAgency.setBackground(grayBackground);
446        currentAgency.setOpaque(true);
447        currentAgency.setHorizontalTextPosition(JLabel.LEFT);
448        userList.add(currentAgency);
449
450        Box bottomPanel = new Box(BoxLayout.X_AXIS);
451        bottomPanel.setAlignmentX(LEFT_ALIGNMENT);
452        bottomPanel.add(userList);
453        bottomPanel.add(Box.createHorizontalStrut(5));
454
455        Box bottomRight = new Box(BoxLayout.Y_AXIS);
456        size = new Dimension(775, 85);
457        bottomRight.setPreferredSize(size);
458        bottomRight.setMaximumSize(size);
459        bottomRight.setMinimumSize(size);
460        image = new ImageIcon("images/CADMenuImages/grayFillerTop.png");
461        bottomRight.add(new JLabel(image));
462
463        bottomRightIcons = new Box(BoxLayout.X_AXIS);
464        bottomRightIcons.setAlignmentX(LEFT_ALIGNMENT);
465        image = new ImageIcon("images/CADMenuImages/grayFillerBottom.png");
466        bottomRightIcons.add(new JLabel(image));
467
468        createBottomRightButtons();
469        bottomRightIcons.add(bottomRightButtonsGrayed);
470        bottomRight.add(bottomRightIcons);
471        bottomPanel.add(bottomRight);
472
473        mainPanel.add(bottomPanel);
474    }
475
476    /**
477     * Creates the two bottom right buttons.
478     */
479    public void createBottomRightButtons() {
480        bottomRightButtonsGrayed = new Box(BoxLayout.X_AXIS);
481        ImageIcon image = new ImageIcon(
482                "images/CADMenuImages/checkmarkGrey.png");
483        buttonCheckmark = makeButton(image);
484        bottomRightButtonsGrayed.add(buttonCheckmark);
485        image = new ImageIcon("images/CADMenuImages/minusGrey.png");
486
487        buttonMinus = makeButton(image);
488        bottomRightButtonsGrayed.add(buttonMinus);
489
490        bottomRightButtonsColored = new Box(BoxLayout.X_AXIS);
491        image = new ImageIcon("images/CADMenuImages/checkmarkGreen.png");
492        buttonCheckmark = makeButton(image);
493        bottomRightButtonsColored.add(buttonCheckmark);
494        image = new ImageIcon("images/CADMenuImages/minusRed.png");
495
496        buttonMinus = makeButton(image);
497        bottomRightButtonsColored.add(buttonMinus);
498    }
499
500    /**
501     * This method creates the drop down menus for both the tool and more
502     * buttons
503     */
504    public void createDropDownMenus() {
505
506        ImageIcon image = new ImageIcon("images/ToolMenuImages/toolMenu.png");
507        JLabel menu = new JLabel(image);
508        toolMenu = new JFrame();
509        toolMenu.getContentPane().add(menu);
510        toolMenu.setUndecorated(true);
511        toolMenu.pack();
512        toolMenu.setVisible(false);
513
514        image = new ImageIcon("images/MoreMenuImages/moreMenu.png");
515        menu = new JLabel(image);
516        moreMenu = new JFrame();
517        moreMenu.getContentPane().add(menu);
518        moreMenu.setUndecorated(true);
519        moreMenu.pack();
520        moreMenu.setVisible(false);
521    }
522
523    /**
524     * Takes in the name of the user and displays it.
525     */
526    public void setName(String username) {
527        name.setText(username);
528        userListName1.setText(username);
529    }
530
531    /**
532     * Factory method. Makes a JButton with an image and listener.
533     *
534     * @param image
535     *            the image this button will display.
536     * @param listener
537     *            the action listener for this button.
538     * @return the JButton.
539     */
540    public JButton makeButton(ImageIcon image) {
541        JButton button = new JButton(image);
542        Dimension size = new Dimension(image.getImage().getWidth(null), image
543                .getImage().getHeight(null));
544        button.setPreferredSize(size);
545        button.setMinimumSize(size);
546        button.setMaximumSize(size);
547        button.setBorderPainted(false);
548        button.setFocusable(false);
549
550        return button;
551    }
552
553    /**
554     * Hides the tool drop down menu. Also resets the image to the default(no
555     * highlighted rows).
556     */
557    public void closeToolMenu() {
558        ImageIcon image;
559        JLabel menu;
560        image = new ImageIcon("images/ToolMenuImages/toolMenu.png");
561        menu = new JLabel(image);
562        toolMenu.getContentPane().removeAll();
563        toolMenu.getContentPane().add(menu);
564        toolMenu.validate();
565        toolMenu.setVisible(false);
566    }
567
568    /**
569     * Hides the more drop down menu. Also resets the image to the default(no
570     * highlighted rows).
571     */
572    public void closeMoreMenu() {
573        ImageIcon image;
574        JLabel menu;
575        image = new ImageIcon("images/MoreMenuImages/moreMenu.png");
576        menu = new JLabel(image);
577        moreMenu.getContentPane().removeAll();
578        moreMenu.getContentPane().add(menu);
579        moreMenu.validate();
580        moreMenu.setVisible(false);
581    }
582
583    /**
584     * This method is called every second in ScreenManger to update the display
585     * time every second.
586     */
587    public void handleUpdateTime() {
588        Date date = new Date();
589        String dateTime = dateFormat.format(date);
590        dateAndTime.setText(dateTime);
591    }
592
593    /**
594     * Makes screen visible.
595     */
596    public void open() {
597        setVisible(true);
598    }
599
600    /**
601     * Hides screen.
602     */
603    public void close() {
604        setVisible(false);
605    }
606
607    /**
608     * Currently not used.
609     */
610    public void setIncomingIncident() {
611        bottomRightIcons.remove(bottomRightButtonsGrayed);
612        bottomRightIcons.add(bottomRightButtonsColored);
613        revalidate();
614        repaint();
615    }
616
617    /**
618     * Currently not used.
619     */
620    public void endIncomingIncident() {
621        bottomRightIcons.remove(bottomRightButtonsColored);
622        bottomRightIcons.add(bottomRightButtonsGrayed);
623        revalidate();
624        repaint();
625    }
626
627    /**
628     * Sets position title to trainee.
629     */
630    public void removeDispatcherStatus() {
631        position.setText("Trainee");
632    }
633
634}
Note: See TracBrowser for help on using the repository browser.