[6365] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[6340] | 2 | package org.openstreetmap.josm.gui.widgets;
|
---|
[626] | 3 |
|
---|
[8510] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[5050] | 6 | import java.awt.Cursor;
|
---|
[5019] | 7 | import java.awt.event.MouseEvent;
|
---|
| 8 | import java.awt.event.MouseListener;
|
---|
[6340] | 9 |
|
---|
[5050] | 10 | import javax.swing.JLabel;
|
---|
| 11 | import javax.swing.SwingUtilities;
|
---|
[6340] | 12 |
|
---|
[10604] | 13 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[6340] | 14 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
| 15 |
|
---|
[626] | 16 | /**
|
---|
| 17 | * Label that contains a clickable link.
|
---|
[6365] | 18 | * @since 6340
|
---|
[626] | 19 | */
|
---|
[5050] | 20 | public class UrlLabel extends JLabel implements MouseListener {
|
---|
[626] | 21 |
|
---|
[2243] | 22 | private String url = "";
|
---|
| 23 | private String description = "";
|
---|
[626] | 24 |
|
---|
[6267] | 25 | /**
|
---|
[6365] | 26 | * Constructs a new empty {@code UrlLabel}.
|
---|
[6267] | 27 | */
|
---|
[2243] | 28 | public UrlLabel() {
|
---|
[5019] | 29 | addMouseListener(this);
|
---|
[5050] | 30 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
[2243] | 31 | }
|
---|
| 32 |
|
---|
[6365] | 33 | /**
|
---|
| 34 | * Constructs a new {@code UrlLabel} for the given URL.
|
---|
| 35 | * @param url The URL to use, also used as description
|
---|
| 36 | */
|
---|
[1169] | 37 | public UrlLabel(String url) {
|
---|
[5050] | 38 | this (url, url, 0);
|
---|
[1169] | 39 | }
|
---|
[6070] | 40 |
|
---|
[6365] | 41 | /**
|
---|
| 42 | * Constructs a new {@code UrlLabel} for the given URL and font increase.
|
---|
| 43 | * @param url The URL to use, also used as description
|
---|
| 44 | * @param fontPlus The font increase in 1/72 of an inch units.
|
---|
| 45 | */
|
---|
[5050] | 46 | public UrlLabel(String url, int fontPlus) {
|
---|
| 47 | this (url, url, fontPlus);
|
---|
| 48 | }
|
---|
[626] | 49 |
|
---|
[6365] | 50 | /**
|
---|
| 51 | * Constructs a new {@code UrlLabel} for the given URL and description.
|
---|
| 52 | * @param url The URL to use
|
---|
[7509] | 53 | * @param description The description to display
|
---|
[6365] | 54 | */
|
---|
[1169] | 55 | public UrlLabel(String url, String description) {
|
---|
[6342] | 56 | this (url, description, 0);
|
---|
[5050] | 57 | }
|
---|
[6070] | 58 |
|
---|
[6365] | 59 | /**
|
---|
| 60 | * Constructs a new {@code UrlLabel} for the given URL, description and font increase.
|
---|
| 61 | * @param url The URL to use
|
---|
[7509] | 62 | * @param description The description to display
|
---|
[6365] | 63 | * @param fontPlus The font increase in 1/72 of an inch units.
|
---|
| 64 | */
|
---|
[5050] | 65 | public UrlLabel(String url, String description, int fontPlus) {
|
---|
[2243] | 66 | this();
|
---|
| 67 | setUrl(url);
|
---|
| 68 | setDescription(description);
|
---|
[8510] | 69 | if (fontPlus != 0) {
|
---|
[10181] | 70 | setFont(getFont().deriveFont(0, (float) getFont().getSize()+fontPlus));
|
---|
[6267] | 71 | }
|
---|
[2243] | 72 | refresh();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[6365] | 75 | protected final void refresh() {
|
---|
[2250] | 76 | if (url != null) {
|
---|
| 77 | setText("<html><a href=\""+url+"\">"+description+"</a></html>");
|
---|
[5440] | 78 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
| 79 | setToolTipText(String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
|
---|
[2250] | 80 | } else {
|
---|
| 81 | setText("<html>" + description + "</html>");
|
---|
[5440] | 82 | setCursor(null);
|
---|
[6101] | 83 | setToolTipText(null);
|
---|
[2250] | 84 | }
|
---|
[1169] | 85 | }
|
---|
[626] | 86 |
|
---|
[2318] | 87 | /**
|
---|
| 88 | * Sets the URL to be visited if the user clicks on this URL label. If null, the
|
---|
| 89 | * label turns into a normal label without hyperlink.
|
---|
[2512] | 90 | *
|
---|
[2318] | 91 | * @param url the url. Can be null.
|
---|
| 92 | */
|
---|
[6365] | 93 | public final void setUrl(String url) {
|
---|
[2318] | 94 | this.url = url;
|
---|
[2243] | 95 | refresh();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2318] | 98 | /**
|
---|
| 99 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
[2512] | 100 | *
|
---|
[2318] | 101 | * @param description the description
|
---|
| 102 | */
|
---|
[6365] | 103 | public final void setDescription(String description) {
|
---|
[8510] | 104 | this.description = description == null ? "" : description;
|
---|
[2318] | 105 | this.description = this.description.replace("&", "&").replace(">", ">").replace("<", "<");
|
---|
[2243] | 106 | refresh();
|
---|
| 107 | }
|
---|
[5019] | 108 |
|
---|
| 109 | @Override
|
---|
[5050] | 110 | public void mouseClicked(MouseEvent e) {
|
---|
[6365] | 111 | if (SwingUtilities.isLeftMouseButton(e)) {
|
---|
[5050] | 112 | OpenBrowser.displayUrl(url);
|
---|
[6365] | 113 | } else if (SwingUtilities.isRightMouseButton(e)) {
|
---|
[10604] | 114 | ClipboardUtils.copyString(url);
|
---|
[5050] | 115 | }
|
---|
| 116 | }
|
---|
[7509] | 117 |
|
---|
[5019] | 118 | @Override
|
---|
[6365] | 119 | public void mousePressed(MouseEvent e) {
|
---|
| 120 | // Ignored
|
---|
| 121 | }
|
---|
[7509] | 122 |
|
---|
[5019] | 123 | @Override
|
---|
[6365] | 124 | public void mouseEntered(MouseEvent e) {
|
---|
| 125 | // Ignored
|
---|
| 126 | }
|
---|
[7509] | 127 |
|
---|
[5019] | 128 | @Override
|
---|
[6365] | 129 | public void mouseExited(MouseEvent e) {
|
---|
| 130 | // Ignored
|
---|
| 131 | }
|
---|
[7509] | 132 |
|
---|
[5019] | 133 | @Override
|
---|
[6365] | 134 | public void mouseReleased(MouseEvent e) {
|
---|
| 135 | // Ignored
|
---|
| 136 | }
|
---|
[626] | 137 | }
|
---|