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 | private final Map<Tag, Void> recentTags;
|
---|
18 | private SearchCompiler.Match tagsToIgnore;
|
---|
19 |
|
---|
20 | RecentTagCollection(final int capacity) {
|
---|
21 | // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
|
---|
22 | recentTags = new LinkedHashMap<Tag, Void>(capacity + 1, 1.1f, true) {
|
---|
23 | @Override
|
---|
24 | protected boolean removeEldestEntry(Map.Entry<Tag, Void> eldest) {
|
---|
25 | return size() > capacity;
|
---|
26 | }
|
---|
27 | };
|
---|
28 | tagsToIgnore = SearchCompiler.Never.INSTANCE;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void loadFromPreference(CollectionProperty property) {
|
---|
32 | recentTags.clear();
|
---|
33 | Iterator<String> it = property.get().iterator();
|
---|
34 | while (it.hasNext()) {
|
---|
35 | String key = it.next();
|
---|
36 | String value = it.next();
|
---|
37 | add(new Tag(key, value));
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void saveToPreference(CollectionProperty property) {
|
---|
42 | List<String> c = new ArrayList<>(recentTags.size() * 2);
|
---|
43 | for (Tag t : recentTags.keySet()) {
|
---|
44 | c.add(t.getKey());
|
---|
45 | c.add(t.getValue());
|
---|
46 | }
|
---|
47 | property.put(c);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void add(Tag tag) {
|
---|
51 | if (!tagsToIgnore.match(tag)) {
|
---|
52 | recentTags.put(tag, null);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public boolean isEmpty() {
|
---|
57 | return recentTags.isEmpty();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public List<Tag> toList() {
|
---|
61 | return new ArrayList<>(recentTags.keySet());
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void setTagsToIgnore(SearchCompiler.Match tagsToIgnore) {
|
---|
65 | this.tagsToIgnore = tagsToIgnore;
|
---|
66 | final Iterator<Tag> it = recentTags.keySet().iterator();
|
---|
67 | while (it.hasNext()) {
|
---|
68 | if (tagsToIgnore.match(it.next())) {
|
---|
69 | it.remove();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void setTagsToIgnore(SearchAction.SearchSetting tagsToIgnore) throws SearchCompiler.ParseError {
|
---|
75 | setTagsToIgnore(tagsToIgnore.text.isEmpty() ? SearchCompiler.Never.INSTANCE : SearchCompiler.compile(tagsToIgnore));
|
---|
76 | }
|
---|
77 |
|
---|
78 | public SearchAction.SearchSetting ignoreTag(Tag tagToIgnore, SearchAction.SearchSetting settingToUpdate) throws SearchCompiler.ParseError {
|
---|
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 | }
|
---|