- Timestamp:
- 2016-11-23T15:08:41+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java
r11198 r11296 38 38 import java.util.Objects; 39 39 import java.util.TreeMap; 40 import java.util.stream.IntStream; 40 41 41 42 import javax.swing.AbstractAction; … … 79 80 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 80 81 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 82 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority; 81 83 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem; 82 84 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; … … 109 111 private final Comparator<AutoCompletionListItem> defaultACItemComparator = 110 112 (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue()); 111 112 private String lastAddKey;113 private String lastAddValue;114 113 115 114 /** Default number of recent tags */ … … 164 163 SearchAction.SearchSetting tagsToIgnore; 165 164 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 */ 167 171 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 } 168 179 169 180 /** … … 186 197 public final String getDataKey(int viewRow) { 187 198 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); 188 205 } 189 206 … … 335 352 private void cacheRecentTags() { 336 353 tags = recentTags.toList(); 354 Collections.reverse(tags); 337 355 } 338 356 … … 665 683 +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL)); 666 684 685 cacheRecentTags(); 667 686 AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager(); 668 687 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 669 688 670 AutoCompletionListItem itemToSelect = null;671 689 // remove the object's tag keys from the list 672 690 Iterator<AutoCompletionListItem> iter = keyList.iterator(); 673 691 while (iter.hasNext()) { 674 692 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(); 686 695 } 687 696 } … … 696 705 values.setEditable(true); 697 706 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 }); 704 716 705 717 focus = addFocusAdapter(autocomplete, defaultACItemComparator); … … 719 731 }); 720 732 721 cacheRecentTags();722 733 suggestRecentlyAddedTags(); 723 734 … … 861 872 int count = 0; 862 873 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++) { 867 875 final Tag t = tags.get(i); 868 876 boolean keyExists = keyExists(t); … … 1056 1064 } 1057 1065 } 1058 lastAddKey = key;1059 lastAddValue = value;1060 1066 recentTags.add(new Tag(key, value)); 1061 1067 valueCount.put(key, new TreeMap<String, Integer>());
Note:
See TracChangeset
for help on using the changeset viewer.