source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItem.java@ 15667

Last change on this file since 15667 was 15667, checked in by Don-vip, 4 years ago

fix #18455 - smarter error detection, should lead to less false positives

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.tagging.presets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trc;
6
7import java.io.File;
8import java.util.Arrays;
9import java.util.Collection;
10import java.util.EnumSet;
11import java.util.LinkedHashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.Set;
15
16import javax.swing.ImageIcon;
17import javax.swing.JPanel;
18
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.OsmDataManager;
21import org.openstreetmap.josm.data.osm.OsmPrimitive;
22import org.openstreetmap.josm.data.osm.Tag;
23import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
24import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
25import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
26import org.openstreetmap.josm.spi.preferences.Config;
27import org.openstreetmap.josm.tools.ImageProvider;
28import org.openstreetmap.josm.tools.Logging;
29import org.xml.sax.SAXException;
30
31/**
32 * Class that represents single part of a preset - one field or text label that is shown to user
33 * @since 6068
34 */
35public abstract class TaggingPresetItem {
36
37 // 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)
38 private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
39
40 protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
41 initAutoCompletionField(field, Arrays.asList(key));
42 }
43
44 protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
45 DataSet data = OsmDataManager.getInstance().getEditDataSet();
46 if (data == null) {
47 return;
48 }
49 AutoCompletionList list = new AutoCompletionList();
50 AutoCompletionManager.of(data).populateWithTagValues(list, keys);
51 field.setAutoCompletionList(list);
52 }
53
54 /**
55 * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
56 * All components defining this tagging preset item must be added to given panel.
57 *
58 * @param p The panel where components must be added
59 * @param sel The related selected OSM primitives
60 * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying,
61 * i.e. whether the map feature already existed on the primitive.
62 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
63 */
64 protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
65
66 /**
67 * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
68 * @param changedTags The list of changed tags to modify if needed
69 */
70 protected abstract void addCommands(List<Tag> changedTags);
71
72 /**
73 * Tests whether the tags match this item.
74 * Note that for a match, at least one positive and no negative is required.
75 * @param tags the tags of an {@link OsmPrimitive}
76 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
77 */
78 public Boolean matches(Map<String, String> tags) {
79 return null;
80 }
81
82 protected static Set<TaggingPresetType> getType(String types) throws SAXException {
83 if (types == null || types.isEmpty()) {
84 throw new SAXException(tr("Unknown type: {0}", types));
85 }
86 if (TYPE_CACHE.containsKey(types))
87 return TYPE_CACHE.get(types);
88 Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
89 for (String type : Arrays.asList(types.split(","))) {
90 try {
91 TaggingPresetType presetType = TaggingPresetType.fromString(type);
92 if (presetType != null) {
93 result.add(presetType);
94 }
95 } catch (IllegalArgumentException e) {
96 throw new SAXException(tr("Unknown type: {0}", type), e);
97 }
98 }
99 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}
Note: See TracBrowser for help on using the repository browser.