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

Last change on this file was 18258, checked in by Don-vip, 3 years ago

see #21408 - fixups (patch by marcello)

  • Property svn:eol-style set to native
File size: 6.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.Collections;
11import java.util.EnumSet;
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.data.preferences.BooleanProperty;
24import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem;
25import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
26import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
27import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
28import org.openstreetmap.josm.gui.util.LruCache;
29import org.openstreetmap.josm.tools.ImageProvider;
30import org.openstreetmap.josm.tools.Logging;
31import org.openstreetmap.josm.tools.Utils;
32import org.xml.sax.SAXException;
33
34/**
35 * Class that represents single part of a preset - one field or text label that is shown to user
36 * @since 6068
37 */
38public abstract class TaggingPresetItem {
39
40 // cache the parsing of types using a LRU cache
41 private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LruCache<>(16);
42 /**
43 * Display OSM keys as {@linkplain org.openstreetmap.josm.gui.widgets.OsmIdTextField#setHint hint}
44 */
45 protected static final BooleanProperty DISPLAY_KEYS_AS_HINT = new BooleanProperty("taggingpreset.display-keys-as-hint", true);
46
47 protected void initAutoCompletionField(AutoCompletingTextField field, String... key) {
48 initAutoCompletionField(field, Arrays.asList(key));
49 }
50
51 protected void initAutoCompletionField(AutoCompletingTextField field, List<String> keys) {
52 DataSet data = OsmDataManager.getInstance().getEditDataSet();
53 if (data == null) {
54 return;
55 }
56 AutoCompletionList list = new AutoCompletionList();
57 AutoCompletionManager.of(data).populateWithTagValues(list, keys);
58 field.setAutoCompletionList(list);
59 }
60
61 /**
62 * Returns all cached {@link AutoCompletionItem}s for given keys.
63 *
64 * @param keys retrieve the items for these keys
65 * @return the currently cached items, sorted by priority and alphabet
66 * @since 18221
67 */
68 protected List<AutoCompletionItem> getAllForKeys(List<String> keys) {
69 DataSet data = OsmDataManager.getInstance().getEditDataSet();
70 if (data == null) {
71 return Collections.emptyList();
72 }
73 return AutoCompletionManager.of(data).getAllForKeys(keys);
74 }
75
76 /**
77 * Called by {@link TaggingPreset#createPanel} during tagging preset panel creation.
78 * All components defining this tagging preset item must be added to given panel.
79 *
80 * @param p The panel where components must be added
81 * @param support supporting class for creating the GUI
82 * @return {@code true} if this item adds semantic tagging elements, {@code false} otherwise.
83 */
84 protected abstract boolean addToPanel(JPanel p, TaggingPresetItemGuiSupport support);
85
86 /**
87 * Adds the new tags to apply to selected OSM primitives when the preset holding this item is applied.
88 * @param changedTags The list of changed tags to modify if needed
89 */
90 protected abstract void addCommands(List<Tag> changedTags);
91
92 /**
93 * Tests whether the tags match this item.
94 * Note that for a match, at least one positive and no negative is required.
95 * @param tags the tags of an {@link OsmPrimitive}
96 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
97 */
98 public Boolean matches(Map<String, String> tags) {
99 return null; // NOSONAR
100 }
101
102 protected static Set<TaggingPresetType> getType(String types) throws SAXException {
103 if (Utils.isEmpty(types)) {
104 throw new SAXException(tr("Unknown type: {0}", types));
105 }
106 if (TYPE_CACHE.containsKey(types))
107 return TYPE_CACHE.get(types);
108 Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
109 for (String type : types.split(",", -1)) {
110 try {
111 TaggingPresetType presetType = TaggingPresetType.fromString(type);
112 if (presetType != null) {
113 result.add(presetType);
114 }
115 } catch (IllegalArgumentException e) {
116 throw new SAXException(tr("Unknown type: {0}", type), e);
117 }
118 }
119 TYPE_CACHE.put(types, result);
120 return result;
121 }
122
123 protected static String fixPresetString(String s) {
124 return s == null ? s : s.replace("'", "''");
125 }
126
127 protected static String getLocaleText(String text, String textContext, String defaultText) {
128 if (text == null) {
129 return defaultText;
130 } else if (textContext != null) {
131 return trc(textContext, fixPresetString(text));
132 } else {
133 return tr(fixPresetString(text));
134 }
135 }
136
137 protected static Integer parseInteger(String str) {
138 if (Utils.isEmpty(str))
139 return null;
140 try {
141 return Integer.valueOf(str);
142 } catch (NumberFormatException e) {
143 Logging.trace(e);
144 }
145 return null;
146 }
147
148 /**
149 * Loads a tagging preset icon
150 * @param iconName the icon name
151 * @param zipIcons zip file where the image is located
152 * @param maxSize maximum image size (or null)
153 * @return the requested image or null if the request failed
154 */
155 public static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
156 final Collection<String> s = TaggingPresets.ICON_SOURCES.get();
157 ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
158 if (maxSize != null && maxSize > 0) {
159 imgProv.setMaxSize(maxSize);
160 }
161 return imgProv.get();
162 }
163
164 /**
165 * Determine whether the given preset items match the tags
166 * @param data the preset items
167 * @param tags the tags to match
168 * @return whether the given preset items match the tags
169 * @since 9932
170 */
171 public static boolean matches(Iterable<? extends TaggingPresetItem> data, Map<String, String> tags) {
172 boolean atLeastOnePositiveMatch = false;
173 for (TaggingPresetItem item : data) {
174 Boolean m = item.matches(tags);
175 if (m != null && !m)
176 return false;
177 else if (m != null) {
178 atLeastOnePositiveMatch = true;
179 }
180 }
181 return atLeastOnePositiveMatch;
182 }
183}
Note: See TracBrowser for help on using the repository browser.