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

Last change on this file since 5170 was 5170, checked in by Don-vip, 12 years ago

cleanup svn:mime-type properties preventing Java sources from being viewed as such on Trac

  • Property svn:eol-style set to native
File size: 3.7 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.preferences.map.TaggingPresetPreference;
24import org.openstreetmap.josm.gui.tagging.TaggingPreset;
25import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
26import org.openstreetmap.josm.tools.GBC;
27
28public class PresetListPanel extends JPanel {
29
30 public PresetListPanel() {
31 super(new GridBagLayout());
32 }
33
34 public interface PresetHandler {
35 Collection<OsmPrimitive> getSelection();
36 void updateTags(List<Tag> tags);
37 }
38
39 /**
40 * Small helper class that manages the highlighting of the label on hover as well as opening
41 * the corresponding preset when clicked
42 */
43 private static class PresetLabelML implements MouseListener {
44 final JLabel label;
45 final Font hover;
46 final Font normal;
47 final TaggingPreset tag;
48 final PresetHandler presetHandler;
49
50 PresetLabelML(JLabel lbl, TaggingPreset t, PresetHandler presetHandler) {
51 super();
52 label = lbl;
53 lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
54 normal = label.getFont();
55 hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
56 tag = t;
57 this.presetHandler = presetHandler;
58 }
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 public void mouseEntered(MouseEvent arg0) {
71 label.setFont(hover);
72 }
73 public void mouseExited(MouseEvent arg0) {
74 label.setFont(normal);
75 }
76 public void mousePressed(MouseEvent arg0) {}
77 public void mouseReleased(MouseEvent arg0) {}
78 }
79
80 public void updatePresets(final Collection<PresetType> types, final Map<String, String> tags, PresetHandler presetHandler) {
81
82 removeAll();
83 if (types.isEmpty()) {
84 setVisible(false);
85 return;
86 }
87
88 for (TaggingPreset t : TaggingPresetPreference.taggingPresets) {
89 if (!t.matches(types, tags, true)) {
90 continue;
91 }
92
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.