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