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 result.add(presetType); 091 } catch (IllegalArgumentException e) { 092 throw new SAXException(tr("Unknown type: {0}", type), e); 093 } 094 } 095 TYPE_CACHE.put(types, result); 096 return result; 097 } 098 099 protected static String fixPresetString(String s) { 100 return s == null ? s : s.replaceAll("'", "''"); 101 } 102 103 protected static String getLocaleText(String text, String text_context, String defaultText) { 104 if (text == null) { 105 return defaultText; 106 } else if (text_context != null) { 107 return trc(text_context, fixPresetString(text)); 108 } else { 109 return tr(fixPresetString(text)); 110 } 111 } 112 113 protected static Integer parseInteger(String str) { 114 if (str == null || str.isEmpty()) 115 return null; 116 try { 117 return Integer.valueOf(str); 118 } catch (Exception e) { 119 if (Main.isTraceEnabled()) { 120 Main.trace(e.getMessage()); 121 } 122 } 123 return null; 124 } 125 126 protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) { 127 final Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null); 128 ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true); 129 if (maxSize != null) { 130 imgProv.setMaxSize(maxSize); 131 } 132 return imgProv.get(); 133 } 134}