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