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.util.BugException;
18 * A label of a URL that is clickable. Clicking the URL will launch the URL in
19 * the default browser if the Desktop class is supported.
21 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23 public class URLLabel extends SelectableLabel {
26 * Create a label showing the url it will direct to.
30 public URLLabel(String url) {
35 * Create a label with separate URL and label.
37 * @param url the URL clicking will open.
38 * @param label the label.
40 public URLLabel(final String url, String label) {
45 if (Desktop.isDesktopSupported()) {
47 // Blue, underlined font
48 Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
49 map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
50 this.setFont(this.getFont().deriveFont(map));
51 this.setForeground(Color.BLUE);
53 this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
56 this.addMouseListener(new MouseAdapter() {
58 public void mouseClicked(MouseEvent e) {
59 Desktop d = Desktop.getDesktop();
61 d.browse(new URI(url));
62 } catch (URISyntaxException e1) {
63 throw new BugException("BUG: Illegal URL: " + url, e1);
64 } catch (IOException e1) {
65 System.err.println("Unable to launch browser:");