001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.tools;
003    
004    import java.awt.Cursor;
005    import java.awt.event.MouseEvent;
006    import java.awt.event.MouseListener;
007    import javax.swing.JLabel;
008    import javax.swing.SwingUtilities;
009    import static org.openstreetmap.josm.tools.I18n.tr;
010    
011    /**
012     * Label that contains a clickable link.
013     * @author Imi
014     * 5050: Simplifications by Zverikk included by akks
015     */
016    public class UrlLabel extends JLabel implements MouseListener {
017    
018        private String url = "";
019        private String description = "";
020        private int fontPlus;
021    
022        public UrlLabel() {
023            addMouseListener(this);
024            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
025        }
026    
027        public UrlLabel(String url) {
028            this (url, url, 0);
029        }
030        
031        public UrlLabel(String url, int fontPlus) {
032            this (url, url, fontPlus);
033        }
034    
035        public UrlLabel(String url, String description) {
036            this (url, url, 0);
037        }
038        
039        public UrlLabel(String url, String description, int fontPlus) {
040            this();
041            setUrl(url);
042            setDescription(description);
043            this.fontPlus = fontPlus;
044            if (fontPlus!=0) setFont(getFont().deriveFont(0, getFont().getSize()+fontPlus));
045            refresh();
046        }
047    
048        protected void refresh() {
049            if (url != null) {
050                setText("<html><a href=\""+url+"\">"+description+"</a></html>");
051                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
052                setToolTipText(String.format("<html>%s<br/>%s</html>", url, tr("Right click = copy to clipboard")));
053            } else {
054                setText("<html>" + description + "</html>");
055                setCursor(null);
056                setToolTipText(null);
057            }
058        }
059    
060        /**
061         * Sets the URL to be visited if the user clicks on this URL label. If null, the
062         * label turns into a normal label without hyperlink.
063         *
064         * @param url the url. Can be null.
065         */
066        public void setUrl(String url) {
067            this.url = url;
068            refresh();
069        }
070    
071        /**
072         * Sets the text part of the URL label. Defaults to the empty string if description is null.
073         *
074         * @param description the description
075         */
076        public void setDescription(String description) {
077            this.description = description == null? "" : description;
078            this.description = this.description.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;");
079            refresh();
080        }
081    
082        @Override
083        public void mouseClicked(MouseEvent e) {
084            if( SwingUtilities.isLeftMouseButton(e) ) {
085                OpenBrowser.displayUrl(url);
086            } else if( SwingUtilities.isRightMouseButton(e) ) {
087                Utils.copyToClipboard(url);
088            }
089        }
090        @Override
091        public void mousePressed(MouseEvent e) {    }
092        @Override
093        public void mouseEntered(MouseEvent e) {    }
094        @Override
095        public void mouseExited(MouseEvent e) {    }
096        @Override
097        public void mouseReleased(MouseEvent e) {    }
098    
099    }