source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/UrlLabel.java@ 11921

Last change on this file since 11921 was 11416, checked in by Don-vip, 7 years ago

fix #14151 - see #14042 - fix NPE properly (regression from r11306) + add robustness

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Cursor;
7import java.awt.event.MouseEvent;
8import java.awt.event.MouseListener;
9
10import javax.swing.JLabel;
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
14import org.openstreetmap.josm.tools.OpenBrowser;
15
16/**
17 * Label that contains a clickable link.
18 * @since 6340
19 */
20public 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 && !url.isEmpty()) {
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.
89 * If null or empty, the 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("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
106 refresh();
107 }
108
109 @Override
110 public void mouseClicked(MouseEvent e) {
111 if (url != null && !url.isEmpty()) {
112 if (SwingUtilities.isLeftMouseButton(e)) {
113 OpenBrowser.displayUrl(url);
114 } else if (SwingUtilities.isRightMouseButton(e)) {
115 ClipboardUtils.copyString(url);
116 }
117 }
118 }
119
120 @Override
121 public void mousePressed(MouseEvent e) {
122 // Ignored
123 }
124
125 @Override
126 public void mouseEntered(MouseEvent e) {
127 // Ignored
128 }
129
130 @Override
131 public void mouseExited(MouseEvent e) {
132 // Ignored
133 }
134
135 @Override
136 public void mouseReleased(MouseEvent e) {
137 // Ignored
138 }
139}
Note: See TracBrowser for help on using the repository browser.