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.OsmPrimitive; 021import org.openstreetmap.josm.data.osm.Tag; 022import org.openstreetmap.josm.gui.layer.OsmDataLayer; 023import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 024import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 025import org.openstreetmap.josm.tools.ImageProvider; 026import org.xml.sax.SAXException; 027 028/** 029 * Class that represents single part of a preset - one field or text label that is shown to user 030 * @since 6068 031 */ 032public abstract class TaggingPresetItem { 033 034 // 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) 035 private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true); 036 037 protected void initAutoCompletionField(AutoCompletingTextField field, String... key) { 038 initAutoCompletionField(field, Arrays.asList(key)); 039 } 040 041 protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) { 042 if (Main.main == null) return; 043 OsmDataLayer layer = Main.main.getEditLayer(); 044 if (layer == null) { 045 return; 046 } 047 AutoCompletionList list = new AutoCompletionList(); 048 layer.data.getAutoCompletionManager().populateWithTagValues(list, keys); 049 field.setAutoCompletionList(list); 050 } 051 052 /** 053 * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation. 054 * All components defining this tagging preset item must be added to given panel. 055 * 056 * @param p The panel where components must be added 057 * @param sel The related selected OSM primitives 058 * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying, 059 * i.e. whether the map feature already existed on the primitive. 060 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise. 061 */ 062 protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches); 063 064 /** 065 * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied. 066 * @param changedTags The list of changed tags to modify if needed 067 */ 068 protected abstract void addCommands(List<Tag> changedTags); 069 070 /** 071 * Tests whether the tags match this item. 072 * Note that for a match, at least one positive and no negative is required. 073 * @param tags the tags of an {@link OsmPrimitive} 074 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 075 */ 076 protected Boolean matches(Map<String, String> tags) { 077 return null; 078 } 079 080 protected static Set<TaggingPresetType> getType(String types) throws SAXException { 081 if (types == null || types.isEmpty()) { 082 throw new SAXException(tr("Unknown type: {0}", types)); 083 } 084 if (TYPE_CACHE.containsKey(types)) 085 return TYPE_CACHE.get(types); 086 Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class); 087 for (String type : Arrays.asList(types.split(","))) { 088 try { 089 TaggingPresetType presetType = TaggingPresetType.fromString(type); 090 if (presetType != null) { 091 result.add(presetType); 092 } 093 } catch (IllegalArgumentException e) { 094 throw new SAXException(tr("Unknown type: {0}", type), e); 095 } 096 } 097 TYPE_CACHE.put(types, result); 098 return result; 099 } 100 101 protected static String fixPresetString(String s) { 102 return s == null ? s : s.replaceAll("'", "''"); 103 } 104 105 protected static String getLocaleText(String text, String textContext, String defaultText) { 106 if (text == null) { 107 return defaultText; 108 } else if (textContext != null) { 109 return trc(textContext, fixPresetString(text)); 110 } else { 111 return tr(fixPresetString(text)); 112 } 113 } 114 115 protected static Integer parseInteger(String str) { 116 if (str == null || str.isEmpty()) 117 return null; 118 try { 119 return Integer.valueOf(str); 120 } catch (NumberFormatException e) { 121 if (Main.isTraceEnabled()) { 122 Main.trace(e.getMessage()); 123 } 124 } 125 return null; 126 } 127 128 protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) { 129 final Collection<String> s = Main.pref.getCollection("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}