Changeset 11296 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2016-11-23T15:08:41+01:00 (7 years ago)
Author:
simon04
Message:

fix #14030 - Add tags: preselect second property if first is already used

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r11198 r11296  
    3838import java.util.Objects;
    3939import java.util.TreeMap;
     40import java.util.stream.IntStream;
    4041
    4142import javax.swing.AbstractAction;
     
    7980import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    8081import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
     82import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority;
    8183import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
    8284import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
     
    109111    private final Comparator<AutoCompletionListItem> defaultACItemComparator =
    110112            (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
    111 
    112     private String lastAddKey;
    113     private String lastAddValue;
    114113
    115114    /** Default number of recent tags */
     
    164163    SearchAction.SearchSetting tagsToIgnore;
    165164
    166     // Copy of recently added tags, used to cache initial status
     165    /**
     166     * Copy of recently added tags in sorted from newest to oldest order.
     167     *
     168     * We store the maximum number of recent tags to allow dynamic change of number of tags shown in the preferences.
     169     * Used to cache initial status.
     170     */
    167171    private List<Tag> tags;
     172
     173    static {
     174        // init user input based on recent tags
     175        final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
     176        recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
     177        recentTags.toList().forEach(tag -> AutoCompletionManager.rememberUserInput(tag.getKey(), tag.getValue(), false));
     178    }
    168179
    169180    /**
     
    186197    public final String getDataKey(int viewRow) {
    187198        return tagData.getValueAt(tagTable.convertRowIndexToModel(viewRow), 0).toString();
     199    }
     200
     201    private boolean containsDataKey(String key) {
     202        return IntStream.range(0, tagData.getRowCount())
     203                .mapToObj(i -> tagData.getValueAt(i, 0) /* sic! do not use getDataKey*/)
     204                .anyMatch(key::equals);
    188205    }
    189206
     
    335352    private void cacheRecentTags() {
    336353        tags = recentTags.toList();
     354        Collections.reverse(tags);
    337355    }
    338356
     
    665683                +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
    666684
     685            cacheRecentTags();
    667686            AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager();
    668687            List<AutoCompletionListItem> keyList = autocomplete.getKeys();
    669688
    670             AutoCompletionListItem itemToSelect = null;
    671689            // remove the object's tag keys from the list
    672690            Iterator<AutoCompletionListItem> iter = keyList.iterator();
    673691            while (iter.hasNext()) {
    674692                AutoCompletionListItem item = iter.next();
    675                 if (item.getValue().equals(lastAddKey)) {
    676                     itemToSelect = item;
    677                 }
    678                 for (int i = 0; i < tagData.getRowCount(); ++i) {
    679                     if (item.getValue().equals(tagData.getValueAt(i, 0) /* sic! do not use getDataKey*/)) {
    680                         if (itemToSelect == item) {
    681                             itemToSelect = null;
    682                         }
    683                         iter.remove();
    684                         break;
    685                     }
     693                if (containsDataKey(item.getValue())) {
     694                    iter.remove();
    686695                }
    687696            }
     
    696705            values.setEditable(true);
    697706            mainPanel.add(values, GBC.eop().fill(GBC.HORIZONTAL));
    698             if (itemToSelect != null) {
    699                 keys.setSelectedItem(itemToSelect);
    700                 if (lastAddValue != null) {
    701                     values.setSelectedItem(lastAddValue);
    702                 }
    703             }
     707
     708            // pre-fill first recent tag for which the key is not already present
     709            tags.stream()
     710                    .filter(tag -> !containsDataKey(tag.getKey()))
     711                    .findFirst()
     712                    .ifPresent(tag -> {
     713                        keys.setSelectedItem(tag.getKey());
     714                        values.setSelectedItem(tag.getValue());
     715                    });
    704716
    705717            focus = addFocusAdapter(autocomplete, defaultACItemComparator);
     
    719731                });
    720732
    721             cacheRecentTags();
    722733            suggestRecentlyAddedTags();
    723734
     
    861872            int count = 0;
    862873            destroyActions();
    863             // We store the maximum number of recent tags to allow dynamic change of number of tags shown in the preferences.
    864             // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum
    865             // number and not the number of tags to show.
    866             for (int i = tags.size()-1; i >= 0 && count < tagsToShow; i--) {
     874            for (int i = 0; i < tags.size() && count < tagsToShow; i++) {
    867875                final Tag t = tags.get(i);
    868876                boolean keyExists = keyExists(t);
     
    10561064                }
    10571065            }
    1058             lastAddKey = key;
    1059             lastAddValue = value;
    10601066            recentTags.add(new Tag(key, value));
    10611067            valueCount.put(key, new TreeMap<String, Integer>());
Note: See TracChangeset for help on using the changeset viewer.