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.tagging.TaggingPreset;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
|
---|
25 | import org.openstreetmap.josm.tools.GBC;
|
---|
26 |
|
---|
27 | public 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 | }
|
---|