001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging;
003
004import java.util.Collection;
005import java.util.List;
006import java.util.Map;
007
008import javax.swing.JPanel;
009
010import org.openstreetmap.josm.Main;
011import org.openstreetmap.josm.data.osm.OsmPrimitive;
012import org.openstreetmap.josm.data.osm.Tag;
013import org.openstreetmap.josm.gui.layer.OsmDataLayer;
014import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
015import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
016
017/**
018 * Class that represents single part of a preset - one field or text label that is shown to user
019 * @since 6068
020 */
021public abstract class TaggingPresetItem {
022
023    protected void initAutoCompletionField(AutoCompletingTextField field, String key) {
024        if (Main.main == null) return;
025        OsmDataLayer layer = Main.main.getEditLayer();
026        if (layer == null) {
027            return;
028        }
029        AutoCompletionList list = new AutoCompletionList();
030        layer.data.getAutoCompletionManager().populateWithTagValues(list, key);
031        field.setAutoCompletionList(list);
032    }
033
034    /**
035     * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
036     * All components defining this tagging preset item must be added to given panel.
037     * @param p The panel where components must be added
038     * @param sel The related selected OSM primitives
039     * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
040     */
041    abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel);
042
043    /**
044     * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
045     * @param changedTags The list of changed tags to modify if needed
046     */
047    abstract void addCommands(List<Tag> changedTags);
048
049    boolean requestFocusInWindow() {
050        return false;
051    }
052
053    /**
054     * Tests whether the tags match this item.
055     * Note that for a match, at least one positive and no negative is required.
056     * @param tags the tags of an {@link OsmPrimitive}
057     * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
058     */
059    Boolean matches(Map<String, String> tags) {
060        return null;
061    }
062}