1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.awt.event.MouseListener;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.SwingUtilities;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
12 | import org.openstreetmap.josm.tools.Utils;
|
---|
13 |
|
---|
14 | import 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 | */
|
---|
21 | public 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("&", "&").replace(">", ">").replace("<", "<");
|
---|
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 | }
|
---|