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