[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 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 |
|
---|
[2243] | 14 | private String url = "";
|
---|
| 15 | private String description = "";
|
---|
[626] | 16 |
|
---|
[2243] | 17 | public UrlLabel() {
|
---|
| 18 | addHyperlinkListener(this);
|
---|
| 19 | setEditable(false);
|
---|
| 20 | setOpaque(false);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[1169] | 23 | public UrlLabel(String url) {
|
---|
| 24 | this (url, url);
|
---|
| 25 | }
|
---|
[626] | 26 |
|
---|
[1169] | 27 | public UrlLabel(String url, String description) {
|
---|
[2243] | 28 | this();
|
---|
| 29 | setUrl(url);
|
---|
| 30 | setDescription(description);
|
---|
| 31 | refresh();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | protected void refresh() {
|
---|
[1169] | 35 | setContentType("text/html");
|
---|
[2250] | 36 | if (url != null) {
|
---|
| 37 | setText("<html><a href=\""+url+"\">"+description+"</a></html>");
|
---|
| 38 | } else {
|
---|
| 39 | setText("<html>" + description + "</html>");
|
---|
| 40 | }
|
---|
[1169] | 41 | setToolTipText(url);
|
---|
| 42 | }
|
---|
[626] | 43 |
|
---|
[1169] | 44 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
| 45 | if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
|
---|
| 46 | OpenBrowser.displayUrl(url);
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[2243] | 49 |
|
---|
[2318] | 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.
|
---|
[2512] | 53 | *
|
---|
[2318] | 54 | * @param url the url. Can be null.
|
---|
| 55 | */
|
---|
[2243] | 56 | public void setUrl(String url) {
|
---|
[2318] | 57 | this.url = url;
|
---|
[2243] | 58 | refresh();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[2318] | 61 | /**
|
---|
| 62 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
[2512] | 63 | *
|
---|
[2318] | 64 | * @param description the description
|
---|
| 65 | */
|
---|
[2243] | 66 | public void setDescription(String description) {
|
---|
| 67 | this.description = description == null? "" : description;
|
---|
[2318] | 68 | this.description = this.description.replace("&", "&").replace(">", ">").replace("<", "<");
|
---|
[2243] | 69 | refresh();
|
---|
| 70 | }
|
---|
[626] | 71 | }
|
---|