source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PresetListPanel.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import java.awt.Cursor;
5import java.awt.Dimension;
6import java.awt.Font;
7import java.awt.GridBagLayout;
8import java.awt.event.MouseEvent;
9import java.awt.event.MouseListener;
10import java.awt.font.TextAttribute;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.List;
14import java.util.Map;
15
16import javax.swing.Action;
17import javax.swing.Icon;
18import javax.swing.JLabel;
19import javax.swing.JPanel;
20
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Tag;
23import org.openstreetmap.josm.gui.tagging.TaggingPreset;
24import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
25import org.openstreetmap.josm.tools.GBC;
26
27public class PresetListPanel extends JPanel {
28
29 public PresetListPanel() {
30 super(new GridBagLayout());
31 }
32
33 public interface PresetHandler {
34 Collection<OsmPrimitive> getSelection();
35 void updateTags(List<Tag> tags);
36 }
37
38 /**
39 * Small helper class that manages the highlighting of the label on hover as well as opening
40 * the corresponding preset when clicked
41 */
42 private static class PresetLabelML implements MouseListener {
43 final JLabel label;
44 final Font hover;
45 final Font normal;
46 final TaggingPreset tag;
47 final PresetHandler presetHandler;
48
49 PresetLabelML(JLabel lbl, TaggingPreset t, PresetHandler presetHandler) {
50 super();
51 label = lbl;
52 lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
53 normal = label.getFont();
54 hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
55 tag = t;
56 this.presetHandler = presetHandler;
57 }
58 @Override
59 public void mouseClicked(MouseEvent arg0) {
60 Collection<OsmPrimitive> selection = tag.createSelection(presetHandler.getSelection());
61 if (selection == null || selection.isEmpty())
62 return;
63 int answer = tag.showDialog(selection, false);
64
65 if (answer == TaggingPreset.DIALOG_ANSWER_APPLY) {
66 presetHandler.updateTags(tag.getChangedTags());
67 }
68
69 }
70 @Override
71 public void mouseEntered(MouseEvent arg0) {
72 label.setFont(hover);
73 }
74 @Override
75 public void mouseExited(MouseEvent arg0) {
76 label.setFont(normal);
77 }
78 @Override
79 public void mousePressed(MouseEvent arg0) {}
80 @Override
81 public void mouseReleased(MouseEvent arg0) {}
82 }
83
84 public void updatePresets(final Collection<TaggingPresetType> types, final Map<String, String> tags, PresetHandler presetHandler) {
85
86 removeAll();
87 if (types.isEmpty()) {
88 setVisible(false);
89 return;
90 }
91
92 for (TaggingPreset t : TaggingPreset.getMatchingPresets(types, tags, true)) {
93 JLabel lbl = new JLabel(t.getName() + " …");
94 lbl.setIcon((Icon) t.getValue(Action.SMALL_ICON));
95 lbl.addMouseListener(new PresetLabelML(lbl, t, presetHandler));
96 add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
97 }
98
99 if (getComponentCount() > 0) {
100 setVisible(true);
101 // This ensures the presets are exactly as high as needed.
102 int height = getComponentCount() * getComponent(0).getHeight();
103 Dimension size = new Dimension(getWidth(), height);
104 setMaximumSize(size);
105 setMinimumSize(size);
106 } else {
107 setVisible(false);
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.