1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.awt.event.MouseListener;
|
---|
7 | import javax.swing.JLabel;
|
---|
8 | import javax.swing.SwingUtilities;
|
---|
9 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Label that contains a clickable link.
|
---|
13 | * @author Imi
|
---|
14 | * 5050: Simplifications by Zverikk included by akks
|
---|
15 | */
|
---|
16 | public class UrlLabel extends JLabel implements MouseListener {
|
---|
17 |
|
---|
18 | private String url = "";
|
---|
19 | private String description = "";
|
---|
20 | private int fontPlus;
|
---|
21 |
|
---|
22 | public UrlLabel() {
|
---|
23 | addMouseListener(this);
|
---|
24 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
25 | }
|
---|
26 |
|
---|
27 | public UrlLabel(String url) {
|
---|
28 | this (url, url, 0);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public UrlLabel(String url, int fontPlus) {
|
---|
32 | this (url, url, fontPlus);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public UrlLabel(String url, String description) {
|
---|
36 | this (url, url, 0);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public UrlLabel(String url, String description, int fontPlus) {
|
---|
40 | this();
|
---|
41 | setUrl(url);
|
---|
42 | setDescription(description);
|
---|
43 | this.fontPlus = fontPlus;
|
---|
44 | if (fontPlus!=0) setFont(getFont().deriveFont(0, getFont().getSize()+fontPlus));
|
---|
45 | refresh();
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected void refresh() {
|
---|
49 | if (url != null) {
|
---|
50 | setText("<html><a href=\""+url+"\">"+description+"</a></html>");
|
---|
51 | setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
52 | setToolTipText(String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
|
---|
53 | } else {
|
---|
54 | setText("<html>" + description + "</html>");
|
---|
55 | setCursor(null);
|
---|
56 | setToolTipText(null);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Sets the URL to be visited if the user clicks on this URL label. If null, the
|
---|
62 | * label turns into a normal label without hyperlink.
|
---|
63 | *
|
---|
64 | * @param url the url. Can be null.
|
---|
65 | */
|
---|
66 | public void setUrl(String url) {
|
---|
67 | this.url = url;
|
---|
68 | refresh();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Sets the text part of the URL label. Defaults to the empty string if description is null.
|
---|
73 | *
|
---|
74 | * @param description the description
|
---|
75 | */
|
---|
76 | public void setDescription(String description) {
|
---|
77 | this.description = description == null? "" : description;
|
---|
78 | this.description = this.description.replace("&", "&").replace(">", ">").replace("<", "<");
|
---|
79 | refresh();
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void mouseClicked(MouseEvent e) {
|
---|
84 | if( SwingUtilities.isLeftMouseButton(e) ) {
|
---|
85 | OpenBrowser.displayUrl(url);
|
---|
86 | } else if( SwingUtilities.isRightMouseButton(e) ) {
|
---|
87 | Utils.copyToClipboard(url);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | @Override
|
---|
91 | public void mousePressed(MouseEvent e) { }
|
---|
92 | @Override
|
---|
93 | public void mouseEntered(MouseEvent e) { }
|
---|
94 | @Override
|
---|
95 | public void mouseExited(MouseEvent e) { }
|
---|
96 | @Override
|
---|
97 | public void mouseReleased(MouseEvent e) { }
|
---|
98 |
|
---|
99 | }
|
---|