001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trc;
006
007import java.io.File;
008import java.util.Arrays;
009import java.util.Collection;
010import java.util.EnumSet;
011import java.util.LinkedHashMap;
012import java.util.List;
013import java.util.Map;
014import java.util.Set;
015
016import javax.swing.ImageIcon;
017import javax.swing.JPanel;
018
019import org.openstreetmap.josm.Main;
020import org.openstreetmap.josm.data.osm.DataSet;
021import org.openstreetmap.josm.data.osm.OsmPrimitive;
022import org.openstreetmap.josm.data.osm.Tag;
023import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
024import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
025import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
026import org.openstreetmap.josm.spi.preferences.Config;
027import org.openstreetmap.josm.tools.ImageProvider;
028import org.openstreetmap.josm.tools.Logging;
029import org.xml.sax.SAXException;
030
031/**
032 * Class that represents single part of a preset - one field or text label that is shown to user
033 * @since 6068
034 */
035public abstract class TaggingPresetItem {
036
037    // cache the parsing of types using a LRU cache (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
038    private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
039
040    protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
041        initAutoCompletionField(field, Arrays.asList(key));
042    }
043
044    protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
045        if (Main.main == null) return;
046        DataSet data = Main.main.getEditDataSet();
047        if (data == null) {
048            return;
049        }
050        AutoCompletionList list = new AutoCompletionList();
051        AutoCompletionManager.of(data).populateWithTagValues(list, keys);
052        field.setAutoCompletionList(list);
053    }
054
055    /**
056     * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
057     * All components defining this tagging preset item must be added to given panel.
058     *
059     * @param p The panel where components must be added
060     * @param sel The related selected OSM primitives
061     * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying,
062     *                               i.e. whether the map feature already existed on the primitive.
063     * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
064     */
065    protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
066
067    /**
068     * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
069     * @param changedTags The list of changed tags to modify if needed
070     */
071    protected abstract void addCommands(List<Tag> changedTags);
072
073    /**
074     * Tests whether the tags match this item.
075     * Note that for a match, at least one positive and no negative is required.
076     * @param tags the tags of an {@link OsmPrimitive}
077     * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
078     */
079    protected Boolean matches(Map<String, String> tags) {
080        return null;
081    }
082
083    protected static Set<TaggingPresetType> getType(String types) throws SAXException {
084        if (types == null || types.isEmpty()) {
085            throw new SAXException(tr("Unknown type: {0}", types));
086        }
087        if (TYPE_CACHE.containsKey(types))
088            return TYPE_CACHE.get(types);
089        Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
090        for (String type : Arrays.asList(types.split(","))) {
091            try {
092                TaggingPresetType presetType = TaggingPresetType.fromString(type);
093                if (presetType != null) {
094                    result.add(presetType);
095                }
096            } catch (IllegalArgumentException e) {
097                throw new SAXException(tr("Unknown type: {0}", type), e);
098            }
099        }
100        TYPE_CACHE.put(types, result);
101        return result;
102    }
103
104    protected static String fixPresetString(String s) {
105        return s == null ? s : s.replaceAll("'", "''");
106    }
107
108    protected static String getLocaleText(String text, String textContext, String defaultText) {
109        if (text == null) {
110            return defaultText;
111        } else if (textContext != null) {
112            return trc(textContext, fixPresetString(text));
113        } else {
114            return tr(fixPresetString(text));
115        }
116    }
117
118    protected static Integer parseInteger(String str) {
119        if (str == null || str.isEmpty())
120            return null;
121        try {
122            return Integer.valueOf(str);
123        } catch (NumberFormatException e) {
124            Logging.trace(e);
125        }
126        return null;
127    }
128
129    protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
130        final Collection<String> s = Config.getPref().getList("taggingpreset.icon.sources", null);
131        ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
132        if (maxSize != null) {
133            imgProv.setMaxSize(maxSize);
134        }
135        return imgProv.get();
136    }
137
138    /**
139     * Determine whether the given preset items match the tags
140     * @param data the preset items
141     * @param tags the tags to match
142     * @return whether the given preset items match the tags
143     * @since 9932
144     */
145    public static boolean matches(Iterable<? extends TaggingPresetItem> data, Map<String, String> tags) {
146        boolean atLeastOnePositiveMatch = false;
147        for (TaggingPresetItem item : data) {
148            Boolean m = item.matches(tags);
149            if (m != null && !m)
150                return false;
151            else if (m != null) {
152                atLeastOnePositiveMatch = true;
153            }
154        }
155        return atLeastOnePositiveMatch;
156    }
157}