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
26
27
28
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
39
40 public MenuBar() {
41 menuBar.add(createFileMenu());
42 menuBar.add(createEditMenu());
43 menuBar.add(createHelpMenu());
44 }
45
46
47
48
49
50
51 public JMenuBar get() {
52 return menuBar;
53 }
54
55
56
57
58
59
60
61
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
82
83
84
85
86
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
112
113
114
115
116
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
128 JLabel label = new JLabel();
129 Font font = label.getFont();
130
131
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
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
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
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
171
172
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 }