[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[626] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[5050] | 4 | import java.awt.Cursor;
|
---|
[5019] | 5 | import java.awt.event.MouseEvent;
|
---|
| 6 | import java.awt.event.MouseListener;
|
---|
[5050] | 7 | import javax.swing.JLabel;
|
---|
| 8 | import javax.swing.SwingUtilities;
|
---|
[5019] | 9 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[626] | 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Label that contains a clickable link.
|
---|
| 13 | * @author Imi
|
---|
[5050] | 14 | * 5050: Simplifications by Zverikk included by akks
|
---|
[626] | 15 | */
|
---|
[5050] | 16 | public class UrlLabel extends JLabel implements MouseListener {
|
---|
[626] | 17 |
|
---|
[2243] | 18 | private String url = "";
|
---|
| 19 | private String description = "";
|
---|
[626] | 20 |
|
---|
[6267] | 21 | /**
|
---|
| 22 | * Constructs a new {@code UrlLabel}.
|
---|
| 23 | */
|
---|
[2243] | 24 | public UrlLabel() {
|
---|
[5019] | 25 | addMouseListener(this);
|
---|
[5050] | 26 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
[2243] | 27 | }
|
---|
| 28 |
|
---|
[1169] | 29 | public UrlLabel(String url) {
|
---|
[5050] | 30 | this (url, url, 0);
|
---|
[1169] | 31 | }
|
---|
[6070] | 32 |
|
---|
[5050] | 33 | public UrlLabel(String url, int fontPlus) {
|
---|
| 34 | this (url, url, fontPlus);
|
---|
| 35 | }
|
---|
[626] | 36 |
|
---|
[1169] | 37 | public UrlLabel(String url, String description) {
|
---|
[5050] | 38 | this (url, url, 0);
|
---|
| 39 | }
|
---|
[6070] | 40 |
|
---|
[5050] | 41 | public UrlLabel(String url, String description, int fontPlus) {
|
---|
[2243] | 42 | this();
|
---|
| 43 | setUrl(url);
|
---|
| 44 | setDescription(description);
|
---|
[6267] | 45 | if (fontPlus!=0) {
|
---|
| 46 | setFont(getFont().deriveFont(0, getFont().getSize()+fontPlus));
|
---|
| 47 | }
|
---|
[2243] | 48 | refresh();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | protected void refresh() {
|
---|
[2250] | 52 | if (url != null) {
|
---|
| 53 | setText("<html><a href=\""+url+"\">"+description+"</a></html>");
|
---|
[5440] | 54 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
| 55 | setToolTipText(String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
|
---|
[2250] | 56 | } else {
|
---|
| 57 | setText("<html>" + description + "</html>");
|
---|
[5440] | 58 | setCursor(null);
|
---|
[6101] | 59 | setToolTipText(null);
|
---|
[2250] | 60 | }
|
---|
[1169] | 61 | }
|
---|
[626] | 62 |
|
---|
[2318] | 63 | /**
|
---|
| 64 | * Sets the URL to be visited if the user clicks on this URL label. If null, the
|
---|
| 65 | * label turns into a normal label without hyperlink.
|
---|
[2512] | 66 | *
|
---|
[2318] | 67 | * @param url the url. Can be null.
|
---|
| 68 | */
|
---|
[2243] | 69 | public void setUrl(String url) {
|
---|
[2318] | 70 | this.url = url;
|
---|
[2243] | 71 | refresh();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[2318] | 74 | /**
|
---|
| 75 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
[2512] | 76 | *
|
---|
[2318] | 77 | * @param description the description
|
---|
| 78 | */
|
---|
[2243] | 79 | public void setDescription(String description) {
|
---|
| 80 | this.description = description == null? "" : description;
|
---|
[2318] | 81 | this.description = this.description.replace("&", "&").replace(">", ">").replace("<", "<");
|
---|
[2243] | 82 | refresh();
|
---|
| 83 | }
|
---|
[5019] | 84 |
|
---|
| 85 | @Override
|
---|
[5050] | 86 | public void mouseClicked(MouseEvent e) {
|
---|
| 87 | if( SwingUtilities.isLeftMouseButton(e) ) {
|
---|
| 88 | OpenBrowser.displayUrl(url);
|
---|
| 89 | } else if( SwingUtilities.isRightMouseButton(e) ) {
|
---|
| 90 | Utils.copyToClipboard(url);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[5019] | 93 | @Override
|
---|
| 94 | public void mousePressed(MouseEvent e) { }
|
---|
| 95 | @Override
|
---|
| 96 | public void mouseEntered(MouseEvent e) { }
|
---|
| 97 | @Override
|
---|
| 98 | public void mouseExited(MouseEvent e) { }
|
---|
| 99 | @Override
|
---|
[5050] | 100 | public void mouseReleased(MouseEvent e) { }
|
---|
[5019] | 101 |
|
---|
[626] | 102 | }
|
---|