001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.properties; 003 004import java.awt.Dimension; 005import java.awt.GridBagLayout; 006import java.awt.event.MouseAdapter; 007import java.awt.event.MouseEvent; 008import java.util.Collection; 009import java.util.Map; 010 011import javax.swing.JLabel; 012import javax.swing.JPanel; 013 014import org.openstreetmap.josm.data.osm.OsmPrimitive; 015import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; 016import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler; 017import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetLabel; 018import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType; 019import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets; 020import org.openstreetmap.josm.tools.GBC; 021 022/** 023 * A list of matching presets for a set of tags. 024 */ 025public class PresetListPanel extends JPanel { 026 027 /** 028 * Constructs a new {@code PresetListPanel}. 029 */ 030 public PresetListPanel() { 031 super(new GridBagLayout()); 032 } 033 034 /** 035 * Updates the preset list based on the {@code tags} and {@code types}, 036 * and associates an interaction with (matching) presets via {@code presetHandler}. 037 * @param types collection of tagging presets types 038 * @param tags collection of tags 039 * @param presetHandler tagging preset handler 040 */ 041 public void updatePresets(final Collection<TaggingPresetType> types, final Map<String, String> tags, 042 final TaggingPresetHandler presetHandler) { 043 044 removeAll(); 045 if (types.isEmpty()) { 046 setVisible(false); 047 return; 048 } 049 050 for (final TaggingPreset t : TaggingPresets.getMatchingPresets(types, tags, true)) { 051 final JLabel lbl = new TaggingPresetLabel(t); 052 lbl.addMouseListener(new MouseAdapter() { 053 @Override 054 public void mouseClicked(MouseEvent e) { 055 Collection<OsmPrimitive> selection = t.createSelection(presetHandler.getSelection()); 056 if (selection == null || selection.isEmpty()) 057 return; 058 int answer = t.showDialog(selection, false); 059 060 if (answer == TaggingPreset.DIALOG_ANSWER_APPLY) { 061 presetHandler.updateTags(t.getChangedTags()); 062 } 063 } 064 }); 065 add(lbl, GBC.eol().fill(GBC.HORIZONTAL)); 066 } 067 068 if (getComponentCount() > 0) { 069 setVisible(true); 070 // This ensures the presets are exactly as high as needed. 071 int height = getComponentCount() * getComponent(0).getHeight(); 072 Dimension size = new Dimension(getWidth(), height); 073 setMaximumSize(size); 074 setMinimumSize(size); 075 } else { 076 setVisible(false); 077 } 078 } 079}