1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import javax.swing.JEditorPane;
|
---|
5 | import javax.swing.event.HyperlinkEvent;
|
---|
6 | import javax.swing.event.HyperlinkListener;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Label that contains a clickable link.
|
---|
10 | * @author Imi
|
---|
11 | */
|
---|
12 | public class UrlLabel extends JEditorPane implements HyperlinkListener {
|
---|
13 |
|
---|
14 | private String url = "";
|
---|
15 | private String description = "";
|
---|
16 |
|
---|
17 | public UrlLabel() {
|
---|
18 | addHyperlinkListener(this);
|
---|
19 | setEditable(false);
|
---|
20 | setOpaque(false);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public UrlLabel(String url) {
|
---|
24 | this (url, url);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public UrlLabel(String url, String description) {
|
---|
28 | this();
|
---|
29 | setUrl(url);
|
---|
30 | setDescription(description);
|
---|
31 | refresh();
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected void refresh() {
|
---|
35 | setContentType("text/html");
|
---|
36 | if (url != null) {
|
---|
37 | setText("<html><a href=\""+url+"\">"+description+"</a></html>");
|
---|
38 | } else {
|
---|
39 | setText("<html>" + description + "</html>");
|
---|
40 | }
|
---|
41 | setToolTipText(url);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
45 | if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
|
---|
46 | OpenBrowser.displayUrl(url);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Sets the URL to be visited if the user clicks on this URL label. If null, the
|
---|
52 | * label turns into a normal label without hyperlink.
|
---|
53 | *
|
---|
54 | * @param url the url. Can be null.
|
---|
55 | */
|
---|
56 | public void setUrl(String url) {
|
---|
57 | this.url = url;
|
---|
58 | refresh();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
63 | *
|
---|
64 | * @param description the description
|
---|
65 | */
|
---|
66 | public void setDescription(String description) {
|
---|
67 | this.description = description == null? "" : description;
|
---|
68 | this.description = this.description.replace("&", "&").replace(">", ">").replace("<", "<");
|
---|
69 | refresh();
|
---|
70 | }
|
---|
71 | }
|
---|