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

Last change on this file since 7509 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.Cursor;
5import java.awt.event.MouseEvent;
6import java.awt.event.MouseListener;
7
8import javax.swing.JLabel;
9import javax.swing.SwingUtilities;
10
11import org.openstreetmap.josm.tools.OpenBrowser;
12import org.openstreetmap.josm.tools.Utils;
13
14import static org.openstreetmap.josm.tools.I18n.tr;
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, 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("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
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 Utils.copyToClipboard(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}
Note: See TracBrowser for help on using the repository browser.