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