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.Font;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.awt.event.MouseListener;
|
---|
10 |
|
---|
11 | import javax.swing.Icon;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.SwingUtilities;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
16 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
17 | import org.openstreetmap.josm.tools.Utils;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Label that contains a clickable link.
|
---|
21 | * @since 6340
|
---|
22 | */
|
---|
23 | public class UrlLabel extends JLabel implements MouseListener {
|
---|
24 |
|
---|
25 | private String url = "";
|
---|
26 | private String description = "";
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new empty {@code UrlLabel}.
|
---|
30 | */
|
---|
31 | public UrlLabel() {
|
---|
32 | init("", "", 0);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code UrlLabel} for the given URL.
|
---|
37 | * @param url The URL to use, also used as description
|
---|
38 | */
|
---|
39 | public UrlLabel(String url) {
|
---|
40 | this (url, url, 0);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code UrlLabel} for the given URL and font increase.
|
---|
45 | * @param url The URL to use, also used as description
|
---|
46 | * @param fontPlus The font increase in 1/72 of an inch units.
|
---|
47 | */
|
---|
48 | public UrlLabel(String url, int fontPlus) {
|
---|
49 | this (url, url, fontPlus);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructs a new {@code UrlLabel} for the given URL and description.
|
---|
54 | * @param url The URL to use
|
---|
55 | * @param description The description to display
|
---|
56 | */
|
---|
57 | public UrlLabel(String url, String description) {
|
---|
58 | this (url, description, 0);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Constructs a new {@code UrlLabel} for the given URL, description and font increase.
|
---|
63 | * @param url The URL to use
|
---|
64 | * @param description The description to display
|
---|
65 | * @param image The image to be displayed by the label instead of text
|
---|
66 | * @since 14822
|
---|
67 | */
|
---|
68 | public UrlLabel(String url, String description, Icon image) {
|
---|
69 | super(image);
|
---|
70 | init(url, description, 0);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Constructs a new {@code UrlLabel} for the given URL, description and font increase.
|
---|
75 | * @param url The URL to use
|
---|
76 | * @param description The description to display
|
---|
77 | * @param fontPlus The font increase in 1/72 of an inch units.
|
---|
78 | */
|
---|
79 | public UrlLabel(String url, String description, int fontPlus) {
|
---|
80 | init(url, description, fontPlus);
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void init(String url, String description, int fontPlus) {
|
---|
84 | addMouseListener(this);
|
---|
85 | setUrl(url);
|
---|
86 | setDescription(description);
|
---|
87 | if (fontPlus != 0) {
|
---|
88 | setFont(getFont().deriveFont(Font.PLAIN, (float) getFont().getSize()+fontPlus));
|
---|
89 | }
|
---|
90 | refresh();
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected final void refresh() {
|
---|
94 | if (url != null && !url.isEmpty()) {
|
---|
95 | refresh("<html><a color=\"" + JosmEditorPane.getLinkColor() + "\" href=\"" + url + "\">" + description + "</a></html>",
|
---|
96 | Cursor.getPredefinedCursor(Cursor.HAND_CURSOR),
|
---|
97 | String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
|
---|
98 | } else {
|
---|
99 | refresh("<html>" + description + "</html>", null, null);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void refresh(String text, Cursor cursor, String tooltip) {
|
---|
104 | boolean hasImage = getIcon() != null;
|
---|
105 | if (!hasImage) {
|
---|
106 | setText(text);
|
---|
107 | }
|
---|
108 | setCursor(cursor);
|
---|
109 | setToolTipText(tooltip);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Sets the URL to be visited if the user clicks on this URL label.
|
---|
114 | * If null or empty, the label turns into a normal label without hyperlink.
|
---|
115 | *
|
---|
116 | * @param url the url. Can be null.
|
---|
117 | */
|
---|
118 | public final void setUrl(String url) {
|
---|
119 | this.url = url;
|
---|
120 | refresh();
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
125 | *
|
---|
126 | * @param description the description
|
---|
127 | */
|
---|
128 | public final void setDescription(String description) {
|
---|
129 | setDescription(description, true);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
134 | *
|
---|
135 | * @param description the description
|
---|
136 | * @param escapeReservedCharacters if {@code true}, HTML reserved characters will be escaped
|
---|
137 | * @since 13853
|
---|
138 | */
|
---|
139 | public final void setDescription(String description, boolean escapeReservedCharacters) {
|
---|
140 | this.description = description == null ? "" : description;
|
---|
141 | if (escapeReservedCharacters) {
|
---|
142 | this.description = Utils.escapeReservedCharactersHTML(this.description);
|
---|
143 | }
|
---|
144 | refresh();
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public void mouseClicked(MouseEvent e) {
|
---|
149 | if (url != null && !url.isEmpty()) {
|
---|
150 | if (SwingUtilities.isLeftMouseButton(e)) {
|
---|
151 | OpenBrowser.displayUrl(url);
|
---|
152 | } else if (SwingUtilities.isRightMouseButton(e)) {
|
---|
153 | ClipboardUtils.copyString(url);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void mousePressed(MouseEvent e) {
|
---|
160 | // Ignored
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public void mouseEntered(MouseEvent e) {
|
---|
165 | // Ignored
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public void mouseExited(MouseEvent e) {
|
---|
170 | // Ignored
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public void mouseReleased(MouseEvent e) {
|
---|
175 | // Ignored
|
---|
176 | }
|
---|
177 | }
|
---|