source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetLabel.java@ 13050

Last change on this file since 13050 was 12646, checked in by bastiK, 7 years ago

see #14794 - javadoc

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import java.awt.Cursor;
5import java.awt.Font;
6import java.awt.event.MouseEvent;
7import java.awt.event.MouseListener;
8import java.awt.font.TextAttribute;
9import java.util.Collections;
10
11import javax.swing.JLabel;
12
13/**
14 * A hyperlink {@link JLabel}.
15 *
16 * To indicate that the user can click on the text, it displays an appropriate
17 * mouse cursor and dotted underline when the mouse is inside the hover area.
18 */
19public class TaggingPresetLabel extends JLabel {
20
21 protected final TaggingPreset t;
22
23 /**
24 * Constructs a new {@code PresetLabel}.
25 * @param t the tagging preset
26 */
27 public TaggingPresetLabel(TaggingPreset t) {
28 super(t.getName() + " …");
29 setIcon(t.getIcon());
30 addMouseListener(new PresetLabelMouseListener(this));
31 this.t = t;
32 }
33
34 /**
35 * Small helper class that manages the highlighting of the label on hover as well as opening
36 * the corresponding preset when clicked
37 */
38 public static class PresetLabelMouseListener implements MouseListener {
39 protected final JLabel label;
40 protected final Font hover;
41 protected final Font normal;
42
43 /**
44 * Constructs a new {@code PresetLabelMouseListener}.
45 * @param lbl Label to highlight
46 */
47 public PresetLabelMouseListener(JLabel lbl) {
48 label = lbl;
49 lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
50 normal = label.getFont();
51 hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
52 }
53
54 @Override
55 public void mouseClicked(MouseEvent e) {
56 // Do nothing
57 }
58
59 @Override
60 public void mouseEntered(MouseEvent e) {
61 label.setFont(hover);
62 }
63
64 @Override
65 public void mouseExited(MouseEvent e) {
66 label.setFont(normal);
67 }
68
69 @Override
70 public void mousePressed(MouseEvent e) {
71 // Do nothing
72 }
73
74 @Override
75 public void mouseReleased(MouseEvent e) {
76 // Do nothing
77 }
78 }
79}
Note: See TracBrowser for help on using the repository browser.