Changeset 8863 in josm for trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItem.java
- Timestamp:
- 2015-10-12T22:58:25+02:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/tagging/presets
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItem.java
r8862 r8863 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.tagging ;2 package org.openstreetmap.josm.gui.tagging.presets; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 import static org.openstreetmap.josm.tools.I18n.trc; 6 7 import java.io.File; 4 8 import java.util.Arrays; 5 9 import java.util.Collection; 10 import java.util.EnumSet; 11 import java.util.LinkedHashMap; 6 12 import java.util.List; 7 13 import java.util.Map; 14 import java.util.Set; 8 15 16 import javax.swing.ImageIcon; 9 17 import javax.swing.JPanel; 10 18 … … 15 23 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 16 24 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 25 import org.openstreetmap.josm.tools.ImageProvider; 26 import org.xml.sax.SAXException; 17 27 18 28 /** … … 21 31 */ 22 32 public abstract class TaggingPresetItem { 33 34 // 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) 35 private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true); 23 36 24 37 protected void initAutoCompletionField(AutoCompletingTextField field, String... key) { … … 47 60 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise. 48 61 */ 49 abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);62 protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches); 50 63 51 64 /** … … 53 66 * @param changedTags The list of changed tags to modify if needed 54 67 */ 55 abstract void addCommands(List<Tag> changedTags); 56 57 boolean requestFocusInWindow() { 58 return false; 59 } 68 protected abstract void addCommands(List<Tag> changedTags); 60 69 61 70 /** … … 65 74 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 66 75 */ 67 Boolean matches(Map<String, String> tags) {76 protected Boolean matches(Map<String, String> tags) { 68 77 return null; 69 78 } 79 80 protected static Set<TaggingPresetType> getType(String types) throws SAXException { 81 if (types == null || types.isEmpty()) { 82 throw new SAXException(tr("Unknown type: {0}", types)); 83 } 84 if (TYPE_CACHE.containsKey(types)) 85 return TYPE_CACHE.get(types); 86 Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class); 87 for (String type : Arrays.asList(types.split(","))) { 88 try { 89 TaggingPresetType presetType = TaggingPresetType.fromString(type); 90 result.add(presetType); 91 } catch (IllegalArgumentException e) { 92 throw new SAXException(tr("Unknown type: {0}", type), e); 93 } 94 } 95 TYPE_CACHE.put(types, result); 96 return result; 97 } 98 99 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 } 70 134 }
Note:
See TracChangeset
for help on using the changeset viewer.