Ticket #21156: 21156.patch
| File 21156.patch, 5.8 KB (added by , 4 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItemGuiSupport.java
5 5 import org.openstreetmap.josm.data.osm.Tag; 6 6 import org.openstreetmap.josm.data.osm.Tagged; 7 7 import org.openstreetmap.josm.data.osm.search.SearchCompiler; 8 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;9 8 import org.openstreetmap.josm.tools.ListenerList; 10 9 import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider; 11 10 … … 13 12 import java.util.Collection; 14 13 import java.util.Collections; 15 14 import java.util.function.Supplier; 16 import java.util.stream.Collectors;17 15 18 16 /** 19 17 * Supporting class for creating the GUI for a preset item. … … 69 67 /** 70 68 * Creates a new {@code TaggingPresetItemGuiSupport} 71 69 * 70 * @param presetInitiallyMatches whether the preset initially matched 72 71 * @param selected the selected primitives 73 * @param presetInitiallyMatches whether the preset initially matched72 * @param changedTagsSupplier the changed tags 74 73 * @return the new {@code TaggingPresetItemGuiSupport} 75 74 */ 76 75 public static TaggingPresetItemGuiSupport create( … … 81 80 /** 82 81 * Creates a new {@code TaggingPresetItemGuiSupport} 83 82 * 83 * @param presetInitiallyMatches whether the preset initially matched 84 84 * @param selected the selected primitives 85 * @param presetInitiallyMatches whether the preset initially matched86 85 * @return the new {@code TaggingPresetItemGuiSupport} 87 86 */ 88 87 public static TaggingPresetItemGuiSupport create( … … 90 89 return new TaggingPresetItemGuiSupport(presetInitiallyMatches, Arrays.asList(selected), Collections::emptyList); 91 90 } 92 91 92 /** 93 * Get tags with values as currently shown in the dialog. 94 * If exactly one primitive is selected, get all tags of it, then 95 * overwrite with the current values shown in the dialog. 96 * Else get only the tags shown in the dialog. 97 * @return Tags 98 */ 99 public Tagged getTagged() { 100 if (selected.size() != 1) { 101 return Tagged.ofTags(changedTagsSupplier.get()); 102 } 103 // if there is only one primitive selected, get its tags 104 Tagged tagged = Tagged.ofMap(selected.iterator().next().getKeys()); 105 // update changed tags 106 changedTagsSupplier.get().forEach(tag -> tagged.put(tag)); 107 return tagged; 108 } 109 93 110 @Override 94 111 public Collection<String> getTemplateKeys() { 95 return changedTagsSupplier.get().stream().map(Tag::getKey).collect(Collectors.toList());112 return getTagged().keySet(); 96 113 } 97 114 98 115 @Override 99 116 public Object getTemplateValue(String key, boolean special) { 100 return changedTagsSupplier.get().stream() 101 .filter(tag -> key.equals(tag.getKey())) 102 .findFirst().map(Tag::getValue).orElseGet(() -> { 103 KeyedItem.Usage usage = KeyedItem.determineTextUsage(getSelected(), key); 104 return usage.hasUniqueValue() ? usage.getFirst() : null; 105 }); 117 String value = getTagged().get(key); 118 return (value == null || value.isEmpty()) ? null : value; 106 119 } 107 120 108 121 @Override 109 122 public boolean evaluateCondition(SearchCompiler.Match condition) { 110 Tagged tagged = Tagged.ofTags(changedTagsSupplier.get()); 111 return condition.match(tagged); 123 return condition.match(getTagged()); 112 124 } 113 125 114 126 /** -
src/org/openstreetmap/josm/gui/tagging/presets/items/Text.java
238 238 return Collections.singleton(default_); 239 239 } 240 240 241 /** 242 * Set the value template. 243 * @param pattern The value_template pattern. 244 * @throws SAXException If an error occured while parsing. 245 */ 241 246 public void setValue_template(String pattern) throws SAXException { // NOPMD 242 247 try { 243 248 this.valueTemplate = new TemplateParser(pattern).parse(); … … 248 253 } 249 254 250 255 private void setupListeners(AutoCompletingTextField textField, TaggingPresetItemGuiSupport support) { 251 if (valueTemplate == null) { // only fire on normal fields 256 // value_templates don't work well with multiple selected items because, 257 // as the command queue is currently implemented, we can only save 258 // the same value to all selected primitives, which is probably not 259 // what you want. 260 if (valueTemplate == null || support.getSelected().size() > 1) { // only fire on normal fields 252 261 textField.getDocument().addDocumentListener(DocumentAdapter.create(ignore -> 253 262 support.fireItemValueModified(this, key, textField.getText()))); 254 263 } else { // only listen on calculated fields 255 textField.setForeground(Color.BLUE);256 264 support.addListener((source, key, newValue) -> { 257 265 String valueTemplateText = valueTemplate.getText(support); 258 266 Logging.trace("Evaluating value_template {0} for key {1} from {2} with new value {3} => {4}", 259 267 valueTemplate, key, source, newValue, valueTemplateText); 260 268 textField.setItem(valueTemplateText); 269 if (originalValue != null && !originalValue.equals(valueTemplateText)) { 270 textField.setForeground(Color.RED); 271 } else { 272 textField.setForeground(Color.BLUE); 273 } 261 274 }); 262 275 } 263 276 }
