1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.awt.event.MouseListener;
|
---|
10 | import java.awt.font.TextAttribute;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import javax.swing.Action;
|
---|
17 | import javax.swing.Icon;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
25 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 |
|
---|
28 | public 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 | }
|
---|