[7128] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.tagging;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Cursor;
|
---|
| 5 | import java.awt.Font;
|
---|
| 6 | import java.awt.event.MouseEvent;
|
---|
| 7 | import java.awt.event.MouseListener;
|
---|
| 8 | import java.awt.font.TextAttribute;
|
---|
| 9 | import java.util.Collections;
|
---|
| 10 |
|
---|
[8130] | 11 | import javax.swing.JLabel;
|
---|
| 12 |
|
---|
[7128] | 13 | public class PresetLabel extends JLabel {
|
---|
| 14 |
|
---|
| 15 | protected final TaggingPreset t;
|
---|
| 16 |
|
---|
[8130] | 17 | /**
|
---|
| 18 | * Constructs a new {@code PresetLabel}.
|
---|
| 19 | * @param t the tagging preset
|
---|
| 20 | */
|
---|
[7128] | 21 | public PresetLabel(TaggingPreset t) {
|
---|
| 22 | super(t.getName() + " …");
|
---|
| 23 | setIcon(t.getIcon());
|
---|
| 24 | addMouseListener(new PresetLabelMouseListener(this));
|
---|
| 25 | this.t = t;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Small helper class that manages the highlighting of the label on hover as well as opening
|
---|
| 30 | * the corresponding preset when clicked
|
---|
| 31 | */
|
---|
| 32 | public static class PresetLabelMouseListener implements MouseListener {
|
---|
[8130] | 33 | protected final JLabel label;
|
---|
| 34 | protected final Font hover;
|
---|
| 35 | protected final Font normal;
|
---|
[7128] | 36 |
|
---|
[8130] | 37 | /**
|
---|
| 38 | * Constructs a new {@code PresetLabelMouseListener}.
|
---|
| 39 | * @param lbl Label to highlight
|
---|
| 40 | */
|
---|
[7128] | 41 | public PresetLabelMouseListener(JLabel lbl) {
|
---|
| 42 | label = lbl;
|
---|
| 43 | lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
---|
| 44 | normal = label.getFont();
|
---|
| 45 | hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
|
---|
| 46 | }
|
---|
[8130] | 47 |
|
---|
[7128] | 48 | @Override
|
---|
[8130] | 49 | public void mouseClicked(MouseEvent e) {
|
---|
| 50 | // Do nothing
|
---|
[7128] | 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
[8130] | 54 | public void mouseEntered(MouseEvent e) {
|
---|
[7128] | 55 | label.setFont(hover);
|
---|
| 56 | }
|
---|
[8130] | 57 |
|
---|
[7128] | 58 | @Override
|
---|
[8130] | 59 | public void mouseExited(MouseEvent e) {
|
---|
[7128] | 60 | label.setFont(normal);
|
---|
| 61 | }
|
---|
[8130] | 62 |
|
---|
[7128] | 63 | @Override
|
---|
[8130] | 64 | public void mousePressed(MouseEvent e) {
|
---|
| 65 | // Do nothing
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[7128] | 68 | @Override
|
---|
[8130] | 69 | public void mouseReleased(MouseEvent e) {
|
---|
| 70 | // Do nothing
|
---|
| 71 | }
|
---|
[7128] | 72 | }
|
---|
| 73 | }
|
---|