1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Dimension;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GridBagLayout;
|
---|
11 | import java.awt.GridLayout;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ActionListener;
|
---|
15 | import java.awt.event.MouseAdapter;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.io.File;
|
---|
18 | import java.lang.reflect.Method;
|
---|
19 | import java.lang.reflect.Modifier;
|
---|
20 | import java.text.NumberFormat;
|
---|
21 | import java.text.ParseException;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Arrays;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.Collections;
|
---|
26 | import java.util.EnumSet;
|
---|
27 | import java.util.HashMap;
|
---|
28 | import java.util.LinkedHashMap;
|
---|
29 | import java.util.LinkedList;
|
---|
30 | import java.util.List;
|
---|
31 | import java.util.Map;
|
---|
32 | import java.util.Map.Entry;
|
---|
33 | import java.util.Set;
|
---|
34 | import java.util.SortedSet;
|
---|
35 | import java.util.TreeSet;
|
---|
36 |
|
---|
37 | import javax.swing.AbstractButton;
|
---|
38 | import javax.swing.BorderFactory;
|
---|
39 | import javax.swing.ButtonGroup;
|
---|
40 | import javax.swing.Icon;
|
---|
41 | import javax.swing.ImageIcon;
|
---|
42 | import javax.swing.JButton;
|
---|
43 | import javax.swing.JComponent;
|
---|
44 | import javax.swing.JLabel;
|
---|
45 | import javax.swing.JList;
|
---|
46 | import javax.swing.JPanel;
|
---|
47 | import javax.swing.JScrollPane;
|
---|
48 | import javax.swing.JSeparator;
|
---|
49 | import javax.swing.JToggleButton;
|
---|
50 | import javax.swing.ListCellRenderer;
|
---|
51 | import javax.swing.ListModel;
|
---|
52 |
|
---|
53 | import org.openstreetmap.josm.Main;
|
---|
54 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
55 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
56 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
57 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
58 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
59 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
60 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
61 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority;
|
---|
62 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
63 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
64 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
65 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
66 | import org.openstreetmap.josm.gui.widgets.QuadStateCheckBox;
|
---|
67 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
68 | import org.openstreetmap.josm.tools.AlphanumComparator;
|
---|
69 | import org.openstreetmap.josm.tools.GBC;
|
---|
70 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
71 | import org.openstreetmap.josm.tools.Predicate;
|
---|
72 | import org.openstreetmap.josm.tools.Utils;
|
---|
73 | import org.xml.sax.SAXException;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Class that contains all subtypes of TaggingPresetItem, static supplementary data, types and methods
|
---|
77 | * @since 6068
|
---|
78 | */
|
---|
79 | public final class TaggingPresetItems {
|
---|
80 | private TaggingPresetItems() {
|
---|
81 | }
|
---|
82 |
|
---|
83 | private static int auto_increment_selected;
|
---|
84 | /** Translatation of "<different>". Use in combo boxes to display en entry matching several different values. */
|
---|
85 | public static final String DIFFERENT = tr("<different>");
|
---|
86 |
|
---|
87 | private static final BooleanProperty PROP_FILL_DEFAULT = new BooleanProperty("taggingpreset.fill-default-for-tagged-primitives", false);
|
---|
88 |
|
---|
89 | // 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)
|
---|
90 | private static final Map<String, Set<TaggingPresetType>> TYPE_CACHE = new LinkedHashMap<>(16, 1.1f, true);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Last value of each key used in presets, used for prefilling corresponding fields
|
---|
94 | */
|
---|
95 | private static final Map<String, String> LAST_VALUES = new HashMap<>();
|
---|
96 |
|
---|
97 | public static class PresetListEntry implements Comparable<PresetListEntry> {
|
---|
98 | public String value;
|
---|
99 | /** The context used for translating {@link #value} */
|
---|
100 | public String value_context;
|
---|
101 | public String display_value;
|
---|
102 | public String short_description;
|
---|
103 | /** The location of icon file to display */
|
---|
104 | public String icon;
|
---|
105 | /** The size of displayed icon. If not set, default is size from icon file */
|
---|
106 | public String icon_size;
|
---|
107 | /** The localized version of {@link #display_value}. */
|
---|
108 | public String locale_display_value;
|
---|
109 | /** The localized version of {@link #short_description}. */
|
---|
110 | public String locale_short_description;
|
---|
111 | private final File zipIcons = TaggingPresetReader.getZipIcons();
|
---|
112 |
|
---|
113 | // Cached size (currently only for Combo) to speed up preset dialog initialization
|
---|
114 | private int prefferedWidth = -1;
|
---|
115 | private int prefferedHeight = -1;
|
---|
116 |
|
---|
117 | public String getListDisplay() {
|
---|
118 | if (value.equals(DIFFERENT))
|
---|
119 | return "<b>"+DIFFERENT.replaceAll("<", "<").replaceAll(">", ">")+"</b>";
|
---|
120 |
|
---|
121 | if (value.isEmpty())
|
---|
122 | return " ";
|
---|
123 |
|
---|
124 | final StringBuilder res = new StringBuilder("<b>");
|
---|
125 | res.append(getDisplayValue(true).replaceAll("<", "<").replaceAll(">", ">"))
|
---|
126 | .append("</b>");
|
---|
127 | if (getShortDescription(true) != null) {
|
---|
128 | // wrap in table to restrict the text width
|
---|
129 | res.append("<div style=\"width:300px; padding:0 0 5px 5px\">")
|
---|
130 | .append(getShortDescription(true))
|
---|
131 | .append("</div>");
|
---|
132 | }
|
---|
133 | return res.toString();
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Returns the entry icon, if any.
|
---|
138 | * @return the entry icon, or {@code null}
|
---|
139 | */
|
---|
140 | public ImageIcon getIcon() {
|
---|
141 | return icon == null ? null : loadImageIcon(icon, zipIcons, parseInteger(icon_size));
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Constructs a new {@code PresetListEntry}, uninitialized.
|
---|
146 | */
|
---|
147 | public PresetListEntry() {
|
---|
148 | }
|
---|
149 |
|
---|
150 | public PresetListEntry(String value) {
|
---|
151 | this.value = value;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public String getDisplayValue(boolean translated) {
|
---|
155 | return translated
|
---|
156 | ? Utils.firstNonNull(locale_display_value, tr(display_value), trc(value_context, value))
|
---|
157 | : Utils.firstNonNull(display_value, value);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public String getShortDescription(boolean translated) {
|
---|
161 | return translated
|
---|
162 | ? Utils.firstNonNull(locale_short_description, tr(short_description))
|
---|
163 | : short_description;
|
---|
164 | }
|
---|
165 |
|
---|
166 | // toString is mainly used to initialize the Editor
|
---|
167 | @Override
|
---|
168 | public String toString() {
|
---|
169 | if (value.equals(DIFFERENT))
|
---|
170 | return DIFFERENT;
|
---|
171 | return getDisplayValue(true).replaceAll("<.*>", ""); // remove additional markup, e.g. <br>
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public int compareTo(PresetListEntry o) {
|
---|
176 | return AlphanumComparator.getInstance().compare(this.getDisplayValue(true), o.getDisplayValue(true));
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | public static class Role {
|
---|
181 | public Set<TaggingPresetType> types;
|
---|
182 | public String key;
|
---|
183 | /** The text to display */
|
---|
184 | public String text;
|
---|
185 | /** The context used for translating {@link #text} */
|
---|
186 | public String text_context;
|
---|
187 | /** The localized version of {@link #text}. */
|
---|
188 | public String locale_text;
|
---|
189 | public SearchCompiler.Match memberExpression;
|
---|
190 |
|
---|
191 | public boolean required;
|
---|
192 | private long count;
|
---|
193 |
|
---|
194 | public void setType(String types) throws SAXException {
|
---|
195 | this.types = getType(types);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void setRequisite(String str) throws SAXException {
|
---|
199 | if ("required".equals(str)) {
|
---|
200 | required = true;
|
---|
201 | } else if (!"optional".equals(str))
|
---|
202 | throw new SAXException(tr("Unknown requisite: {0}", str));
|
---|
203 | }
|
---|
204 |
|
---|
205 | public void setMember_expression(String member_expression) throws SAXException {
|
---|
206 | try {
|
---|
207 | final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
|
---|
208 | searchSetting.text = member_expression;
|
---|
209 | searchSetting.caseSensitive = true;
|
---|
210 | searchSetting.regexSearch = true;
|
---|
211 | this.memberExpression = SearchCompiler.compile(searchSetting);
|
---|
212 | } catch (SearchCompiler.ParseError ex) {
|
---|
213 | throw new SAXException(tr("Illegal member expression: {0}", ex.getMessage()), ex);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | public void setCount(String count) {
|
---|
218 | this.count = Long.parseLong(count);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Return either argument, the highest possible value or the lowest allowed value
|
---|
223 | */
|
---|
224 | public long getValidCount(long c) {
|
---|
225 | if (count > 0 && !required)
|
---|
226 | return c != 0 ? count : 0;
|
---|
227 | else if (count > 0)
|
---|
228 | return count;
|
---|
229 | else if (!required)
|
---|
230 | return c != 0 ? c : 0;
|
---|
231 | else
|
---|
232 | return c != 0 ? c : 1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | public boolean addToPanel(JPanel p) {
|
---|
236 | String cstring;
|
---|
237 | if (count > 0 && !required) {
|
---|
238 | cstring = "0,"+count;
|
---|
239 | } else if (count > 0) {
|
---|
240 | cstring = String.valueOf(count);
|
---|
241 | } else if (!required) {
|
---|
242 | cstring = "0-...";
|
---|
243 | } else {
|
---|
244 | cstring = "1-...";
|
---|
245 | }
|
---|
246 | if (locale_text == null) {
|
---|
247 | locale_text = getLocaleText(text, text_context, null);
|
---|
248 | }
|
---|
249 | p.add(new JLabel(locale_text+':'), GBC.std().insets(0, 0, 10, 0));
|
---|
250 | p.add(new JLabel(key), GBC.std().insets(0, 0, 10, 0));
|
---|
251 | p.add(new JLabel(cstring), types == null ? GBC.eol() : GBC.std().insets(0, 0, 10, 0));
|
---|
252 | if (types != null) {
|
---|
253 | JPanel pp = new JPanel();
|
---|
254 | for (TaggingPresetType t : types) {
|
---|
255 | pp.add(new JLabel(ImageProvider.get(t.getIconName())));
|
---|
256 | }
|
---|
257 | p.add(pp, GBC.eol());
|
---|
258 | }
|
---|
259 | return true;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Enum denoting how a match (see {@link TaggingPresetItem#matches}) is performed.
|
---|
265 | */
|
---|
266 | public enum MatchType {
|
---|
267 |
|
---|
268 | /** Neutral, i.e., do not consider this item for matching. */
|
---|
269 | NONE("none"),
|
---|
270 | /** Positive if key matches, neutral otherwise. */
|
---|
271 | KEY("key"),
|
---|
272 | /** Positive if key matches, negative otherwise. */
|
---|
273 | KEY_REQUIRED("key!"),
|
---|
274 | /** Positive if key and value matches, neutral otherwise. */
|
---|
275 | KEY_VALUE("keyvalue"),
|
---|
276 | /** Positive if key and value matches, negative otherwise. */
|
---|
277 | KEY_VALUE_REQUIRED("keyvalue!");
|
---|
278 |
|
---|
279 | private final String value;
|
---|
280 |
|
---|
281 | MatchType(String value) {
|
---|
282 | this.value = value;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Replies the associated textual value.
|
---|
287 | * @return the associated textual value
|
---|
288 | */
|
---|
289 | public String getValue() {
|
---|
290 | return value;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Determines the {@code MatchType} for the given textual value.
|
---|
295 | * @param type the textual value
|
---|
296 | * @return the {@code MatchType} for the given textual value
|
---|
297 | */
|
---|
298 | public static MatchType ofString(String type) {
|
---|
299 | for (MatchType i : EnumSet.allOf(MatchType.class)) {
|
---|
300 | if (i.getValue().equals(type))
|
---|
301 | return i;
|
---|
302 | }
|
---|
303 | throw new IllegalArgumentException(type + " is not allowed");
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | public static class Usage {
|
---|
308 | private SortedSet<String> values;
|
---|
309 | private boolean hadKeys;
|
---|
310 | private boolean hadEmpty;
|
---|
311 |
|
---|
312 | public boolean hasUniqueValue() {
|
---|
313 | return values.size() == 1 && !hadEmpty;
|
---|
314 | }
|
---|
315 |
|
---|
316 | public boolean unused() {
|
---|
317 | return values.isEmpty();
|
---|
318 | }
|
---|
319 |
|
---|
320 | public String getFirst() {
|
---|
321 | return values.first();
|
---|
322 | }
|
---|
323 |
|
---|
324 | public boolean hadKeys() {
|
---|
325 | return hadKeys;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * A tagging preset item displaying a localizable text.
|
---|
331 | * @since 6190
|
---|
332 | */
|
---|
333 | public abstract static class TaggingPresetTextItem extends TaggingPresetItem {
|
---|
334 |
|
---|
335 | /** The text to display */
|
---|
336 | public String text;
|
---|
337 |
|
---|
338 | /** The context used for translating {@link #text} */
|
---|
339 | public String text_context;
|
---|
340 |
|
---|
341 | /** The localized version of {@link #text} */
|
---|
342 | public String locale_text;
|
---|
343 |
|
---|
344 | protected final void initializeLocaleText(String defaultText) {
|
---|
345 | if (locale_text == null) {
|
---|
346 | locale_text = getLocaleText(text, text_context, defaultText);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | @Override
|
---|
351 | void addCommands(List<Tag> changedTags) {
|
---|
352 | }
|
---|
353 |
|
---|
354 | protected String fieldsToString() {
|
---|
355 | return (text != null ? "text=" + text + ", " : "")
|
---|
356 | + (text_context != null ? "text_context=" + text_context + ", " : "")
|
---|
357 | + (locale_text != null ? "locale_text=" + locale_text : "");
|
---|
358 | }
|
---|
359 |
|
---|
360 | @Override
|
---|
361 | public String toString() {
|
---|
362 | return getClass().getSimpleName() + " [" + fieldsToString() + ']';
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Label type.
|
---|
368 | */
|
---|
369 | public static class Label extends TaggingPresetTextItem {
|
---|
370 |
|
---|
371 | /** The location of icon file to display (optional) */
|
---|
372 | public String icon;
|
---|
373 | /** The size of displayed icon. If not set, default is 16px */
|
---|
374 | public String icon_size;
|
---|
375 |
|
---|
376 | @Override
|
---|
377 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
378 | initializeLocaleText(null);
|
---|
379 | addLabel(p, getIcon(), locale_text);
|
---|
380 | return true;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Adds a new {@code JLabel} to the given panel.
|
---|
385 | * @param p The panel
|
---|
386 | * @param icon the icon (optional, can be null)
|
---|
387 | * @param label The text label
|
---|
388 | */
|
---|
389 | public static void addLabel(JPanel p, Icon icon, String label) {
|
---|
390 | p.add(new JLabel(label, icon, JLabel.LEADING), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Returns the label icon, if any.
|
---|
395 | * @return the label icon, or {@code null}
|
---|
396 | */
|
---|
397 | public ImageIcon getIcon() {
|
---|
398 | Integer size = parseInteger(icon_size);
|
---|
399 | return icon == null ? null : loadImageIcon(icon, TaggingPresetReader.getZipIcons(), size != null ? size : 16);
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Hyperlink type.
|
---|
405 | */
|
---|
406 | public static class Link extends TaggingPresetTextItem {
|
---|
407 |
|
---|
408 | /** The link to display. */
|
---|
409 | public String href;
|
---|
410 |
|
---|
411 | /** The localized version of {@link #href}. */
|
---|
412 | public String locale_href;
|
---|
413 |
|
---|
414 | @Override
|
---|
415 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
416 | initializeLocaleText(tr("More information about this feature"));
|
---|
417 | String url = locale_href;
|
---|
418 | if (url == null) {
|
---|
419 | url = href;
|
---|
420 | }
|
---|
421 | if (url != null) {
|
---|
422 | p.add(new UrlLabel(url, locale_text, 2), GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL));
|
---|
423 | }
|
---|
424 | return false;
|
---|
425 | }
|
---|
426 |
|
---|
427 | @Override
|
---|
428 | protected String fieldsToString() {
|
---|
429 | return super.fieldsToString()
|
---|
430 | + (href != null ? "href=" + href + ", " : "")
|
---|
431 | + (locale_href != null ? "locale_href=" + locale_href + ", " : "");
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | public static class PresetLink extends TaggingPresetItem {
|
---|
436 |
|
---|
437 | public String preset_name = "";
|
---|
438 |
|
---|
439 | @Override
|
---|
440 | boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
441 | final String presetName = preset_name;
|
---|
442 | final TaggingPreset t = Utils.filter(TaggingPresets.getTaggingPresets(), new Predicate<TaggingPreset>() {
|
---|
443 | @Override
|
---|
444 | public boolean evaluate(TaggingPreset object) {
|
---|
445 | return presetName.equals(object.name);
|
---|
446 | }
|
---|
447 | }).iterator().next();
|
---|
448 | if (t == null) return false;
|
---|
449 | JLabel lbl = new PresetLabel(t);
|
---|
450 | lbl.addMouseListener(new MouseAdapter() {
|
---|
451 | @Override
|
---|
452 | public void mouseClicked(MouseEvent arg0) {
|
---|
453 | t.actionPerformed(null);
|
---|
454 | }
|
---|
455 | });
|
---|
456 | p.add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
457 | return false;
|
---|
458 | }
|
---|
459 |
|
---|
460 | @Override
|
---|
461 | void addCommands(List<Tag> changedTags) {
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | public static class Roles extends TaggingPresetItem {
|
---|
466 |
|
---|
467 | public final List<Role> roles = new LinkedList<>();
|
---|
468 |
|
---|
469 | @Override
|
---|
470 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
471 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
472 | if (!roles.isEmpty()) {
|
---|
473 | JPanel proles = new JPanel(new GridBagLayout());
|
---|
474 | proles.add(new JLabel(tr("Available roles")), GBC.std().insets(0, 0, 10, 0));
|
---|
475 | proles.add(new JLabel(tr("role")), GBC.std().insets(0, 0, 10, 0));
|
---|
476 | proles.add(new JLabel(tr("count")), GBC.std().insets(0, 0, 10, 0));
|
---|
477 | proles.add(new JLabel(tr("elements")), GBC.eol());
|
---|
478 | for (Role i : roles) {
|
---|
479 | i.addToPanel(proles);
|
---|
480 | }
|
---|
481 | p.add(proles, GBC.eol());
|
---|
482 | }
|
---|
483 | return false;
|
---|
484 | }
|
---|
485 |
|
---|
486 | @Override
|
---|
487 | public void addCommands(List<Tag> changedTags) {
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | public static class Optional extends TaggingPresetTextItem {
|
---|
492 |
|
---|
493 | // TODO: Draw a box around optional stuff
|
---|
494 | @Override
|
---|
495 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
496 | initializeLocaleText(tr("Optional Attributes:"));
|
---|
497 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
498 | p.add(new JLabel(locale_text), GBC.eol());
|
---|
499 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
500 | return false;
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * Horizontal separator type.
|
---|
506 | */
|
---|
507 | public static class Space extends TaggingPresetItem {
|
---|
508 |
|
---|
509 | @Override
|
---|
510 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
511 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
512 | return false;
|
---|
513 | }
|
---|
514 |
|
---|
515 | @Override
|
---|
516 | public void addCommands(List<Tag> changedTags) {
|
---|
517 | }
|
---|
518 |
|
---|
519 | @Override
|
---|
520 | public String toString() {
|
---|
521 | return "Space";
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Class used to represent a {@link JSeparator} inside tagging preset window.
|
---|
527 | * @since 6198
|
---|
528 | */
|
---|
529 | public static class ItemSeparator extends TaggingPresetItem {
|
---|
530 |
|
---|
531 | @Override
|
---|
532 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
533 | p.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
534 | return false;
|
---|
535 | }
|
---|
536 |
|
---|
537 | @Override
|
---|
538 | public void addCommands(List<Tag> changedTags) {
|
---|
539 | }
|
---|
540 |
|
---|
541 | @Override
|
---|
542 | public String toString() {
|
---|
543 | return "ItemSeparator";
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Preset item associated to an OSM key.
|
---|
549 | */
|
---|
550 | public abstract static class KeyedItem extends TaggingPresetItem {
|
---|
551 |
|
---|
552 | public String key;
|
---|
553 | /** The text to display */
|
---|
554 | public String text;
|
---|
555 | /** The context used for translating {@link #text} */
|
---|
556 | public String text_context;
|
---|
557 | public String match = getDefaultMatch().getValue();
|
---|
558 |
|
---|
559 | public abstract MatchType getDefaultMatch();
|
---|
560 |
|
---|
561 | public abstract Collection<String> getValues();
|
---|
562 |
|
---|
563 | @Override
|
---|
564 | Boolean matches(Map<String, String> tags) {
|
---|
565 | switch (MatchType.ofString(match)) {
|
---|
566 | case NONE:
|
---|
567 | return null;
|
---|
568 | case KEY:
|
---|
569 | return tags.containsKey(key) ? Boolean.TRUE : null;
|
---|
570 | case KEY_REQUIRED:
|
---|
571 | return tags.containsKey(key);
|
---|
572 | case KEY_VALUE:
|
---|
573 | return tags.containsKey(key) && getValues().contains(tags.get(key)) ? Boolean.TRUE : null;
|
---|
574 | case KEY_VALUE_REQUIRED:
|
---|
575 | return tags.containsKey(key) && getValues().contains(tags.get(key));
|
---|
576 | default:
|
---|
577 | throw new IllegalStateException();
|
---|
578 | }
|
---|
579 | }
|
---|
580 |
|
---|
581 | @Override
|
---|
582 | public String toString() {
|
---|
583 | return "KeyedItem [key=" + key + ", text=" + text
|
---|
584 | + ", text_context=" + text_context + ", match=" + match
|
---|
585 | + ']';
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Invisible type allowing to hardcode an OSM key/value from the preset definition.
|
---|
591 | */
|
---|
592 | public static class Key extends KeyedItem {
|
---|
593 |
|
---|
594 | /** The hardcoded value for key */
|
---|
595 | public String value;
|
---|
596 |
|
---|
597 | @Override
|
---|
598 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
599 | return false;
|
---|
600 | }
|
---|
601 |
|
---|
602 | @Override
|
---|
603 | public void addCommands(List<Tag> changedTags) {
|
---|
604 | changedTags.add(new Tag(key, value));
|
---|
605 | }
|
---|
606 |
|
---|
607 | @Override
|
---|
608 | public MatchType getDefaultMatch() {
|
---|
609 | return MatchType.KEY_VALUE_REQUIRED;
|
---|
610 | }
|
---|
611 |
|
---|
612 | @Override
|
---|
613 | public Collection<String> getValues() {
|
---|
614 | return Collections.singleton(value);
|
---|
615 | }
|
---|
616 |
|
---|
617 | @Override
|
---|
618 | public String toString() {
|
---|
619 | return "Key [key=" + key + ", value=" + value + ", text=" + text
|
---|
620 | + ", text_context=" + text_context + ", match=" + match
|
---|
621 | + ']';
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * Text field type.
|
---|
627 | */
|
---|
628 | public static class Text extends KeyedItem {
|
---|
629 |
|
---|
630 | /** The localized version of {@link #text}. */
|
---|
631 | public String locale_text;
|
---|
632 | public String default_;
|
---|
633 | public String originalValue;
|
---|
634 | public String use_last_as_default = "false";
|
---|
635 | public String auto_increment;
|
---|
636 | public String length;
|
---|
637 | public String alternative_autocomplete_keys;
|
---|
638 |
|
---|
639 | private JComponent value;
|
---|
640 |
|
---|
641 | @Override
|
---|
642 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
643 |
|
---|
644 | // find out if our key is already used in the selection.
|
---|
645 | Usage usage = determineTextUsage(sel, key);
|
---|
646 | AutoCompletingTextField textField = new AutoCompletingTextField();
|
---|
647 | if (alternative_autocomplete_keys != null) {
|
---|
648 | initAutoCompletionField(textField, (key + ',' + alternative_autocomplete_keys).split(","));
|
---|
649 | } else {
|
---|
650 | initAutoCompletionField(textField, key);
|
---|
651 | }
|
---|
652 | if (Main.pref.getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
653 | textField.setHint(key);
|
---|
654 | }
|
---|
655 | if (length != null && !length.isEmpty()) {
|
---|
656 | textField.setMaxChars(Integer.valueOf(length));
|
---|
657 | }
|
---|
658 | if (usage.unused()) {
|
---|
659 | if (auto_increment_selected != 0 && auto_increment != null) {
|
---|
660 | try {
|
---|
661 | textField.setText(Integer.toString(Integer.parseInt(LAST_VALUES.get(key)) + auto_increment_selected));
|
---|
662 | } catch (NumberFormatException ex) {
|
---|
663 | // Ignore - cannot auto-increment if last was non-numeric
|
---|
664 | if (Main.isTraceEnabled()) {
|
---|
665 | Main.trace(ex.getMessage());
|
---|
666 | }
|
---|
667 | }
|
---|
668 | } else if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
669 | // selected osm primitives are untagged or filling default values feature is enabled
|
---|
670 | if (!"false".equals(use_last_as_default) && LAST_VALUES.containsKey(key) && !presetInitiallyMatches) {
|
---|
671 | textField.setText(LAST_VALUES.get(key));
|
---|
672 | } else {
|
---|
673 | textField.setText(default_);
|
---|
674 | }
|
---|
675 | } else {
|
---|
676 | // selected osm primitives are tagged and filling default values feature is disabled
|
---|
677 | textField.setText("");
|
---|
678 | }
|
---|
679 | value = textField;
|
---|
680 | originalValue = null;
|
---|
681 | } else if (usage.hasUniqueValue()) {
|
---|
682 | // all objects use the same value
|
---|
683 | textField.setText(usage.getFirst());
|
---|
684 | value = textField;
|
---|
685 | originalValue = usage.getFirst();
|
---|
686 | } else {
|
---|
687 | // the objects have different values
|
---|
688 | JosmComboBox<String> comboBox = new JosmComboBox<>(usage.values.toArray(new String[0]));
|
---|
689 | comboBox.setEditable(true);
|
---|
690 | comboBox.setEditor(textField);
|
---|
691 | comboBox.getEditor().setItem(DIFFERENT);
|
---|
692 | value = comboBox;
|
---|
693 | originalValue = DIFFERENT;
|
---|
694 | }
|
---|
695 | if (locale_text == null) {
|
---|
696 | locale_text = getLocaleText(text, text_context, null);
|
---|
697 | }
|
---|
698 |
|
---|
699 | // if there's an auto_increment setting, then wrap the text field
|
---|
700 | // into a panel, appending a number of buttons.
|
---|
701 | // auto_increment has a format like -2,-1,1,2
|
---|
702 | // the text box being the first component in the panel is relied
|
---|
703 | // on in a rather ugly fashion further down.
|
---|
704 | if (auto_increment != null) {
|
---|
705 | ButtonGroup bg = new ButtonGroup();
|
---|
706 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
707 | pnl.add(value, GBC.std().fill(GBC.HORIZONTAL));
|
---|
708 |
|
---|
709 | // first, one button for each auto_increment value
|
---|
710 | for (final String ai : auto_increment.split(",")) {
|
---|
711 | JToggleButton aibutton = new JToggleButton(ai);
|
---|
712 | aibutton.setToolTipText(tr("Select auto-increment of {0} for this field", ai));
|
---|
713 | aibutton.setMargin(new java.awt.Insets(0, 0, 0, 0));
|
---|
714 | aibutton.setFocusable(false);
|
---|
715 | saveHorizontalSpace(aibutton);
|
---|
716 | bg.add(aibutton);
|
---|
717 | try {
|
---|
718 | // TODO there must be a better way to parse a number like "+3" than this.
|
---|
719 | final int buttonvalue = (NumberFormat.getIntegerInstance().parse(ai.replace("+", ""))).intValue();
|
---|
720 | if (auto_increment_selected == buttonvalue) aibutton.setSelected(true);
|
---|
721 | aibutton.addActionListener(new ActionListener() {
|
---|
722 | @Override
|
---|
723 | public void actionPerformed(ActionEvent e) {
|
---|
724 | auto_increment_selected = buttonvalue;
|
---|
725 | }
|
---|
726 | });
|
---|
727 | pnl.add(aibutton, GBC.std());
|
---|
728 | } catch (ParseException x) {
|
---|
729 | Main.error("Cannot parse auto-increment value of '" + ai + "' into an integer");
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | // an invisible toggle button for "release" of the button group
|
---|
734 | final JToggleButton clearbutton = new JToggleButton("X");
|
---|
735 | clearbutton.setVisible(false);
|
---|
736 | clearbutton.setFocusable(false);
|
---|
737 | bg.add(clearbutton);
|
---|
738 | // and its visible counterpart. - this mechanism allows us to
|
---|
739 | // have *no* button selected after the X is clicked, instead
|
---|
740 | // of the X remaining selected
|
---|
741 | JButton releasebutton = new JButton("X");
|
---|
742 | releasebutton.setToolTipText(tr("Cancel auto-increment for this field"));
|
---|
743 | releasebutton.setMargin(new java.awt.Insets(0, 0, 0, 0));
|
---|
744 | releasebutton.setFocusable(false);
|
---|
745 | releasebutton.addActionListener(new ActionListener() {
|
---|
746 | @Override
|
---|
747 | public void actionPerformed(ActionEvent e) {
|
---|
748 | auto_increment_selected = 0;
|
---|
749 | clearbutton.setSelected(true);
|
---|
750 | }
|
---|
751 | });
|
---|
752 | saveHorizontalSpace(releasebutton);
|
---|
753 | pnl.add(releasebutton, GBC.eol());
|
---|
754 | value = pnl;
|
---|
755 | }
|
---|
756 | p.add(new JLabel(locale_text+':'), GBC.std().insets(0, 0, 10, 0));
|
---|
757 | p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
758 | return true;
|
---|
759 | }
|
---|
760 |
|
---|
761 | private static void saveHorizontalSpace(AbstractButton button) {
|
---|
762 | Insets insets = button.getBorder().getBorderInsets(button);
|
---|
763 | // Ensure the current look&feel does not waste horizontal space (as seen in Nimbus & Aqua)
|
---|
764 | if (insets != null && insets.left+insets.right > insets.top+insets.bottom) {
|
---|
765 | int min = Math.min(insets.top, insets.bottom);
|
---|
766 | button.setBorder(BorderFactory.createEmptyBorder(insets.top, min, insets.bottom, min));
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | private static String getValue(Component comp) {
|
---|
771 | if (comp instanceof JosmComboBox) {
|
---|
772 | return ((JosmComboBox<?>) comp).getEditor().getItem().toString();
|
---|
773 | } else if (comp instanceof JosmTextField) {
|
---|
774 | return ((JosmTextField) comp).getText();
|
---|
775 | } else if (comp instanceof JPanel) {
|
---|
776 | return getValue(((JPanel) comp).getComponent(0));
|
---|
777 | } else {
|
---|
778 | return null;
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 | @Override
|
---|
783 | public void addCommands(List<Tag> changedTags) {
|
---|
784 |
|
---|
785 | // return if unchanged
|
---|
786 | String v = getValue(value);
|
---|
787 | if (v == null) {
|
---|
788 | Main.error("No 'last value' support for component " + value);
|
---|
789 | return;
|
---|
790 | }
|
---|
791 |
|
---|
792 | v = Tag.removeWhiteSpaces(v);
|
---|
793 |
|
---|
794 | if (!"false".equals(use_last_as_default) || auto_increment != null) {
|
---|
795 | LAST_VALUES.put(key, v);
|
---|
796 | }
|
---|
797 | if (v.equals(originalValue) || (originalValue == null && v.isEmpty()))
|
---|
798 | return;
|
---|
799 |
|
---|
800 | changedTags.add(new Tag(key, v));
|
---|
801 | AutoCompletionManager.rememberUserInput(key, v, true);
|
---|
802 | }
|
---|
803 |
|
---|
804 | @Override
|
---|
805 | boolean requestFocusInWindow() {
|
---|
806 | return value.requestFocusInWindow();
|
---|
807 | }
|
---|
808 |
|
---|
809 | @Override
|
---|
810 | public MatchType getDefaultMatch() {
|
---|
811 | return MatchType.NONE;
|
---|
812 | }
|
---|
813 |
|
---|
814 | @Override
|
---|
815 | public Collection<String> getValues() {
|
---|
816 | if (default_ == null || default_.isEmpty())
|
---|
817 | return Collections.emptyList();
|
---|
818 | return Collections.singleton(default_);
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | /**
|
---|
823 | * A group of {@link Check}s.
|
---|
824 | * @since 6114
|
---|
825 | */
|
---|
826 | public static class CheckGroup extends TaggingPresetItem {
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Number of columns (positive integer)
|
---|
830 | */
|
---|
831 | public String columns;
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * List of checkboxes
|
---|
835 | */
|
---|
836 | public final List<Check> checks = new LinkedList<>();
|
---|
837 |
|
---|
838 | @Override
|
---|
839 | boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
840 | Integer cols = Integer.valueOf(columns);
|
---|
841 | int rows = (int) Math.ceil(checks.size()/cols.doubleValue());
|
---|
842 | JPanel panel = new JPanel(new GridLayout(rows, cols));
|
---|
843 |
|
---|
844 | for (Check check : checks) {
|
---|
845 | check.addToPanel(panel, sel, presetInitiallyMatches);
|
---|
846 | }
|
---|
847 |
|
---|
848 | p.add(panel, GBC.eol());
|
---|
849 | return false;
|
---|
850 | }
|
---|
851 |
|
---|
852 | @Override
|
---|
853 | void addCommands(List<Tag> changedTags) {
|
---|
854 | for (Check check : checks) {
|
---|
855 | check.addCommands(changedTags);
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | @Override
|
---|
860 | Boolean matches(Map<String, String> tags) {
|
---|
861 | for (Check check : checks) {
|
---|
862 | if (Boolean.TRUE.equals(check.matches(tags))) {
|
---|
863 | return Boolean.TRUE;
|
---|
864 | }
|
---|
865 | }
|
---|
866 | return null;
|
---|
867 | }
|
---|
868 |
|
---|
869 | @Override
|
---|
870 | public String toString() {
|
---|
871 | return "CheckGroup [columns=" + columns + ']';
|
---|
872 | }
|
---|
873 | }
|
---|
874 |
|
---|
875 | /**
|
---|
876 | * Checkbox type.
|
---|
877 | */
|
---|
878 | public static class Check extends KeyedItem {
|
---|
879 |
|
---|
880 | /** The localized version of {@link #text}. */
|
---|
881 | public String locale_text;
|
---|
882 | /** the value to set when checked (default is "yes") */
|
---|
883 | public String value_on = OsmUtils.trueval;
|
---|
884 | /** the value to set when unchecked (default is "no") */
|
---|
885 | public String value_off = OsmUtils.falseval;
|
---|
886 | /** whether the off value is disabled in the dialog, i.e., only unset or yes are provided */
|
---|
887 | public boolean disable_off;
|
---|
888 | /** "on" or "off" or unset (default is unset) */
|
---|
889 | public String default_; // only used for tagless objects
|
---|
890 |
|
---|
891 | private QuadStateCheckBox check;
|
---|
892 | private QuadStateCheckBox.State initialState;
|
---|
893 | private Boolean def;
|
---|
894 |
|
---|
895 | @Override
|
---|
896 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
897 |
|
---|
898 | // find out if our key is already used in the selection.
|
---|
899 | final Usage usage = determineBooleanUsage(sel, key);
|
---|
900 | final String oneValue = usage.values.isEmpty() ? null : usage.values.last();
|
---|
901 | def = "on".equals(default_) ? Boolean.TRUE : "off".equals(default_) ? Boolean.FALSE : null;
|
---|
902 |
|
---|
903 | if (locale_text == null) {
|
---|
904 | locale_text = getLocaleText(text, text_context, null);
|
---|
905 | }
|
---|
906 |
|
---|
907 | if (usage.values.size() < 2 && (oneValue == null || value_on.equals(oneValue) || value_off.equals(oneValue))) {
|
---|
908 | if (def != null && !PROP_FILL_DEFAULT.get()) {
|
---|
909 | // default is set and filling default values feature is disabled - check if all primitives are untagged
|
---|
910 | for (OsmPrimitive s : sel) {
|
---|
911 | if (s.hasKeys()) {
|
---|
912 | def = null;
|
---|
913 | }
|
---|
914 | }
|
---|
915 | }
|
---|
916 |
|
---|
917 | // all selected objects share the same value which is either true or false or unset,
|
---|
918 | // we can display a standard check box.
|
---|
919 | initialState = value_on.equals(oneValue) || Boolean.TRUE.equals(def)
|
---|
920 | ? QuadStateCheckBox.State.SELECTED
|
---|
921 | : value_off.equals(oneValue) || Boolean.FALSE.equals(def)
|
---|
922 | ? QuadStateCheckBox.State.NOT_SELECTED
|
---|
923 | : QuadStateCheckBox.State.UNSET;
|
---|
924 |
|
---|
925 | } else {
|
---|
926 | def = null;
|
---|
927 | // the objects have different values, or one or more objects have something
|
---|
928 | // else than true/false. we display a quad-state check box
|
---|
929 | // in "partial" state.
|
---|
930 | initialState = QuadStateCheckBox.State.PARTIAL;
|
---|
931 | }
|
---|
932 |
|
---|
933 | final List<QuadStateCheckBox.State> allowedStates = new ArrayList<>(4);
|
---|
934 | if (QuadStateCheckBox.State.PARTIAL.equals(initialState))
|
---|
935 | allowedStates.add(QuadStateCheckBox.State.PARTIAL);
|
---|
936 | allowedStates.add(QuadStateCheckBox.State.SELECTED);
|
---|
937 | if (!disable_off || value_off.equals(oneValue))
|
---|
938 | allowedStates.add(QuadStateCheckBox.State.NOT_SELECTED);
|
---|
939 | allowedStates.add(QuadStateCheckBox.State.UNSET);
|
---|
940 | check = new QuadStateCheckBox(locale_text, initialState,
|
---|
941 | allowedStates.toArray(new QuadStateCheckBox.State[allowedStates.size()]));
|
---|
942 |
|
---|
943 | p.add(check, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
944 | return true;
|
---|
945 | }
|
---|
946 |
|
---|
947 | @Override
|
---|
948 | public void addCommands(List<Tag> changedTags) {
|
---|
949 | // if the user hasn't changed anything, don't create a command.
|
---|
950 | if (check.getState() == initialState && def == null) return;
|
---|
951 |
|
---|
952 | // otherwise change things according to the selected value.
|
---|
953 | changedTags.add(new Tag(key,
|
---|
954 | check.getState() == QuadStateCheckBox.State.SELECTED ? value_on :
|
---|
955 | check.getState() == QuadStateCheckBox.State.NOT_SELECTED ? value_off :
|
---|
956 | null));
|
---|
957 | }
|
---|
958 |
|
---|
959 | @Override
|
---|
960 | boolean requestFocusInWindow() {
|
---|
961 | return check.requestFocusInWindow();
|
---|
962 | }
|
---|
963 |
|
---|
964 | @Override
|
---|
965 | public MatchType getDefaultMatch() {
|
---|
966 | return MatchType.NONE;
|
---|
967 | }
|
---|
968 |
|
---|
969 | @Override
|
---|
970 | public Collection<String> getValues() {
|
---|
971 | return disable_off ? Arrays.asList(value_on) : Arrays.asList(value_on, value_off);
|
---|
972 | }
|
---|
973 |
|
---|
974 | @Override
|
---|
975 | public String toString() {
|
---|
976 | return "Check ["
|
---|
977 | + (locale_text != null ? "locale_text=" + locale_text + ", " : "")
|
---|
978 | + (value_on != null ? "value_on=" + value_on + ", " : "")
|
---|
979 | + (value_off != null ? "value_off=" + value_off + ", " : "")
|
---|
980 | + "default_=" + default_ + ", "
|
---|
981 | + (check != null ? "check=" + check + ", " : "")
|
---|
982 | + (initialState != null ? "initialState=" + initialState
|
---|
983 | + ", " : "") + "def=" + def + ']';
|
---|
984 | }
|
---|
985 | }
|
---|
986 |
|
---|
987 | /**
|
---|
988 | * Abstract superclass for combo box and multi-select list types.
|
---|
989 | */
|
---|
990 | public abstract static class ComboMultiSelect extends KeyedItem {
|
---|
991 |
|
---|
992 | /** The localized version of {@link #text}. */
|
---|
993 | public String locale_text;
|
---|
994 | public String values;
|
---|
995 | public String values_from;
|
---|
996 | /** The context used for translating {@link #values} */
|
---|
997 | public String values_context;
|
---|
998 | public String display_values;
|
---|
999 | /** The localized version of {@link #display_values}. */
|
---|
1000 | public String locale_display_values;
|
---|
1001 | public String short_descriptions;
|
---|
1002 | /** The localized version of {@link #short_descriptions}. */
|
---|
1003 | public String locale_short_descriptions;
|
---|
1004 | public String default_;
|
---|
1005 | public String delimiter = ";";
|
---|
1006 | public String use_last_as_default = "false";
|
---|
1007 | /** whether to use values for search via {@link TaggingPresetSelector} */
|
---|
1008 | public String values_searchable = "false";
|
---|
1009 |
|
---|
1010 | protected JComponent component;
|
---|
1011 | protected final Map<String, PresetListEntry> lhm = new LinkedHashMap<>();
|
---|
1012 | private boolean initialized;
|
---|
1013 | protected Usage usage;
|
---|
1014 | protected Object originalValue;
|
---|
1015 |
|
---|
1016 | protected abstract Object getSelectedItem();
|
---|
1017 |
|
---|
1018 | protected abstract void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches);
|
---|
1019 |
|
---|
1020 | protected char getDelChar() {
|
---|
1021 | return delimiter.isEmpty() ? ';' : delimiter.charAt(0);
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | @Override
|
---|
1025 | public Collection<String> getValues() {
|
---|
1026 | initListEntries();
|
---|
1027 | return lhm.keySet();
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | public Collection<String> getDisplayValues() {
|
---|
1031 | initListEntries();
|
---|
1032 | return Utils.transform(lhm.values(), new Utils.Function<PresetListEntry, String>() {
|
---|
1033 | @Override
|
---|
1034 | public String apply(PresetListEntry x) {
|
---|
1035 | return x.getDisplayValue(true);
|
---|
1036 | }
|
---|
1037 | });
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | @Override
|
---|
1041 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
1042 |
|
---|
1043 | initListEntries();
|
---|
1044 |
|
---|
1045 | // find out if our key is already used in the selection.
|
---|
1046 | usage = determineTextUsage(sel, key);
|
---|
1047 | if (!usage.hasUniqueValue() && !usage.unused()) {
|
---|
1048 | lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT));
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | p.add(new JLabel(tr("{0}:", locale_text)), GBC.std().insets(0, 0, 10, 0));
|
---|
1052 | addToPanelAnchor(p, default_, presetInitiallyMatches);
|
---|
1053 |
|
---|
1054 | return true;
|
---|
1055 |
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | private void initListEntries() {
|
---|
1059 | if (initialized) {
|
---|
1060 | lhm.remove(DIFFERENT); // possibly added in #addToPanel
|
---|
1061 | return;
|
---|
1062 | } else if (lhm.isEmpty()) {
|
---|
1063 | initListEntriesFromAttributes();
|
---|
1064 | } else {
|
---|
1065 | if (values != null) {
|
---|
1066 | Main.warn(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
1067 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
1068 | key, text, "values", "list_entry"));
|
---|
1069 | }
|
---|
1070 | if (display_values != null || locale_display_values != null) {
|
---|
1071 | Main.warn(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
1072 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
1073 | key, text, "display_values", "list_entry"));
|
---|
1074 | }
|
---|
1075 | if (short_descriptions != null || locale_short_descriptions != null) {
|
---|
1076 | Main.warn(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
1077 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
1078 | key, text, "short_descriptions", "list_entry"));
|
---|
1079 | }
|
---|
1080 | for (PresetListEntry e : lhm.values()) {
|
---|
1081 | if (e.value_context == null) {
|
---|
1082 | e.value_context = values_context;
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 | if (locale_text == null) {
|
---|
1087 | locale_text = getLocaleText(text, text_context, null);
|
---|
1088 | }
|
---|
1089 | initialized = true;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | private void initListEntriesFromAttributes() {
|
---|
1093 | char delChar = getDelChar();
|
---|
1094 |
|
---|
1095 | String[] value_array = null;
|
---|
1096 |
|
---|
1097 | if (values_from != null) {
|
---|
1098 | String[] class_method = values_from.split("#");
|
---|
1099 | if (class_method != null && class_method.length == 2) {
|
---|
1100 | try {
|
---|
1101 | Method method = Class.forName(class_method[0]).getMethod(class_method[1]);
|
---|
1102 | // Check method is public static String[] methodName()
|
---|
1103 | int mod = method.getModifiers();
|
---|
1104 | if (Modifier.isPublic(mod) && Modifier.isStatic(mod)
|
---|
1105 | && method.getReturnType().equals(String[].class) && method.getParameterTypes().length == 0) {
|
---|
1106 | value_array = (String[]) method.invoke(null);
|
---|
1107 | } else {
|
---|
1108 | Main.error(tr("Broken tagging preset \"{0}-{1}\" - Java method given in ''values_from'' is not \"{2}\"", key, text,
|
---|
1109 | "public static String[] methodName()"));
|
---|
1110 | }
|
---|
1111 | } catch (Exception e) {
|
---|
1112 | Main.error(tr("Broken tagging preset \"{0}-{1}\" - Java method given in ''values_from'' threw {2} ({3})", key, text,
|
---|
1113 | e.getClass().getName(), e.getMessage()));
|
---|
1114 | }
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | if (value_array == null) {
|
---|
1119 | value_array = splitEscaped(delChar, values);
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | final String displ = Utils.firstNonNull(locale_display_values, display_values);
|
---|
1123 | String[] display_array = displ == null ? value_array : splitEscaped(delChar, displ);
|
---|
1124 |
|
---|
1125 | final String descr = Utils.firstNonNull(locale_short_descriptions, short_descriptions);
|
---|
1126 | String[] short_descriptions_array = descr == null ? null : splitEscaped(delChar, descr);
|
---|
1127 |
|
---|
1128 | if (display_array.length != value_array.length) {
|
---|
1129 | Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''display_values'' must be the same as in ''values''",
|
---|
1130 | key, text));
|
---|
1131 | display_array = value_array;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | if (short_descriptions_array != null && short_descriptions_array.length != value_array.length) {
|
---|
1135 | Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''short_descriptions'' must be the same as in ''values''",
|
---|
1136 | key, text));
|
---|
1137 | short_descriptions_array = null;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | final List<PresetListEntry> entries = new ArrayList<>(value_array.length);
|
---|
1141 | for (int i = 0; i < value_array.length; i++) {
|
---|
1142 | final PresetListEntry e = new PresetListEntry(value_array[i]);
|
---|
1143 | e.locale_display_value = locale_display_values != null
|
---|
1144 | ? display_array[i]
|
---|
1145 | : trc(values_context, fixPresetString(display_array[i]));
|
---|
1146 | if (short_descriptions_array != null) {
|
---|
1147 | e.locale_short_description = locale_short_descriptions != null
|
---|
1148 | ? short_descriptions_array[i]
|
---|
1149 | : tr(fixPresetString(short_descriptions_array[i]));
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | entries.add(e);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | if (Main.pref.getBoolean("taggingpreset.sortvalues", true)) {
|
---|
1156 | Collections.sort(entries);
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | for (PresetListEntry i : entries) {
|
---|
1160 | lhm.put(i.value, i);
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | protected String getDisplayIfNull() {
|
---|
1166 | return null;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | @Override
|
---|
1170 | public void addCommands(List<Tag> changedTags) {
|
---|
1171 | Object obj = getSelectedItem();
|
---|
1172 | String display = (obj == null) ? null : obj.toString();
|
---|
1173 | String value = null;
|
---|
1174 | if (display == null) {
|
---|
1175 | display = getDisplayIfNull();
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | if (display != null) {
|
---|
1179 | for (Entry<String, PresetListEntry> entry : lhm.entrySet()) {
|
---|
1180 | String k = entry.getValue().toString();
|
---|
1181 | if (k != null && k.equals(display)) {
|
---|
1182 | value = entry.getKey();
|
---|
1183 | break;
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | if (value == null) {
|
---|
1187 | value = display;
|
---|
1188 | }
|
---|
1189 | } else {
|
---|
1190 | value = "";
|
---|
1191 | }
|
---|
1192 | value = Tag.removeWhiteSpaces(value);
|
---|
1193 |
|
---|
1194 | // no change if same as before
|
---|
1195 | if (originalValue == null) {
|
---|
1196 | if (value.isEmpty())
|
---|
1197 | return;
|
---|
1198 | } else if (value.equals(originalValue.toString()))
|
---|
1199 | return;
|
---|
1200 |
|
---|
1201 | if (!"false".equals(use_last_as_default)) {
|
---|
1202 | LAST_VALUES.put(key, value);
|
---|
1203 | }
|
---|
1204 | changedTags.add(new Tag(key, value));
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | public void addListEntry(PresetListEntry e) {
|
---|
1208 | lhm.put(e.value, e);
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | public void addListEntries(Collection<PresetListEntry> e) {
|
---|
1212 | for (PresetListEntry i : e) {
|
---|
1213 | addListEntry(i);
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | @Override
|
---|
1218 | boolean requestFocusInWindow() {
|
---|
1219 | return component.requestFocusInWindow();
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | private static final ListCellRenderer<PresetListEntry> RENDERER = new ListCellRenderer<PresetListEntry>() {
|
---|
1223 |
|
---|
1224 | private final JLabel lbl = new JLabel();
|
---|
1225 |
|
---|
1226 | @Override
|
---|
1227 | public Component getListCellRendererComponent(JList<? extends PresetListEntry> list, PresetListEntry item, int index,
|
---|
1228 | boolean isSelected, boolean cellHasFocus) {
|
---|
1229 |
|
---|
1230 | // Only return cached size, item is not shown
|
---|
1231 | if (!list.isShowing() && item.prefferedWidth != -1 && item.prefferedHeight != -1) {
|
---|
1232 | if (index == -1) {
|
---|
1233 | lbl.setPreferredSize(new Dimension(item.prefferedWidth, 10));
|
---|
1234 | } else {
|
---|
1235 | lbl.setPreferredSize(new Dimension(item.prefferedWidth, item.prefferedHeight));
|
---|
1236 | }
|
---|
1237 | return lbl;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | lbl.setPreferredSize(null);
|
---|
1241 |
|
---|
1242 | if (isSelected) {
|
---|
1243 | lbl.setBackground(list.getSelectionBackground());
|
---|
1244 | lbl.setForeground(list.getSelectionForeground());
|
---|
1245 | } else {
|
---|
1246 | lbl.setBackground(list.getBackground());
|
---|
1247 | lbl.setForeground(list.getForeground());
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | lbl.setOpaque(true);
|
---|
1251 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
1252 | lbl.setText("<html>" + item.getListDisplay() + "</html>");
|
---|
1253 | lbl.setIcon(item.getIcon());
|
---|
1254 | lbl.setEnabled(list.isEnabled());
|
---|
1255 |
|
---|
1256 | // Cache size
|
---|
1257 | item.prefferedWidth = lbl.getPreferredSize().width;
|
---|
1258 | item.prefferedHeight = lbl.getPreferredSize().height;
|
---|
1259 |
|
---|
1260 | // We do not want the editor to have the maximum height of all
|
---|
1261 | // entries. Return a dummy with bogus height.
|
---|
1262 | if (index == -1) {
|
---|
1263 | lbl.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10));
|
---|
1264 | }
|
---|
1265 | return lbl;
|
---|
1266 | }
|
---|
1267 | };
|
---|
1268 |
|
---|
1269 | protected ListCellRenderer<PresetListEntry> getListCellRenderer() {
|
---|
1270 | return RENDERER;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | @Override
|
---|
1274 | public MatchType getDefaultMatch() {
|
---|
1275 | return MatchType.NONE;
|
---|
1276 | }
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | /**
|
---|
1280 | * Combobox type.
|
---|
1281 | */
|
---|
1282 | public static class Combo extends ComboMultiSelect {
|
---|
1283 |
|
---|
1284 | public boolean editable = true;
|
---|
1285 | protected JosmComboBox<PresetListEntry> combo;
|
---|
1286 | public String length;
|
---|
1287 |
|
---|
1288 | /**
|
---|
1289 | * Constructs a new {@code Combo}.
|
---|
1290 | */
|
---|
1291 | public Combo() {
|
---|
1292 | delimiter = ",";
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | @Override
|
---|
1296 | protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
|
---|
1297 | if (!usage.unused()) {
|
---|
1298 | for (String s : usage.values) {
|
---|
1299 | if (!lhm.containsKey(s)) {
|
---|
1300 | lhm.put(s, new PresetListEntry(s));
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 | }
|
---|
1304 | if (def != null && !lhm.containsKey(def)) {
|
---|
1305 | lhm.put(def, new PresetListEntry(def));
|
---|
1306 | }
|
---|
1307 | lhm.put("", new PresetListEntry(""));
|
---|
1308 |
|
---|
1309 | combo = new JosmComboBox<>(lhm.values().toArray(new PresetListEntry[0]));
|
---|
1310 | component = combo;
|
---|
1311 | combo.setRenderer(getListCellRenderer());
|
---|
1312 | combo.setEditable(editable);
|
---|
1313 | combo.reinitialize(lhm.values());
|
---|
1314 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
1315 | initAutoCompletionField(tf, key);
|
---|
1316 | if (Main.pref.getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
1317 | tf.setHint(key);
|
---|
1318 | }
|
---|
1319 | if (length != null && !length.isEmpty()) {
|
---|
1320 | tf.setMaxChars(Integer.valueOf(length));
|
---|
1321 | }
|
---|
1322 | AutoCompletionList acList = tf.getAutoCompletionList();
|
---|
1323 | if (acList != null) {
|
---|
1324 | acList.add(getDisplayValues(), AutoCompletionItemPriority.IS_IN_STANDARD);
|
---|
1325 | }
|
---|
1326 | combo.setEditor(tf);
|
---|
1327 |
|
---|
1328 | if (usage.hasUniqueValue()) {
|
---|
1329 | // all items have the same value (and there were no unset items)
|
---|
1330 | originalValue = lhm.get(usage.getFirst());
|
---|
1331 | combo.setSelectedItem(originalValue);
|
---|
1332 | } else if (def != null && usage.unused()) {
|
---|
1333 | // default is set and all items were unset
|
---|
1334 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
1335 | // selected osm primitives are untagged or filling default feature is enabled
|
---|
1336 | combo.setSelectedItem(lhm.get(def).getDisplayValue(true));
|
---|
1337 | } else {
|
---|
1338 | // selected osm primitives are tagged and filling default feature is disabled
|
---|
1339 | combo.setSelectedItem("");
|
---|
1340 | }
|
---|
1341 | originalValue = lhm.get(DIFFERENT);
|
---|
1342 | } else if (usage.unused()) {
|
---|
1343 | // all items were unset (and so is default)
|
---|
1344 | originalValue = lhm.get("");
|
---|
1345 | if ("force".equals(use_last_as_default) && LAST_VALUES.containsKey(key) && !presetInitiallyMatches) {
|
---|
1346 | combo.setSelectedItem(lhm.get(LAST_VALUES.get(key)));
|
---|
1347 | } else {
|
---|
1348 | combo.setSelectedItem(originalValue);
|
---|
1349 | }
|
---|
1350 | } else {
|
---|
1351 | originalValue = lhm.get(DIFFERENT);
|
---|
1352 | combo.setSelectedItem(originalValue);
|
---|
1353 | }
|
---|
1354 | p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
1355 |
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | @Override
|
---|
1359 | protected Object getSelectedItem() {
|
---|
1360 | return combo.getSelectedItem();
|
---|
1361 |
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | @Override
|
---|
1365 | protected String getDisplayIfNull() {
|
---|
1366 | if (combo.isEditable())
|
---|
1367 | return combo.getEditor().getItem().toString();
|
---|
1368 | else
|
---|
1369 | return null;
|
---|
1370 | }
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | /**
|
---|
1374 | * Multi-select list type.
|
---|
1375 | */
|
---|
1376 | public static class MultiSelect extends ComboMultiSelect {
|
---|
1377 |
|
---|
1378 | /**
|
---|
1379 | * Number of rows to display (positive integer, optional).
|
---|
1380 | */
|
---|
1381 | public String rows;
|
---|
1382 | protected ConcatenatingJList list;
|
---|
1383 |
|
---|
1384 | @Override
|
---|
1385 | protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
|
---|
1386 | list = new ConcatenatingJList(delimiter, lhm.values().toArray(new PresetListEntry[0]));
|
---|
1387 | component = list;
|
---|
1388 | ListCellRenderer<PresetListEntry> renderer = getListCellRenderer();
|
---|
1389 | list.setCellRenderer(renderer);
|
---|
1390 |
|
---|
1391 | if (usage.hasUniqueValue() && !usage.unused()) {
|
---|
1392 | originalValue = usage.getFirst();
|
---|
1393 | list.setSelectedItem(originalValue);
|
---|
1394 | } else if (def != null && !usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
1395 | originalValue = DIFFERENT;
|
---|
1396 | list.setSelectedItem(def);
|
---|
1397 | } else if (usage.unused()) {
|
---|
1398 | originalValue = null;
|
---|
1399 | list.setSelectedItem(originalValue);
|
---|
1400 | } else {
|
---|
1401 | originalValue = DIFFERENT;
|
---|
1402 | list.setSelectedItem(originalValue);
|
---|
1403 | }
|
---|
1404 |
|
---|
1405 | JScrollPane sp = new JScrollPane(list);
|
---|
1406 | // if a number of rows has been specified in the preset,
|
---|
1407 | // modify preferred height of scroll pane to match that row count.
|
---|
1408 | if (rows != null) {
|
---|
1409 | double height = renderer.getListCellRendererComponent(list,
|
---|
1410 | new PresetListEntry("x"), 0, false, false).getPreferredSize().getHeight() * Integer.parseInt(rows);
|
---|
1411 | sp.setPreferredSize(new Dimension((int) sp.getPreferredSize().getWidth(), (int) height));
|
---|
1412 | }
|
---|
1413 | p.add(sp, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | @Override
|
---|
1417 | protected Object getSelectedItem() {
|
---|
1418 | return list.getSelectedItem();
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | @Override
|
---|
1422 | public void addCommands(List<Tag> changedTags) {
|
---|
1423 | // Do not create any commands if list has been disabled because of an unknown value (fix #8605)
|
---|
1424 | if (list.isEnabled()) {
|
---|
1425 | super.addCommands(changedTags);
|
---|
1426 | }
|
---|
1427 | }
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /**
|
---|
1431 | * Class that allows list values to be assigned and retrieved as a comma-delimited
|
---|
1432 | * string (extracted from TaggingPreset)
|
---|
1433 | */
|
---|
1434 | private static class ConcatenatingJList extends JList<PresetListEntry> {
|
---|
1435 | private String delimiter;
|
---|
1436 |
|
---|
1437 | ConcatenatingJList(String del, PresetListEntry[] o) {
|
---|
1438 | super(o);
|
---|
1439 | delimiter = del;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | public void setSelectedItem(Object o) {
|
---|
1443 | if (o == null) {
|
---|
1444 | clearSelection();
|
---|
1445 | } else {
|
---|
1446 | String s = o.toString();
|
---|
1447 | Set<String> parts = new TreeSet<>(Arrays.asList(s.split(delimiter)));
|
---|
1448 | ListModel<PresetListEntry> lm = getModel();
|
---|
1449 | int[] intParts = new int[lm.getSize()];
|
---|
1450 | int j = 0;
|
---|
1451 | for (int i = 0; i < lm.getSize(); i++) {
|
---|
1452 | final String value = lm.getElementAt(i).value;
|
---|
1453 | if (parts.contains(value)) {
|
---|
1454 | intParts[j++] = i;
|
---|
1455 | parts.remove(value);
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 | setSelectedIndices(Arrays.copyOf(intParts, j));
|
---|
1459 | // check if we have actually managed to represent the full
|
---|
1460 | // value with our presets. if not, cop out; we will not offer
|
---|
1461 | // a selection list that threatens to ruin the value.
|
---|
1462 | setEnabled(parts.isEmpty());
|
---|
1463 | }
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | public String getSelectedItem() {
|
---|
1467 | ListModel<PresetListEntry> lm = getModel();
|
---|
1468 | int[] si = getSelectedIndices();
|
---|
1469 | StringBuilder builder = new StringBuilder();
|
---|
1470 | for (int i = 0; i < si.length; i++) {
|
---|
1471 | if (i > 0) {
|
---|
1472 | builder.append(delimiter);
|
---|
1473 | }
|
---|
1474 | builder.append(lm.getElementAt(si[i]).value);
|
---|
1475 | }
|
---|
1476 | return builder.toString();
|
---|
1477 | }
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | public static Set<TaggingPresetType> getType(String types) throws SAXException {
|
---|
1481 | if (types == null || types.isEmpty()) {
|
---|
1482 | throw new SAXException(tr("Unknown type: {0}", types));
|
---|
1483 | }
|
---|
1484 | if (TYPE_CACHE.containsKey(types))
|
---|
1485 | return TYPE_CACHE.get(types);
|
---|
1486 | Set<TaggingPresetType> result = EnumSet.noneOf(TaggingPresetType.class);
|
---|
1487 | for (String type : Arrays.asList(types.split(","))) {
|
---|
1488 | try {
|
---|
1489 | TaggingPresetType presetType = TaggingPresetType.fromString(type);
|
---|
1490 | result.add(presetType);
|
---|
1491 | } catch (IllegalArgumentException e) {
|
---|
1492 | throw new SAXException(tr("Unknown type: {0}", type), e);
|
---|
1493 | }
|
---|
1494 | }
|
---|
1495 | TYPE_CACHE.put(types, result);
|
---|
1496 | return result;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | static String fixPresetString(String s) {
|
---|
1500 | return s == null ? s : s.replaceAll("'", "''");
|
---|
1501 | }
|
---|
1502 |
|
---|
1503 | private static String getLocaleText(String text, String text_context, String defaultText) {
|
---|
1504 | if (text == null) {
|
---|
1505 | return defaultText;
|
---|
1506 | } else if (text_context != null) {
|
---|
1507 | return trc(text_context, fixPresetString(text));
|
---|
1508 | } else {
|
---|
1509 | return tr(fixPresetString(text));
|
---|
1510 | }
|
---|
1511 | }
|
---|
1512 |
|
---|
1513 | /**
|
---|
1514 | * allow escaped comma in comma separated list:
|
---|
1515 | * "A\, B\, C,one\, two" --> ["A, B, C", "one, two"]
|
---|
1516 | * @param delimiter the delimiter, e.g. a comma. separates the entries and
|
---|
1517 | * must be escaped within one entry
|
---|
1518 | * @param s the string
|
---|
1519 | */
|
---|
1520 | private static String[] splitEscaped(char delimiter, String s) {
|
---|
1521 | if (s == null)
|
---|
1522 | return new String[0];
|
---|
1523 | List<String> result = new ArrayList<>();
|
---|
1524 | boolean backslash = false;
|
---|
1525 | StringBuilder item = new StringBuilder();
|
---|
1526 | for (int i = 0; i < s.length(); i++) {
|
---|
1527 | char ch = s.charAt(i);
|
---|
1528 | if (backslash) {
|
---|
1529 | item.append(ch);
|
---|
1530 | backslash = false;
|
---|
1531 | } else if (ch == '\\') {
|
---|
1532 | backslash = true;
|
---|
1533 | } else if (ch == delimiter) {
|
---|
1534 | result.add(item.toString());
|
---|
1535 | item.setLength(0);
|
---|
1536 | } else {
|
---|
1537 | item.append(ch);
|
---|
1538 | }
|
---|
1539 | }
|
---|
1540 | if (item.length() > 0) {
|
---|
1541 | result.add(item.toString());
|
---|
1542 | }
|
---|
1543 | return result.toArray(new String[result.size()]);
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
1547 | Usage returnValue = new Usage();
|
---|
1548 | returnValue.values = new TreeSet<>();
|
---|
1549 | for (OsmPrimitive s : sel) {
|
---|
1550 | String v = s.get(key);
|
---|
1551 | if (v != null) {
|
---|
1552 | returnValue.values.add(v);
|
---|
1553 | } else {
|
---|
1554 | returnValue.hadEmpty = true;
|
---|
1555 | }
|
---|
1556 | if (s.hasKeys()) {
|
---|
1557 | returnValue.hadKeys = true;
|
---|
1558 | }
|
---|
1559 | }
|
---|
1560 | return returnValue;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
1564 |
|
---|
1565 | Usage returnValue = new Usage();
|
---|
1566 | returnValue.values = new TreeSet<>();
|
---|
1567 | for (OsmPrimitive s : sel) {
|
---|
1568 | String booleanValue = OsmUtils.getNamedOsmBoolean(s.get(key));
|
---|
1569 | if (booleanValue != null) {
|
---|
1570 | returnValue.values.add(booleanValue);
|
---|
1571 | }
|
---|
1572 | }
|
---|
1573 | return returnValue;
|
---|
1574 | }
|
---|
1575 |
|
---|
1576 | protected static ImageIcon loadImageIcon(String iconName, File zipIcons, Integer maxSize) {
|
---|
1577 | final Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
|
---|
1578 | ImageProvider imgProv = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true);
|
---|
1579 | if (maxSize != null) {
|
---|
1580 | imgProv.setMaxSize(maxSize);
|
---|
1581 | }
|
---|
1582 | return imgProv.get();
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | protected static Integer parseInteger(String str) {
|
---|
1586 | if (str == null || str.isEmpty())
|
---|
1587 | return null;
|
---|
1588 | try {
|
---|
1589 | return Integer.valueOf(str);
|
---|
1590 | } catch (Exception e) {
|
---|
1591 | if (Main.isTraceEnabled()) {
|
---|
1592 | Main.trace(e.getMessage());
|
---|
1593 | }
|
---|
1594 | }
|
---|
1595 | return null;
|
---|
1596 | }
|
---|
1597 | }
|
---|