1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components;
21
22 import java.awt.Color;
23 import java.awt.Cursor;
24 import java.awt.Desktop;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
27 import java.io.IOException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30
31 import javax.swing.JLabel;
32
33 /**
34 *
35 * <code>HyperLinkLabel</code> is an extension of the JLabel which provides a
36 * hyperlink and opens a desktop browser with this link when clicked.
37 * The hyper link is aslo visible in the tooltip.
38 * <p>
39 * This class will work properly only in Java 1.6, since URI launch is not
40 * available in earlier versions.
41 * </p>
42 *
43 * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
44 *
45 */
46 public class HyperLinkLabel extends JLabel {
47
48 private static final long serialVersionUID = 1L;
49 private String hyperLink;
50 private URI uri;
51
52 /**
53 * Constructs a new HyperLinkLabel.
54 *
55 */
56 public HyperLinkLabel() {
57 super();
58 initialize();
59 }
60
61 /**
62 * Constructs a new HyperLinkLabel with the supplied text visible on the label.
63 *
64 * @see JLabel
65 * @param text the text to be printed on the label
66 */
67 public HyperLinkLabel(String text) {
68 super(text);
69 initialize();
70 }
71
72 /**
73 * Constructs a new HyperLinkLabel with the supplied text visible on the
74 * label and the hyperlink attached to it.
75 *
76 * @param text the text to be printed on the label
77 * @param hyperLink the hyperlink of this label
78 * @throws URISyntaxException if the provided hyperlink is not a valid URI
79 */
80 public HyperLinkLabel(String text, String hyperLink) throws URISyntaxException {
81 super(text);
82 initialize();
83 setHyperLink(hyperLink);
84 setForeground(Color.BLUE);
85 }
86
87 /**
88 * Initializes the layout.
89 *
90 */
91 private void initialize() {
92 setForeground(Color.BLUE);
93 addMouseListener(new MouseAdapter(){
94 @Override
95 public void mouseClicked(MouseEvent e) {
96 if (uri == null) return;
97 try {
98 Desktop.getDesktop().browse(uri);
99 } catch (IOException e1) {
100 e1.printStackTrace();
101 }
102 }
103 public void mouseEntered(MouseEvent e){
104 e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
105 }
106 });
107 }
108
109 /**
110 * Sets the hyperlink for this label. The hyperlink is visible in the
111 * tooltip of the label and is opened if the label is clicked.
112 *
113 * @param url the hyperlink
114 * @throws URISyntaxException if the provided hyperlink cannot be transformed
115 * to a valid URI
116 */
117 public void setHyperLink(String url) throws URISyntaxException {
118 if (this.hyperLink != null && this.hyperLink.equals(url)) return;
119 String oldValue = this.hyperLink;
120 this.hyperLink = url;
121 uri = new URI(hyperLink);
122 setToolTipText(hyperLink);
123 firePropertyChange("hyperLink", oldValue, this.hyperLink);
124 }
125
126 /**
127 * Returns the hyperlink of this label.
128 *
129 * @return the hyperlink
130 */
131 public String getHyperLink() {
132 return hyperLink;
133 }
134 }