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

Last change on this file since 6342 was 6342, checked in by Don-vip, 10 years ago

various fixes

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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 * @author Imi
19 * 5050: Simplifications by Zverikk included by akks
20 */
21public class UrlLabel extends JLabel implements MouseListener {
22
23 private String url = "";
24 private String description = "";
25
26 /**
27 * Constructs a new {@code UrlLabel}.
28 */
29 public UrlLabel() {
30 addMouseListener(this);
31 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
32 }
33
34 public UrlLabel(String url) {
35 this (url, url, 0);
36 }
37
38 public UrlLabel(String url, int fontPlus) {
39 this (url, url, fontPlus);
40 }
41
42 public UrlLabel(String url, String description) {
43 this (url, description, 0);
44 }
45
46 public UrlLabel(String url, String description, int fontPlus) {
47 this();
48 setUrl(url);
49 setDescription(description);
50 if (fontPlus!=0) {
51 setFont(getFont().deriveFont(0, getFont().getSize()+fontPlus));
52 }
53 refresh();
54 }
55
56 protected void refresh() {
57 if (url != null) {
58 setText("<html><a href=\""+url+"\">"+description+"</a></html>");
59 setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
60 setToolTipText(String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
61 } else {
62 setText("<html>" + description + "</html>");
63 setCursor(null);
64 setToolTipText(null);
65 }
66 }
67
68 /**
69 * Sets the URL to be visited if the user clicks on this URL label. If null, the
70 * label turns into a normal label without hyperlink.
71 *
72 * @param url the url. Can be null.
73 */
74 public void setUrl(String url) {
75 this.url = url;
76 refresh();
77 }
78
79 /**
80 * Sets the text part of the URL label. Defaults to the empty string if description is null.
81 *
82 * @param description the description
83 */
84 public void setDescription(String description) {
85 this.description = description == null? "" : description;
86 this.description = this.description.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
87 refresh();
88 }
89
90 @Override
91 public void mouseClicked(MouseEvent e) {
92 if( SwingUtilities.isLeftMouseButton(e) ) {
93 OpenBrowser.displayUrl(url);
94 } else if( SwingUtilities.isRightMouseButton(e) ) {
95 Utils.copyToClipboard(url);
96 }
97 }
98 @Override
99 public void mousePressed(MouseEvent e) { }
100 @Override
101 public void mouseEntered(MouseEvent e) { }
102 @Override
103 public void mouseExited(MouseEvent e) { }
104 @Override
105 public void mouseReleased(MouseEvent e) { }
106
107}
Note: See TracBrowser for help on using the repository browser.