From aef3f1a5b569c93a8b16ec4845ac375eca2d9166 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Wed, 23 Aug 2023 11:24:10 +0200 Subject: [PATCH] Support the text attribute in optional segments as labels --- documentation/docs/tutorials/presets.md | 6 ++-- .../de/blau/android/presets/PresetParser.java | 32 +++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/documentation/docs/tutorials/presets.md b/documentation/docs/tutorials/presets.md index ea55aaf335..02595abdb7 100644 --- a/documentation/docs/tutorials/presets.md +++ b/documentation/docs/tutorials/presets.md @@ -1,5 +1,5 @@ # Vespucci Preset System -_Documentation for Vespucci 19.1_ +_Documentation for Vespucci 19.2_ As explained in the [help documentation](../help/en/Presets.md) Vespucci uses JOSM compatible presets, currently any preset used in JOSM should simply work with Vespucci, however there can be differences. Particularly with the new preset driven tagging interface presets have become even more important and if you are writing presets yourself and want them to work well in Vespucci please keep on reading. @@ -133,10 +133,10 @@ Note: this is loosely based on what [JOSM claims](https://josm.openstreetmap.de/ | | match | supported | | | match_expression | ignored | | | use_last_as_default | extension | "force" has the same effect as "true" -|__<label>__ | | ignored | +|__<label>__ | | supported | |__<space/>__ | | ignored | |__<optional>__ | | supported | contained fields are not displayed the in "Properties" tab when a preset is applied except if applying with optional fields is used, further matching ignores any optional fields -| | text | ignored | not displayed +| | text | supported | |__<separator/>__ | | supported | starts a new row in the preset selection display |__<item_separator/>__ | | ignored | |__<link>__ | | supported | not legal inside <chunk> elements diff --git a/src/main/java/de/blau/android/presets/PresetParser.java b/src/main/java/de/blau/android/presets/PresetParser.java index b757676792..33ff7ab322 100644 --- a/src/main/java/de/blau/android/presets/PresetParser.java +++ b/src/main/java/de/blau/android/presets/PresetParser.java @@ -154,6 +154,7 @@ static void parseXML(@NonNull Preset preset, @NonNull InputStream input, boolean private int checkGroupCounter = 0; /** */ private String currentLabel = null; + private boolean addedLabel = false; /** * ${@inheritDoc}. @@ -304,6 +305,7 @@ private void parseItem(@NonNull String name, @NonNull Attributes attr) throws SA switch (name) { case OPTIONAL: inOptionalSection = true; + currentLabel = addLabelField(supportLabels, attr); break; case KEY_ATTR: String key = attr.getValue(KEY_ATTR); @@ -396,12 +398,7 @@ private void parseItem(@NonNull String name, @NonNull Attributes attr) throws SA } break; case LABEL: - currentLabel = attr.getValue(TEXT); - if (supportLabels) { - PresetLabelField labelField = new PresetLabelField(currentLabel, attr.getValue(TEXT_CONTEXT)); - currentItem.addField(labelField); - labelField.setOptional(inOptionalSection); - } + currentLabel = addLabelField(supportLabels, attr); break; case CHECKGROUP: checkGroup = new PresetCheckGroupField(currentItem.getName() + PresetCheckGroupField.class.getSimpleName() + checkGroupCounter); @@ -619,9 +616,30 @@ private void parseItem(@NonNull String name, @NonNull Attributes attr) throws SA Log.w(DEBUG_TAG, "Unknown start tag in preset item " + name); } // always zap label after next element - if (!LABEL.equals(name)) { + if (!addedLabel) { currentLabel = null; + } else { + addedLabel = false; + } + } + + /** + * Extract the label text and add a field if supportLabels is true + * + * @param supportLabels flag + * @param attr XML attributes + * @return the label text or null + */ + @Nullable + private String addLabelField(boolean supportLabels, @NonNull Attributes attr) { + String labelText = attr.getValue(TEXT); + if (supportLabels && labelText != null) { + PresetLabelField labelField = new PresetLabelField(labelText, attr.getValue(TEXT_CONTEXT)); + currentItem.addField(labelField); + labelField.setOptional(inOptionalSection); + addedLabel = true; } + return labelText; } /**