updates for 0.9.3
[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 /**
16  * A label of a URL that is clickable.  Clicking the URL will launch the URL in
17  * the default browser if the Desktop class is supported.
18  * 
19  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20  */
21 public class URLLabel extends SelectableLabel {
22         
23         /**
24          * Create a label showing the url it will direct to.
25          * 
26          * @param url   the URL.
27          */
28         public URLLabel(String url) {
29                 this(url, url);
30         }
31         
32         /**
33          * Create a label with separate URL and label.
34          * 
35          * @param url   the URL clicking will open.
36          * @param label the label.
37          */
38         public URLLabel(final String url, String label) {
39                 super();
40                 
41                 setText(label);
42
43                 if (Desktop.isDesktopSupported()) {
44                         
45                         // Blue, underlined font
46                         Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
47                         map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
48                         this.setFont(this.getFont().deriveFont(map));
49                         this.setForeground(Color.BLUE);
50                         
51                         this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
52                         
53                         
54                         this.addMouseListener(new MouseAdapter() {
55                                 @Override
56                                 public void mouseClicked(MouseEvent e) {
57                                         Desktop d = Desktop.getDesktop();
58                                         try {
59                                                 d.browse(new URI(url));
60                                         } catch (URISyntaxException e1) {
61                                                 throw new RuntimeException("BUG: Illegal URL: " + url, e1);
62                                         } catch (IOException e1) {
63                                                 System.err.println("Unable to launch browser:");
64                                                 e1.printStackTrace();
65                                         }
66                                 }
67                         });
68
69                 }
70         }
71 }