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 013/** 014 * A hyperlink {@link JLabel}. 015 * 016 * To indicate that the user can click on the text, it displays an appropriate 017 * mouse cursor and dotted underline when the mouse is inside the hover area. 018 */ 019public class TaggingPresetLabel extends JLabel { 020 021 protected final TaggingPreset t; 022 023 /** 024 * Constructs a new {@code PresetLabel}. 025 * @param t the tagging preset 026 */ 027 public TaggingPresetLabel(TaggingPreset t) { 028 super(t.getName() + " …"); 029 setIcon(t.getIcon()); 030 addMouseListener(new PresetLabelMouseListener(this)); 031 this.t = t; 032 } 033 034 /** 035 * Small helper class that manages the highlighting of the label on hover as well as opening 036 * the corresponding preset when clicked 037 */ 038 public static class PresetLabelMouseListener implements MouseListener { 039 protected final JLabel label; 040 protected final Font hover; 041 protected final Font normal; 042 043 /** 044 * Constructs a new {@code PresetLabelMouseListener}. 045 * @param lbl Label to highlight 046 */ 047 public PresetLabelMouseListener(JLabel lbl) { 048 label = lbl; 049 lbl.setCursor(new Cursor(Cursor.HAND_CURSOR)); 050 normal = label.getFont(); 051 hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED)); 052 } 053 054 @Override 055 public void mouseClicked(MouseEvent e) { 056 // Do nothing 057 } 058 059 @Override 060 public void mouseEntered(MouseEvent e) { 061 label.setFont(hover); 062 } 063 064 @Override 065 public void mouseExited(MouseEvent e) { 066 label.setFont(normal); 067 } 068 069 @Override 070 public void mousePressed(MouseEvent e) { 071 // Do nothing 072 } 073 074 @Override 075 public void mouseReleased(MouseEvent e) { 076 // Do nothing 077 } 078 } 079}