source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/RecentTagCollection.java

Last change on this file was 16082, checked in by simon04, 4 years ago

fix Java warning, Javadoc warning

File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Map;
8
9import org.openstreetmap.josm.data.osm.Tag;
10import org.openstreetmap.josm.data.osm.search.SearchCompiler;
11import org.openstreetmap.josm.data.osm.search.SearchParseError;
12import org.openstreetmap.josm.data.osm.search.SearchSetting;
13import org.openstreetmap.josm.data.preferences.ListProperty;
14import org.openstreetmap.josm.gui.util.LruCache;
15import org.openstreetmap.josm.tools.Logging;
16
17/**
18 * Manages list of recently used tags that will be displayed in the {@link PropertiesDialog}.
19 */
20class RecentTagCollection {
21
22 private final Map<Tag, Void> recentTags;
23 private SearchCompiler.Match tagsToIgnore;
24
25 RecentTagCollection(final int capacity) {
26 recentTags = new LruCache<>(capacity);
27 tagsToIgnore = SearchCompiler.Never.INSTANCE;
28 }
29
30 public void loadFromPreference(ListProperty property) {
31 recentTags.clear();
32 List<String> list = property.get();
33 Iterator<String> it = list.iterator();
34 while (it.hasNext()) {
35 String key = it.next();
36 if (it.hasNext()) {
37 String value = it.next();
38 add(new Tag(key, value));
39 } else {
40 Logging.error("Invalid or incomplete list property: " + list);
41 break;
42 }
43 }
44 }
45
46 public void saveToPreference(ListProperty property) {
47 List<String> c = new ArrayList<>(recentTags.size() * 2);
48 for (Tag t : recentTags.keySet()) {
49 c.add(t.getKey());
50 c.add(t.getValue());
51 }
52 property.put(c);
53 }
54
55 public void add(Tag tag) {
56 if (!tagsToIgnore.match(tag)) {
57 recentTags.put(tag, null);
58 }
59 }
60
61 public boolean isEmpty() {
62 return recentTags.isEmpty();
63 }
64
65 public List<Tag> toList() {
66 return new ArrayList<>(recentTags.keySet());
67 }
68
69 public void setTagsToIgnore(SearchCompiler.Match tagsToIgnore) {
70 this.tagsToIgnore = tagsToIgnore;
71 recentTags.keySet().removeIf(tagsToIgnore::match);
72 }
73
74 public void setTagsToIgnore(SearchSetting tagsToIgnore) throws SearchParseError {
75 setTagsToIgnore(tagsToIgnore.text.isEmpty() ? SearchCompiler.Never.INSTANCE : SearchCompiler.compile(tagsToIgnore));
76 }
77
78 public SearchSetting ignoreTag(Tag tagToIgnore, SearchSetting settingToUpdate) throws SearchParseError {
79 final String forTag = SearchCompiler.buildSearchStringForTag(tagToIgnore.getKey(), tagToIgnore.getValue());
80 settingToUpdate.text = settingToUpdate.text.isEmpty()
81 ? forTag
82 : settingToUpdate.text + " OR " + forTag;
83 setTagsToIgnore(settingToUpdate);
84 return settingToUpdate;
85 }
86}
Note: See TracBrowser for help on using the repository browser.