001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import java.awt.Cursor;
005import java.awt.Font;
006import java.awt.event.MouseEvent;
007import java.awt.event.MouseListener;
008import java.awt.font.TextAttribute;
009import java.util.Collections;
010
011import javax.swing.JLabel;
012
013public class TaggingPresetLabel extends JLabel {
014
015    protected final TaggingPreset t;
016
017    /**
018     * Constructs a new {@code PresetLabel}.
019     * @param t the tagging preset
020     */
021    public TaggingPresetLabel(TaggingPreset t) {
022        super(t.getName() + " …");
023        setIcon(t.getIcon());
024        addMouseListener(new PresetLabelMouseListener(this));
025        this.t = t;
026    }
027
028    /**
029     * Small helper class that manages the highlighting of the label on hover as well as opening
030     * the corresponding preset when clicked
031     */
032    public static class PresetLabelMouseListener implements MouseListener {
033        protected final JLabel label;
034        protected final Font hover;
035        protected final Font normal;
036
037        /**
038         * Constructs a new {@code PresetLabelMouseListener}.
039         * @param lbl Label to highlight
040         */
041        public PresetLabelMouseListener(JLabel lbl) {
042            label = lbl;
043            lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
044            normal = label.getFont();
045            hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
046        }
047
048        @Override
049        public void mouseClicked(MouseEvent e) {
050            // Do nothing
051        }
052
053        @Override
054        public void mouseEntered(MouseEvent e) {
055            label.setFont(hover);
056        }
057
058        @Override
059        public void mouseExited(MouseEvent e) {
060            label.setFont(normal);
061        }
062
063        @Override
064        public void mousePressed(MouseEvent e) {
065            // Do nothing
066        }
067
068        @Override
069        public void mouseReleased(MouseEvent e) {
070            // Do nothing
071        }
072    }
073}