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