1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.LinkedHashMap;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
11 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
12 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
13 | import org.openstreetmap.josm.data.preferences.CollectionProperty;
|
---|
14 |
|
---|
15 | class RecentTagCollection {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
|
---|
19 | */
|
---|
20 | static final class LruCache extends LinkedHashMap<Tag, Void> {
|
---|
21 | private final int capacity;
|
---|
22 |
|
---|
23 | LruCache(int capacity) {
|
---|
24 | super(capacity + 1, 1.1f, true);
|
---|
25 | this.capacity = capacity;
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
|
---|
30 | return size() > capacity;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | private final Map<Tag, Void> recentTags;
|
---|
35 | private SearchCompiler.Match tagsToIgnore;
|
---|
36 |
|
---|
37 | RecentTagCollection(final int capacity) {
|
---|
38 | recentTags = new LruCache(capacity);
|
---|
39 | tagsToIgnore = SearchCompiler.Never.INSTANCE;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void loadFromPreference(CollectionProperty property) {
|
---|
43 | recentTags.clear();
|
---|
44 | Iterator<String> it = property.get().iterator();
|
---|
45 | while (it.hasNext()) {
|
---|
46 | String key = it.next();
|
---|
47 | String value = it.next();
|
---|
48 | add(new Tag(key, value));
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void saveToPreference(CollectionProperty property) {
|
---|
53 | List<String> c = new ArrayList<>(recentTags.size() * 2);
|
---|
54 | for (Tag t : recentTags.keySet()) {
|
---|
55 | c.add(t.getKey());
|
---|
56 | c.add(t.getValue());
|
---|
57 | }
|
---|
58 | property.put(c);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void add(Tag tag) {
|
---|
62 | if (!tagsToIgnore.match(tag)) {
|
---|
63 | recentTags.put(tag, null);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public boolean isEmpty() {
|
---|
68 | return recentTags.isEmpty();
|
---|
69 | }
|
---|
70 |
|
---|
71 | public List<Tag> toList() {
|
---|
72 | return new ArrayList<>(recentTags.keySet());
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void setTagsToIgnore(SearchCompiler.Match tagsToIgnore) {
|
---|
76 | this.tagsToIgnore = tagsToIgnore;
|
---|
77 | recentTags.keySet().removeIf(tagsToIgnore::match);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void setTagsToIgnore(SearchAction.SearchSetting tagsToIgnore) throws SearchCompiler.ParseError {
|
---|
81 | setTagsToIgnore(tagsToIgnore.text.isEmpty() ? SearchCompiler.Never.INSTANCE : SearchCompiler.compile(tagsToIgnore));
|
---|
82 | }
|
---|
83 |
|
---|
84 | public SearchAction.SearchSetting ignoreTag(Tag tagToIgnore, SearchAction.SearchSetting settingToUpdate) throws SearchCompiler.ParseError {
|
---|
85 | final String forTag = SearchCompiler.buildSearchStringForTag(tagToIgnore.getKey(), tagToIgnore.getValue());
|
---|
86 | settingToUpdate.text = settingToUpdate.text.isEmpty()
|
---|
87 | ? forTag
|
---|
88 | : settingToUpdate.text + " OR " + forTag;
|
---|
89 | setTagsToIgnore(settingToUpdate);
|
---|
90 | return settingToUpdate;
|
---|
91 | }
|
---|
92 | }
|
---|