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

Last change on this file since 7128 was 7128, checked in by simon04, 10 years ago

Fix Sonar issue (package cycle)

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import org.openstreetmap.josm.data.osm.OsmPrimitive;
5import org.openstreetmap.josm.gui.tagging.PresetHandler;
6import org.openstreetmap.josm.gui.tagging.TaggingPreset;
7import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
8import org.openstreetmap.josm.gui.tagging.PresetLabel;
9import org.openstreetmap.josm.tools.GBC;
10
11import javax.swing.JLabel;
12import javax.swing.JPanel;
13import java.awt.Dimension;
14import java.awt.GridBagLayout;
15import java.awt.event.MouseAdapter;
16import java.awt.event.MouseEvent;
17import java.util.Collection;
18import java.util.Map;
19
20/**
21 * A list of matching presets for a set of tags.
22 */
23public 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}
Note: See TracBrowser for help on using the repository browser.