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.List;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
10 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
11 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
12 | import org.openstreetmap.josm.data.osm.search.SearchSetting;
|
---|
13 | import org.openstreetmap.josm.data.preferences.ListProperty;
|
---|
14 | import org.openstreetmap.josm.gui.util.LruCache;
|
---|
15 | import org.openstreetmap.josm.tools.Logging;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Manages list of recently used tags that will be displayed in the {@link PropertiesDialog}.
|
---|
19 | */
|
---|
20 | class 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 | }
|
---|