Bug fixes and startup checks
[debian/openrocket] / src / net / sf / openrocket / gui / components / URLLabel.java
1 package net.sf.openrocket.gui.components;
2
3 import java.awt.Desktop;
4 import java.awt.event.MouseAdapter;
5 import java.awt.event.MouseEvent;
6 import java.io.IOException;
7 import java.net.URI;
8 import java.net.URISyntaxException;
9
10 import javax.swing.JLabel;
11
12 /**
13  * A label of a URL that is clickable.  Clicking the URL will launch the URL in
14  * the default browser if the Desktop class is supported.
15  * 
16  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17  */
18 public class URLLabel extends JLabel {
19         
20         private final String url;
21         
22         public URLLabel(String urlLabel) {
23                 super();
24                 
25                 this.url = urlLabel;
26                 
27
28                 if (Desktop.isDesktopSupported()) {
29                         
30                         setText("<html><a href=\"" + url + "\">" + url + "</a>");
31
32                         this.addMouseListener(new MouseAdapter() {
33                                 @Override
34                                 public void mouseClicked(MouseEvent e) {
35                                         Desktop d = Desktop.getDesktop();
36                                         try {
37                                                 d.browse(new URI(url));
38                                         } catch (URISyntaxException e1) {
39                                                 throw new RuntimeException("BUG: Illegal URL: " + url, e1);
40                                         } catch (IOException e1) {
41                                                 System.err.println("Unable to launch browser:");
42                                                 e1.printStackTrace();
43                                         }
44                                 }
45                         });
46                         
47                 } else {
48                         setText(url);
49                 }
50         }
51 }