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