View Javadoc
1   package com.nilhcem.fakesmtp.gui.listeners;
2   
3   import com.nilhcem.fakesmtp.core.Configuration;
4   import com.nilhcem.fakesmtp.core.I18n;
5   import java.awt.Container;
6   import java.awt.Desktop;
7   import java.awt.Font;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.net.URI;
11  import javax.swing.JEditorPane;
12  import javax.swing.JLabel;
13  import javax.swing.JOptionPane;
14  import javax.swing.event.HyperlinkEvent;
15  import javax.swing.event.HyperlinkListener;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  /**
20   * Implements the About action.
21   *
22   * @author Vest
23   * @since 2.1
24   */
25  public class AboutActionListener implements ActionListener {
26  
27  	private final I18n i18n = I18n.INSTANCE;
28  	private final Container parent;
29  
30  	private static final Logger LOGGER = LoggerFactory.getLogger(AboutActionListener.class);
31  
32  	/**
33  	 * @param parent The parent container that is used for the About dialog window.
34  	 */
35  	public AboutActionListener(Container parent) {
36  		this.parent = parent;
37  	}
38  
39  	@Override
40  	public void actionPerformed(ActionEvent e) {
41  		// for copying style
42  		JLabel label = new JLabel();
43  		Font font = label.getFont();
44  
45  		// create some css from the label's font
46  		StringBuffer style = new StringBuffer("font-family:").append(font.getFamily()).append(";font-weight:");
47  		if (font.isBold()) {
48  			style.append("bold");
49  		} else {
50  			style.append("normal");
51  		}
52  		style.append(";font-size:").append(font.getSize()).append("pt;");
53  
54  		// html content
55  		String link = i18n.get("menubar.about.dialog.link");
56  		JEditorPane ep = new JEditorPane("text/html",
57  			String.format("<html><body style=\"%s\">%s<br /><a href=\"%s\">%s</a></body></html>",
58  				style, i18n.get("menubar.about.dialog"), link, link));
59  
60  		// handle link events
61  		ep.addHyperlinkListener(new HyperlinkListener() {
62  			@Override
63  			public void hyperlinkUpdate(HyperlinkEvent e) {
64  				if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
65  					AboutActionListener.launchUrl(e.getURL().toString());
66  				}
67  			}
68  		});
69  		ep.setEditable(false);
70  		ep.setBackground(label.getBackground());
71  
72  		// show
73  		JOptionPane.showMessageDialog(parent, ep, String.format(i18n.get("menubar.about.title"),
74  			Configuration.INSTANCE.get("application.name")), JOptionPane.INFORMATION_MESSAGE);
75  	}
76  
77  	/**
78  	 * Opens a web browser to launch the URL specified in parameters.
79  	 *
80  	 * @param url the URL to launch.
81  	 */
82  	private static void launchUrl(String url) {
83  		if (Desktop.isDesktopSupported()) {
84  			try {
85  				Desktop desktop = Desktop.getDesktop();
86  				if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
87  					desktop.browse(new URI(url));
88  				}
89  			} catch (Exception e) {
90  				LOGGER.error("", e);
91  			}
92  		}
93  	}
94  }