View Javadoc
1   package com.nilhcem.fakesmtp.gui;
2   
3   import com.nilhcem.fakesmtp.core.ArgsHandler;
4   import com.nilhcem.fakesmtp.core.Configuration;
5   import com.nilhcem.fakesmtp.core.I18n;
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   
9   import javax.swing.JEditorPane;
10  import javax.swing.JLabel;
11  import javax.swing.JMenu;
12  import javax.swing.JMenuBar;
13  import javax.swing.JMenuItem;
14  import javax.swing.JOptionPane;
15  import javax.swing.event.HyperlinkEvent;
16  import javax.swing.event.HyperlinkListener;
17  import java.awt.Desktop;
18  import java.awt.Font;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.net.URI;
22  import java.util.Observable;
23  
24  /**
25   * Provides the menu bar of the application.
26   *
27   * @author Nilhcem
28   * @since 1.0
29   */
30  public final class MenuBar extends Observable {
31  
32  	private final I18n i18n = I18n.INSTANCE;
33  	private final JMenuBar menuBar = new JMenuBar();
34  
35  	private static final Logger LOGGER = LoggerFactory.getLogger(MenuBar.class);
36  
37  	/**
38  	 * Creates the menu bar and the different menus (file / edit / help).
39  	 */
40  	public MenuBar() {
41  		menuBar.add(createFileMenu());
42  		menuBar.add(createEditMenu());
43  		menuBar.add(createHelpMenu());
44  	}
45  
46  	/**
47  	 * Returns the JMenuBar object.
48  	 *
49  	 * @return the JMenuBar object.
50  	 */
51  	public JMenuBar get() {
52  		return menuBar;
53  	}
54  
55  	/**
56  	 * Creates the file menu.
57  	 * <p>
58  	 * The file menu contains an "Exit" item, to quit the application.
59  	 * </p>
60  	 *
61  	 * @return the newly created file menu.
62  	 */
63  	private JMenu createFileMenu() {
64  		JMenu fileMenu = new JMenu(i18n.get("menubar.file"));
65  		fileMenu.setMnemonic(i18n.get("menubar.mnemo.file").charAt(0));
66  
67  		JMenuItem exit = new JMenuItem(i18n.get("menubar.exit"));
68  		exit.setMnemonic(i18n.get("menubar.mnemo.exit").charAt(0));
69  		exit.addActionListener(new ActionListener() {
70  			@Override
71  			public void actionPerformed(ActionEvent e) {
72  				System.exit(0);
73  			}
74  		});
75  
76  		fileMenu.add(exit);
77  		return fileMenu;
78  	}
79  
80  	/**
81  	 * Creates the edit menu.
82  	 * <p>
83  	 * The edit menu contains a "Messages location" item, to define the location of the incoming mails.
84  	 * </p>
85  	 *
86  	 * @return the newly created edit menu.
87  	 */
88  	private JMenu createEditMenu() {
89  		JMenu editMenu = new JMenu(i18n.get("menubar.edit"));
90  		editMenu.setMnemonic(i18n.get("menubar.mnemo.edit").charAt(0));
91  
92  		JMenuItem mailsLocation = new JMenuItem(i18n.get("menubar.messages.location"));
93  		mailsLocation.setMnemonic(i18n.get("menubar.mnemo.msglocation").charAt(0));
94  		if (ArgsHandler.INSTANCE.memoryModeEnabled()) {
95  			mailsLocation.setEnabled(false);
96  		} else {
97  			mailsLocation.addActionListener(new ActionListener() {
98  				@Override
99  				public void actionPerformed(ActionEvent e) {
100 					setChanged();
101 					notifyObservers();
102 				}
103 			});
104 		}
105 
106 		editMenu.add(mailsLocation);
107 		return editMenu;
108 	}
109 
110 	/**
111 	 * Creates the help menu.
112 	 * <p>
113 	 * The help menu contains an "About" item, to display some software information.
114 	 * </p>
115 	 *
116 	 * @return the newly created help menu.
117 	 */
118 	private JMenu createHelpMenu() {
119 		JMenu helpMenu = new JMenu(i18n.get("menubar.help"));
120 		helpMenu.setMnemonic(i18n.get("menubar.mnemo.help").charAt(0));
121 
122 		JMenuItem about = new JMenuItem(i18n.get("menubar.about"));
123 		about.setMnemonic(i18n.get("menubar.mnemo.about").charAt(0));
124 		about.addActionListener(new ActionListener() {
125 			@Override
126 			public void actionPerformed(ActionEvent e) {
127 				// for copying style
128 				JLabel label = new JLabel();
129 				Font font = label.getFont();
130 
131 				// create some css from the label's font
132 				StringBuffer style = new StringBuffer("font-family:")
133 					.append(font.getFamily()).append(";font-weight:");
134 				if (font.isBold()) {
135 					style.append("bold");
136 				} else {
137 					style.append("normal");
138 				}
139 				style.append(";font-size:").append(font.getSize()).append("pt;");
140 
141 				// html content
142 				String link = i18n.get("menubar.about.dialog.link");
143 				JEditorPane ep = new JEditorPane("text/html",
144 						String.format("<html><body style=\"%s\">%s<br /><a href=\"%s\">%s</a></body></html>",
145 								style, i18n.get("menubar.about.dialog"), link, link));
146 
147 				// handle link events
148 				ep.addHyperlinkListener(new HyperlinkListener() {
149 					@Override
150 					public void hyperlinkUpdate(HyperlinkEvent e) {
151 						if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
152 							MenuBar.launchUrl(e.getURL().toString());
153 						}
154 					}
155 				});
156 				ep.setEditable(false);
157 				ep.setBackground(label.getBackground());
158 
159 				// show
160 				JOptionPane.showMessageDialog(menuBar.getParent(), ep, String.format(i18n.get("menubar.about.title"),
161 						Configuration.INSTANCE.get("application.name")), JOptionPane.INFORMATION_MESSAGE);
162 			}
163 		});
164 
165 		helpMenu.add(about);
166 		return helpMenu;
167 	}
168 
169 	/**
170 	 * Opens a web browser to launch the url specified in parameters.
171 	 *
172 	 * @param url the URL to launch.
173 	 */
174 	private static void launchUrl(String url) {
175 		if (Desktop.isDesktopSupported()) {
176 			try {
177 				Desktop desktop = Desktop.getDesktop();
178 				if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
179 					desktop.browse(new URI(url));
180 				}
181 			} catch (Exception e) {
182 				LOGGER.error("", e);
183 			}
184 		}
185 	}
186 }