Ignore:
Timestamp:
2020-02-18T23:13:44+01:00 (4 years ago)
Author:
simon04
Message:

fix #18709 - Add Tag Dialog: disable auto completion during use of recent tags

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java

    r15837 r15885  
    1212import java.util.LinkedList;
    1313import java.util.Locale;
     14import java.util.stream.IntStream;
    1415
    1516import javax.swing.ComboBoxEditor;
     
    257258    /**
    258259     * Selects a given item in the ComboBox model
    259      * @param item      excepts AutoCompletionItem, String and null
     260     * @param item the item of type AutoCompletionItem, String or null
    260261     */
    261262    @Override
    262263    public void setSelectedItem(Object item) {
     264        setSelectedItem(item, false);
     265    }
     266
     267    /**
     268     * Selects a given item in the ComboBox model
     269     * @param item the item of type AutoCompletionItem, String or null
     270     * @param disableAutoComplete if true, autocomplete {@linkplain #setAutocompleteEnabled is disabled} during the operation
     271     * @since 15885
     272     */
     273    public void setSelectedItem(Object item, final boolean disableAutoComplete) {
     274        final boolean previousState = isAutocompleteEnabled();
     275        if (disableAutoComplete) {
     276            // disable autocomplete to prevent unnecessary actions in AutoCompletingComboBoxDocument#insertString
     277            setAutocompleteEnabled(false);
     278        }
    263279        if (item == null) {
    264280            super.setSelectedItem(null);
     
    268284            String s = (String) item;
    269285            // find the string in the model or create a new item
    270             for (int i = 0; i < getModel().getSize(); i++) {
    271                 AutoCompletionItem acItem = getModel().getElementAt(i);
    272                 if (s.equals(acItem.getValue())) {
    273                     super.setSelectedItem(acItem);
    274                     return;
    275                 }
    276             }
    277             super.setSelectedItem(new AutoCompletionItem(s, AutoCompletionPriority.UNKNOWN));
     286            AutoCompletionItem acItem = IntStream.range(0, getModel().getSize())
     287                    .mapToObj(i -> getModel().getElementAt(i))
     288                    .filter(i -> s.equals(i.getValue()))
     289                    .findFirst()
     290                    .orElseGet(() -> new AutoCompletionItem(s, AutoCompletionPriority.UNKNOWN));
     291            super.setSelectedItem(acItem);
    278292        } else {
     293            setAutocompleteEnabled(previousState);
    279294            throw new IllegalArgumentException("Unsupported item: "+item);
    280295        }
     296        setAutocompleteEnabled(previousState);
    281297    }
    282298
     
    293309        }
    294310        this.setSelectedItem(null);
    295         // disable autocomplete to prevent unnecessary actions in AutoCompletingComboBoxDocument#insertString
    296         autocompleteEnabled = false;
    297         this.setSelectedItem(oldValue);
    298         autocompleteEnabled = true;
     311        this.setSelectedItem(oldValue, true);
    299312    }
    300313
Note: See TracChangeset for help on using the changeset viewer.