Changeset 18080 in josm for trunk/src


Ignore:
Timestamp:
2021-07-22T22:43:10+02:00 (2 months ago)
Author:
Don-vip
Message:

fix #21156 - fix #20851 - see #20861 - disable value_template for multiple selection (patch by marcello)

Location:
trunk/src/org/openstreetmap/josm/gui/tagging/presets
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItemGuiSupport.java

    r17876 r18080  
    66import org.openstreetmap.josm.data.osm.Tagged;
    77import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    8 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
    98import org.openstreetmap.josm.tools.ListenerList;
    109import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
     
    1413import java.util.Collections;
    1514import java.util.function.Supplier;
    16 import java.util.stream.Collectors;
    1715
    1816/**
     
    7068     * Creates a new {@code TaggingPresetItemGuiSupport}
    7169     *
     70     * @param presetInitiallyMatches whether the preset initially matched
    7271     * @param selected the selected primitives
    73      * @param presetInitiallyMatches whether the preset initially matched
     72     * @param changedTagsSupplier the changed tags
    7473     * @return the new {@code TaggingPresetItemGuiSupport}
    7574     */
     
    8281     * Creates a new {@code TaggingPresetItemGuiSupport}
    8382     *
     83     * @param presetInitiallyMatches whether the preset initially matched
    8484     * @param selected the selected primitives
    85      * @param presetInitiallyMatches whether the preset initially matched
    8685     * @return the new {@code TaggingPresetItemGuiSupport}
    8786     */
     
    9190    }
    9291
     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
    93110    @Override
    94111    public Collection<String> getTemplateKeys() {
    95         return changedTagsSupplier.get().stream().map(Tag::getKey).collect(Collectors.toList());
     112        return getTagged().keySet();
    96113    }
    97114
    98115    @Override
    99116    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;
    106119    }
    107120
    108121    @Override
    109122    public boolean evaluateCondition(SearchCompiler.Match condition) {
    110         Tagged tagged = Tagged.ofTags(changedTagsSupplier.get());
    111         return condition.match(tagged);
     123        return condition.match(getTagged());
    112124    }
    113125
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Text.java

    r18040 r18080  
    239239    }
    240240
     241    /**
     242     * Set the value template.
     243     * @param pattern The value_template pattern.
     244     * @throws SAXException If an error occured while parsing.
     245     */
    241246    public void setValue_template(String pattern) throws SAXException { // NOPMD
    242247        try {
     
    249254
    250255    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
    252261            textField.getDocument().addDocumentListener(DocumentAdapter.create(ignore ->
    253262                    support.fireItemValueModified(this, key, textField.getText())));
    254263        } else { // only listen on calculated fields
    255             textField.setForeground(Color.BLUE);
    256264            support.addListener((source, key, newValue) -> {
    257265                String valueTemplateText = valueTemplate.getText(support);
     
    259267                        valueTemplate, key, source, newValue, valueTemplateText);
    260268                textField.setItem(valueTemplateText);
     269                if (originalValue != null && !originalValue.equals(valueTemplateText)) {
     270                    textField.setForeground(Color.RED);
     271                } else {
     272                    textField.setForeground(Color.BLUE);
     273                }
    261274            });
    262275        }
Note: See TracChangeset for help on using the changeset viewer.