Updates for 0.9.5
[debian/openrocket] / 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.util.BugException;
16
17 /**
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.
20  * 
21  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22  */
23 public class URLLabel extends SelectableLabel {
24         
25         /**
26          * Create a label showing the url it will direct to.
27          * 
28          * @param url   the URL.
29          */
30         public URLLabel(String url) {
31                 this(url, url);
32         }
33         
34         /**
35          * Create a label with separate URL and label.
36          * 
37          * @param url   the URL clicking will open.
38          * @param label the label.
39          */
40         public URLLabel(final String url, String label) {
41                 super();
42                 
43                 setText(label);
44
45                 if (Desktop.isDesktopSupported()) {
46                         
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);
52                         
53                         this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
54                         
55                         
56                         this.addMouseListener(new MouseAdapter() {
57                                 @Override
58                                 public void mouseClicked(MouseEvent e) {
59                                         Desktop d = Desktop.getDesktop();
60                                         try {
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:");
66                                                 e1.printStackTrace();
67                                         }
68                                 }
69                         });
70
71                 }
72         }
73 }