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 |
|
---|
11 | import javax.swing.JLabel;
|
---|
12 |
|
---|
13 | public class PresetLabel extends JLabel {
|
---|
14 |
|
---|
15 | protected final TaggingPreset t;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Constructs a new {@code PresetLabel}.
|
---|
19 | * @param t the tagging preset
|
---|
20 | */
|
---|
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 {
|
---|
33 | protected final JLabel label;
|
---|
34 | protected final Font hover;
|
---|
35 | protected final Font normal;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Constructs a new {@code PresetLabelMouseListener}.
|
---|
39 | * @param lbl Label to highlight
|
---|
40 | */
|
---|
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 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void mouseClicked(MouseEvent e) {
|
---|
50 | // Do nothing
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void mouseEntered(MouseEvent e) {
|
---|
55 | label.setFont(hover);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void mouseExited(MouseEvent e) {
|
---|
60 | label.setFont(normal);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void mousePressed(MouseEvent e) {
|
---|
65 | // Do nothing
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void mouseReleased(MouseEvent e) {
|
---|
70 | // Do nothing
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|