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

Last change on this file since 9932 was 9932, checked in by simon04, 8 years ago

see #8460 - fix typo from r9931, fix matching, improve conditions, add unit test

  • Property svn:eol-style set to native
File size: 5.9 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.Main;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Tag;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
24import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
25import org.openstreetmap.josm.tools.ImageProvider;
26import org.xml.sax.SAXException;
27
28/**
29 * Class that represents single part of a preset - one field or text label that is shown to user
30 * @since 6068
31 */
32public 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);
36
37 protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
38 initAutoCompletionField(field, Arrays.asList(key));
39 }
40
41 protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
42 if (Main.main == null) return;
43 OsmDataLayer layer = Main.main.getEditLayer();
44 if (layer == null) {
45 return;
46 }
47 AutoCompletionList list = new AutoCompletionList();
48 layer.data.getAutoCompletionManager().populateWithTagValues(list, keys);
49 field.setAutoCompletionList(list);
50 }
51
52 /**
53 * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
54 * All components defining this tagging preset item must be added to given panel.
55 *
56 * @param p The panel where components must be added
57 * @param sel The related selected OSM primitives
58 * @param presetInitiallyMatches Whether this {@link TaggingPreset} already matched before applying,
59 * i.e. whether the map feature already existed on the primitive.
60 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
61 */
62 protected abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches);
63
64 /**
65 * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
66 * @param changedTags The list of changed tags to modify if needed
67 */
68 protected abstract void addCommands(List<Tag> changedTags);
69
70 /**
71 * Tests whether the tags match this item.
72 * Note that for a match, at least one positive and no negative is required.
73 * @param tags the tags of an {@link OsmPrimitive}
74 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
75 */
76 protected Boolean matches(Map<String, String> tags) {
77 return null;
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 if (presetType != null) {
91 result.add(presetType);
92 }
93 } catch (IllegalArgumentException e) {
94 throw new SAXException(tr("Unknown type: {0}", type), e);
95 }
96 }
97 TYPE_CACHE.put(types, result);
98 return result;
99 }
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 text_context, String defaultText) {
106 if (text == null) {
107 return defaultText;
108 } else if (text_context != null) {
109 return trc(text_context, 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 (Exception 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 * @sice 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.