1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences.sources;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.LinkedHashSet;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Set;
|
---|
11 | import java.util.stream.Collectors;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Helper class for specialized extensions preferences.
|
---|
17 | * @since 12649 (extracted from gui.preferences package)
|
---|
18 | */
|
---|
19 | public abstract class SourcePrefHelper {
|
---|
20 |
|
---|
21 | private final String pref;
|
---|
22 | protected final SourceType type;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code SourcePrefHelper} for the given preference key.
|
---|
26 | * @param pref The preference key
|
---|
27 | * @param type The source type
|
---|
28 | * @since 12825
|
---|
29 | */
|
---|
30 | public SourcePrefHelper(String pref, SourceType type) {
|
---|
31 | this.pref = pref;
|
---|
32 | this.type = type;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the default sources provided by JOSM core.
|
---|
37 | * @return the default sources provided by JOSM core
|
---|
38 | */
|
---|
39 | public abstract Collection<ExtendedSourceEntry> getDefault();
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Serializes the given source entry as a map.
|
---|
43 | * @param entry source entry to serialize
|
---|
44 | * @return map (key=value)
|
---|
45 | */
|
---|
46 | public abstract Map<String, String> serialize(SourceEntry entry);
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Deserializes the given map as a source entry.
|
---|
50 | * @param entryStr map (key=value)
|
---|
51 | * @return source entry
|
---|
52 | */
|
---|
53 | public abstract SourceEntry deserialize(Map<String, String> entryStr);
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Returns the list of sources.
|
---|
57 | * @return The list of sources
|
---|
58 | */
|
---|
59 | public List<SourceEntry> get() {
|
---|
60 |
|
---|
61 | List<Map<String, String>> src = Config.getPref().getListOfMaps(pref, null);
|
---|
62 | if (src == null)
|
---|
63 | return new ArrayList<>(getDefault());
|
---|
64 |
|
---|
65 | List<SourceEntry> entries = new ArrayList<>();
|
---|
66 | for (Map<String, String> sourcePref : src) {
|
---|
67 | SourceEntry e = deserialize(new HashMap<>(sourcePref));
|
---|
68 | if (e != null) {
|
---|
69 | entries.add(e);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return entries;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Saves a list of sources to JOSM preferences.
|
---|
77 | * @param entries list of sources
|
---|
78 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
79 | */
|
---|
80 | public boolean put(Collection<? extends SourceEntry> entries) {
|
---|
81 | List<Map<String, String>> setting = serializeList(entries);
|
---|
82 | boolean unset = Config.getPref().getListOfMaps(pref, null) == null;
|
---|
83 | if (unset) {
|
---|
84 | Collection<Map<String, String>> def = serializeList(getDefault());
|
---|
85 | if (setting.equals(def))
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 | return Config.getPref().putListOfMaps(pref, setting);
|
---|
89 | }
|
---|
90 |
|
---|
91 | private List<Map<String, String>> serializeList(Collection<? extends SourceEntry> entries) {
|
---|
92 | return new ArrayList<>(entries).stream().map(this::serialize).collect(Collectors.toList());
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Returns the set of active source URLs.
|
---|
97 | * @return The set of active source URLs.
|
---|
98 | */
|
---|
99 | public final Set<String> getActiveUrls() {
|
---|
100 | Set<String> urls = new LinkedHashSet<>(); // retain order
|
---|
101 | for (SourceEntry e : get()) {
|
---|
102 | if (e.active) {
|
---|
103 | urls.add(e.url);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return urls;
|
---|
107 | }
|
---|
108 | }
|
---|