create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / URLLabel.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Color;
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;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import net.sf.openrocket.logging.LogHelper;
16 import net.sf.openrocket.startup.Application;
17 import net.sf.openrocket.util.BugException;
18
19 /**
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.
22  * 
23  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24  */
25 public class URLLabel extends SelectableLabel {
26         private static final LogHelper log = Application.getLogger();
27         
28         /**
29          * Create a label showing the url it will direct to.
30          * 
31          * @param url   the URL.
32          */
33         public URLLabel(String url) {
34                 this(url, url);
35         }
36         
37         /**
38          * Create a label with separate URL and label.
39          * 
40          * @param url   the URL clicking will open.
41          * @param label the label.
42          */
43         public URLLabel(final String url, String label) {
44                 super();
45                 
46                 setText(label);
47
48                 if (Desktop.isDesktopSupported()) {
49                         
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);
55                         
56                         this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
57                         
58                         
59                         this.addMouseListener(new MouseAdapter() {
60                                 @Override
61                                 public void mouseClicked(MouseEvent e) {
62                                         Desktop d = Desktop.getDesktop();
63                                         try {
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);
69                                         }
70                                 }
71                         });
72
73                 }
74         }
75 }