View Javadoc
1   package com.nilhcem.fakesmtp.gui;
2   
3   import com.nilhcem.fakesmtp.core.ArgsHandler;
4   import com.nilhcem.fakesmtp.core.I18n;
5   import com.nilhcem.fakesmtp.gui.listeners.AboutActionListener;
6   import com.nilhcem.fakesmtp.gui.listeners.ExitActionListener;
7   
8   import javax.swing.JMenu;
9   import javax.swing.JMenuBar;
10  import javax.swing.JMenuItem;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  import java.util.Observable;
14  
15  /**
16   * Provides the menu bar of the application.
17   *
18   * @author Nilhcem
19   * @since 1.0
20   */
21  public final class MenuBar extends Observable {
22  
23  	private final I18n i18n = I18n.INSTANCE;
24  	private final JMenuBar menuBar = new JMenuBar();
25  	private final MainFrame mainFrame;
26  
27  	/**
28  	 * Creates the menu bar and the different menus (file / edit / help).
29  	 *
30  	 * @param mainFrame MainFrame class required for the closing action.
31  	 */
32  	public MenuBar(MainFrame mainFrame) {
33  		this.mainFrame = mainFrame;
34  
35  		menuBar.add(createFileMenu());
36  		menuBar.add(createEditMenu());
37  		menuBar.add(createHelpMenu());
38  	}
39  
40  	/**
41  	 * Returns the JMenuBar object.
42  	 *
43  	 * @return the JMenuBar object.
44  	 */
45  	public JMenuBar get() {
46  		return menuBar;
47  	}
48  
49  	/**
50  	 * Creates the file menu.
51  	 * <p>
52  	 * The file menu contains an "Exit" item, to quit the application.
53  	 * </p>
54  	 *
55  	 * @return the newly created file menu.
56  	 */
57  	private JMenu createFileMenu() {
58  		JMenu fileMenu = new JMenu(i18n.get("menubar.file"));
59  		fileMenu.setMnemonic(i18n.get("menubar.mnemo.file").charAt(0));
60  
61  		JMenuItem exit = new JMenuItem(i18n.get("menubar.exit"));
62  		exit.setMnemonic(i18n.get("menubar.mnemo.exit").charAt(0));
63  		exit.addActionListener(new ExitActionListener(mainFrame));
64  
65  		fileMenu.add(exit);
66  		return fileMenu;
67  	}
68  
69  	/**
70  	 * Creates the edit menu.
71  	 * <p>
72  	 * The edit menu contains a "Messages location" item, to define the location of the incoming mails.
73  	 * </p>
74  	 *
75  	 * @return the newly created edit menu.
76  	 */
77  	private JMenu createEditMenu() {
78  		JMenu editMenu = new JMenu(i18n.get("menubar.edit"));
79  		editMenu.setMnemonic(i18n.get("menubar.mnemo.edit").charAt(0));
80  
81  		JMenuItem mailsLocation = new JMenuItem(i18n.get("menubar.messages.location"));
82  		mailsLocation.setMnemonic(i18n.get("menubar.mnemo.msglocation").charAt(0));
83  		if (ArgsHandler.INSTANCE.memoryModeEnabled()) {
84  			mailsLocation.setEnabled(false);
85  		} else {
86  			mailsLocation.addActionListener(new ActionListener() {
87  				@Override
88  				public void actionPerformed(ActionEvent e) {
89  					setChanged();
90  					notifyObservers();
91  				}
92  			});
93  		}
94  
95  		editMenu.add(mailsLocation);
96  		return editMenu;
97  	}
98  
99  	/**
100 	 * Creates the help menu.
101 	 * <p>
102 	 * The help menu contains an "About" item, to display some software information.
103 	 * </p>
104 	 *
105 	 * @return the newly created help menu.
106 	 */
107 	private JMenu createHelpMenu() {
108 		JMenu helpMenu = new JMenu(i18n.get("menubar.help"));
109 		helpMenu.setMnemonic(i18n.get("menubar.mnemo.help").charAt(0));
110 
111 		JMenuItem about = new JMenuItem(i18n.get("menubar.about"));
112 		about.setMnemonic(i18n.get("menubar.mnemo.about").charAt(0));
113 		about.addActionListener(new AboutActionListener(menuBar.getParent()));
114 
115 		helpMenu.add(about);
116 		return helpMenu;
117 	}
118 }