source: josm/trunk/src/org/openstreetmap/josm/tools/UrlLabel.java @ 5241

Revision 5050, 2.8 KB checked in by akks, 3 months ago (diff)

UrlLabel class simplification by Zverik, better label layout in VersionInfoPanel (see #7450, #7326)

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.awt.Cursor;
5import java.awt.event.MouseEvent;
6import java.awt.event.MouseListener;
7import javax.swing.JLabel;
8import javax.swing.SwingUtilities;
9import 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 */
16public 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        } else {
52            setText("<html>" + description + "</html>");
53        }
54        setToolTipText(String.format("<html>%s<br/>%s</html>",url, tr("Right click = copy to clipboard")));
55    }
56
57    /**
58     * Sets the URL to be visited if the user clicks on this URL label. If null, the
59     * label turns into a normal label without hyperlink.
60     *
61     * @param url the url. Can be null.
62     */
63    public void setUrl(String url) {
64        this.url = url;
65        refresh();
66    }
67
68    /**
69     * Sets the text part of the URL label. Defaults to the empty string if description is null.
70     *
71     * @param description the description
72     */
73    public void setDescription(String description) {
74        this.description = description == null? "" : description;
75        this.description = this.description.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
76        refresh();
77    }
78
79    @Override
80    public void mouseClicked(MouseEvent e) {
81        if( SwingUtilities.isLeftMouseButton(e) ) {
82            OpenBrowser.displayUrl(url);
83        } else if( SwingUtilities.isRightMouseButton(e) ) {
84            Utils.copyToClipboard(url);
85        }
86    }
87    @Override
88    public void mousePressed(MouseEvent e) {    }
89    @Override
90    public void mouseEntered(MouseEvent e) {    }
91    @Override
92    public void mouseExited(MouseEvent e) {    }
93    @Override
94    public void mouseReleased(MouseEvent e) {    }
95
96}
Note: See TracBrowser for help on using the repository browser.