1 package net.sf.openrocket.gui.components;
4 import java.awt.Cursor;
5 import java.awt.Desktop;
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.awt.font.TextAttribute;
9 import java.io.IOException;
11 import java.net.URISyntaxException;
12 import java.util.HashMap;
15 import net.sf.openrocket.logging.LogHelper;
16 import net.sf.openrocket.startup.Application;
17 import net.sf.openrocket.util.BugException;
20 * A label of a URL that is clickable. Clicking the URL will launch the URL in
21 * the default browser if the Desktop class is supported.
23 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
25 public class URLLabel extends SelectableLabel {
26 private static final LogHelper log = Application.getLogger();
29 * Create a label showing the url it will direct to.
33 public URLLabel(String url) {
38 * Create a label with separate URL and label.
40 * @param url the URL clicking will open.
41 * @param label the label.
43 public URLLabel(final String url, String label) {
48 if (Desktop.isDesktopSupported()) {
50 // Blue, underlined font
51 Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
52 map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
53 this.setFont(this.getFont().deriveFont(map));
54 this.setForeground(Color.BLUE);
56 this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
59 this.addMouseListener(new MouseAdapter() {
61 public void mouseClicked(MouseEvent e) {
62 Desktop d = Desktop.getDesktop();
64 d.browse(new URI(url));
65 } catch (URISyntaxException e1) {
66 throw new BugException("Illegal URL: " + url, e1);
67 } catch (IOException e1) {
68 log.error("Unable to launch browser: " + e1.getMessage(), e1);