Coverage Report - com.nilhcem.fakesmtp.gui.MenuBar
 
Classes in this File Line Coverage Branch Coverage Complexity
MenuBar
0%
0/41
0%
0/8
1.7
MenuBar$1
0%
0/3
N/A
1.7
MenuBar$2
0%
0/4
N/A
1.7
MenuBar$3
0%
0/19
0%
0/2
1.7
MenuBar$3$1
0%
0/4
0%
0/2
1.7
 
 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  0
 public final class MenuBar extends Observable {
 31  
 
 32  0
         private final I18n i18n = I18n.INSTANCE;
 33  0
         private final JMenuBar menuBar = new JMenuBar();
 34  
 
 35  0
         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  0
         public MenuBar() {
 41  0
                 menuBar.add(createFileMenu());
 42  0
                 menuBar.add(createEditMenu());
 43  0
                 menuBar.add(createHelpMenu());
 44  0
         }
 45  
 
 46  
         /**
 47  
          * Returns the JMenuBar object.
 48  
          *
 49  
          * @return the JMenuBar object.
 50  
          */
 51  
         public JMenuBar get() {
 52  0
                 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  0
                 JMenu fileMenu = new JMenu(i18n.get("menubar.file"));
 65  0
                 fileMenu.setMnemonic(i18n.get("menubar.mnemo.file").charAt(0));
 66  
 
 67  0
                 JMenuItem exit = new JMenuItem(i18n.get("menubar.exit"));
 68  0
                 exit.setMnemonic(i18n.get("menubar.mnemo.exit").charAt(0));
 69  0
                 exit.addActionListener(new ActionListener() {
 70  
                         @Override
 71  
                         public void actionPerformed(ActionEvent e) {
 72  0
                                 System.exit(0);
 73  0
                         }
 74  
                 });
 75  
 
 76  0
                 fileMenu.add(exit);
 77  0
                 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  0
                 JMenu editMenu = new JMenu(i18n.get("menubar.edit"));
 90  0
                 editMenu.setMnemonic(i18n.get("menubar.mnemo.edit").charAt(0));
 91  
 
 92  0
                 JMenuItem mailsLocation = new JMenuItem(i18n.get("menubar.messages.location"));
 93  0
                 mailsLocation.setMnemonic(i18n.get("menubar.mnemo.msglocation").charAt(0));
 94  0
                 if (ArgsHandler.INSTANCE.memoryModeEnabled()) {
 95  0
                         mailsLocation.setEnabled(false);
 96  
                 } else {
 97  0
                         mailsLocation.addActionListener(new ActionListener() {
 98  
                                 @Override
 99  
                                 public void actionPerformed(ActionEvent e) {
 100  0
                                         setChanged();
 101  0
                                         notifyObservers();
 102  0
                                 }
 103  
                         });
 104  
                 }
 105  
 
 106  0
                 editMenu.add(mailsLocation);
 107  0
                 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  0
                 JMenu helpMenu = new JMenu(i18n.get("menubar.help"));
 120  0
                 helpMenu.setMnemonic(i18n.get("menubar.mnemo.help").charAt(0));
 121  
 
 122  0
                 JMenuItem about = new JMenuItem(i18n.get("menubar.about"));
 123  0
                 about.setMnemonic(i18n.get("menubar.mnemo.about").charAt(0));
 124  0
                 about.addActionListener(new ActionListener() {
 125  
                         @Override
 126  
                         public void actionPerformed(ActionEvent e) {
 127  
                                 // for copying style
 128  0
                                 JLabel label = new JLabel();
 129  0
                                 Font font = label.getFont();
 130  
 
 131  
                                 // create some css from the label's font
 132  0
                                 StringBuffer style = new StringBuffer("font-family:")
 133  0
                                         .append(font.getFamily()).append(";font-weight:");
 134  0
                                 if (font.isBold()) {
 135  0
                                         style.append("bold");
 136  
                                 } else {
 137  0
                                         style.append("normal");
 138  
                                 }
 139  0
                                 style.append(";font-size:").append(font.getSize()).append("pt;");
 140  
 
 141  
                                 // html content
 142  0
                                 String link = i18n.get("menubar.about.dialog.link");
 143  0
                                 JEditorPane ep = new JEditorPane("text/html",
 144  0
                                                 String.format("<html><body style=\"%s\">%s<br /><a href=\"%s\">%s</a></body></html>",
 145  0
                                                                 style, i18n.get("menubar.about.dialog"), link, link));
 146  
 
 147  
                                 // handle link events
 148  0
                                 ep.addHyperlinkListener(new HyperlinkListener() {
 149  
                                         @Override
 150  
                                         public void hyperlinkUpdate(HyperlinkEvent e) {
 151  0
                                                 if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
 152  0
                                                         MenuBar.launchUrl(e.getURL().toString());
 153  
                                                 }
 154  0
                                         }
 155  
                                 });
 156  0
                                 ep.setEditable(false);
 157  0
                                 ep.setBackground(label.getBackground());
 158  
 
 159  
                                 // show
 160  0
                                 JOptionPane.showMessageDialog(menuBar.getParent(), ep, String.format(i18n.get("menubar.about.title"),
 161  0
                                                 Configuration.INSTANCE.get("application.name")), JOptionPane.INFORMATION_MESSAGE);
 162  0
                         }
 163  
                 });
 164  
 
 165  0
                 helpMenu.add(about);
 166  0
                 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  0
                 if (Desktop.isDesktopSupported()) {
 176  
                         try {
 177  0
                                 Desktop desktop = Desktop.getDesktop();
 178  0
                                 if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
 179  0
                                         desktop.browse(new URI(url));
 180  
                                 }
 181  0
                         } catch (Exception e) {
 182  0
                                 LOGGER.error("", e);
 183  0
                         }
 184  
                 }
 185  0
         }
 186  
 }