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.data.osm.DataSet; 020import org.openstreetmap.josm.data.osm.OsmDataManager; 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 DataSet data = OsmDataManager.getInstance().getEditDataSet(); 046 if (data == null) { 047 return; 048 } 049 AutoCompletionList list = new AutoCompletionList(); 050 AutoCompletionManager.of(data).populateWithTagValues(list, keys); 051 field.setAutoCompletionList(list); 052 } 053 054 /** 055 * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation. 056 * All components defining this tagging preset item must be added to given panel. 057 * 058 * @param p The panel where components must be added 059 * @param sel The related selected OSM primitives 060 * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying, 061 * i.e. whether the map feature already existed on the primitive. 062 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise. 063 */ 064 protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches); 065 066 /** 067 * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied. 068 * @param changedTags The list of changed tags to modify if needed 069 */ 070 protected abstract void addCommands(List<Tag> changedTags); 071 072 /** 073 * Tests whether the tags match this item. 074 * Note that for a match, at least one positive and no negative is required. 075 * @param tags the tags of an {@link OsmPrimitive} 076 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 077 */ 078 protected Boolean matches(Map<String, String> tags) { 079 return null; 080 } 081 082 protected static Set<TaggingPresetType> getType(String types) throws SAXException { 083 if (types == null || types.isEmpty()) { 084 throw new SAXException(tr("Unknown type: {0}", types)); 085 } 086 if (TYPE_CACHE.containsKey(types)) 087 return TYPE_CACHE.get(types); 088 Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class); 089 for (String type : Arrays.asList(types.split(","))) { 090 try { 091 TaggingPresetType presetType = TaggingPresetType.fromString(type); 092 if (presetType != null) { 093 result.add(presetType); 094 } 095 } catch (IllegalArgumentException e) { 096 throw new SAXException(tr("Unknown type: {0}", type), e); 097 } 098 } 099 TYPE_CACHE.put(types, result); 100 return result; 101 } 102 103 protected static String fixPresetString(String s) { 104 return s == null ? s : s.replace("'", "''"); 105 } 106 107 protected static String getLocaleText(String text, String textContext, String defaultText) { 108 if (text == null) { 109 return defaultText; 110 } else if (textContext != null) { 111 return trc(textContext, fixPresetString(text)); 112 } else { 113 return tr(fixPresetString(text)); 114 } 115 } 116 117 protected static Integer parseInteger(String str) { 118 if (str == null || str.isEmpty()) 119 return null; 120 try { 121 return Integer.valueOf(str); 122 } catch (NumberFormatException e) { 123 Logging.trace(e); 124 } 125 return null; 126 } 127 128 protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) { 129 final Collection<String> s = Config.getPref().getList("taggingpreset.icon.sources", null); 130 ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true); 131 if (maxSize != null) { 132 imgProv.setMaxSize(maxSize); 133 } 134 return imgProv.get(); 135 } 136 137 /** 138 * Determine whether the given preset items match the tags 139 * @param data the preset items 140 * @param tags the tags to match 141 * @return whether the given preset items match the tags 142 * @since 9932 143 */ 144 public static boolean matches(Iterable<? extends TaggingPresetItem> data, Map<String, String> tags) { 145 boolean atLeastOnePositiveMatch = false; 146 for (TaggingPresetItem item : data) { 147 Boolean m = item.matches(tags); 148 if (m != null && !m) 149 return false; 150 else if (m != null) { 151 atLeastOnePositiveMatch = true; 152 } 153 } 154 return atLeastOnePositiveMatch; 155 } 156}