1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GridBagLayout;
|
---|
6 | import java.awt.event.MouseAdapter;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetLabel;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
---|
20 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
21 | import org.openstreetmap.josm.tools.GBC;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A list of matching presets for a set of tags.
|
---|
25 | */
|
---|
26 | public class PresetListPanel extends JPanel {
|
---|
27 |
|
---|
28 | static final class LabelMouseAdapter extends MouseAdapter {
|
---|
29 | private final TaggingPreset t;
|
---|
30 | private final TaggingPresetHandler presetHandler;
|
---|
31 |
|
---|
32 | LabelMouseAdapter(TaggingPreset t, TaggingPresetHandler presetHandler) {
|
---|
33 | this.t = t;
|
---|
34 | this.presetHandler = presetHandler;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void mouseClicked(MouseEvent e) {
|
---|
39 | Collection<OsmPrimitive> selection = t.createSelection(presetHandler.getSelection());
|
---|
40 | if (selection.isEmpty())
|
---|
41 | return;
|
---|
42 | int answer = t.showDialog(selection, false);
|
---|
43 | DataSet ds = selection.iterator().next().getDataSet();
|
---|
44 | boolean locked = ds != null && ds.isLocked();
|
---|
45 |
|
---|
46 | if (answer == TaggingPreset.DIALOG_ANSWER_APPLY && !locked) {
|
---|
47 | presetHandler.updateTags(t.getChangedTags());
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructs a new {@code PresetListPanel}.
|
---|
54 | */
|
---|
55 | public PresetListPanel() {
|
---|
56 | super(new GridBagLayout());
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Updates the preset list based on the {@code tags} and {@code types},
|
---|
61 | * and associates an interaction with (matching) presets via {@code presetHandler}.
|
---|
62 | * @param types collection of tagging presets types
|
---|
63 | * @param tags collection of tags
|
---|
64 | * @param presetHandler tagging preset handler
|
---|
65 | */
|
---|
66 | public void updatePresets(final Collection<TaggingPresetType> types, final Map<String, String> tags,
|
---|
67 | final TaggingPresetHandler presetHandler) {
|
---|
68 |
|
---|
69 | removeAll();
|
---|
70 | if (types.isEmpty()) {
|
---|
71 | setVisible(false);
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | for (final TaggingPreset t : TaggingPresets.getMatchingPresets(types, tags, true)) {
|
---|
76 | final JLabel lbl = new TaggingPresetLabel(t);
|
---|
77 | lbl.addMouseListener(new LabelMouseAdapter(t, presetHandler));
|
---|
78 | add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (getComponentCount() > 0) {
|
---|
82 | setVisible(true);
|
---|
83 | // This ensures the presets are exactly as high as needed.
|
---|
84 | int height = getComponentCount() * getComponent(0).getHeight();
|
---|
85 | Dimension size = new Dimension(getWidth(), height);
|
---|
86 | setMaximumSize(size);
|
---|
87 | setMinimumSize(size);
|
---|
88 | } else {
|
---|
89 | setVisible(false);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|