1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences.sources;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Arrays;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Objects;
|
---|
12 | import java.util.TreeSet;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Helper class for map paint styles preferences.
|
---|
19 | * @since 12649 (extracted from gui.preferences package)
|
---|
20 | */
|
---|
21 | public class MapPaintPrefHelper extends SourcePrefHelper {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The unique instance.
|
---|
25 | */
|
---|
26 | public static final MapPaintPrefHelper INSTANCE = new MapPaintPrefHelper();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code MapPaintPrefHelper}.
|
---|
30 | */
|
---|
31 | public MapPaintPrefHelper() {
|
---|
32 | super("mappaint.style.entries", SourceType.MAP_PAINT_STYLE);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<SourceEntry> get() {
|
---|
37 | List<SourceEntry> ls = super.get();
|
---|
38 | if (insertNewDefaults(ls)) {
|
---|
39 | put(ls);
|
---|
40 | }
|
---|
41 | return ls;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * If the selection of default styles changes in future releases, add
|
---|
46 | * the new entries to the user-configured list. Remember the known URLs,
|
---|
47 | * so an item that was deleted explicitly is not added again.
|
---|
48 | * @param list new defaults
|
---|
49 | * @return {@code true} if a change occurred
|
---|
50 | */
|
---|
51 | private boolean insertNewDefaults(List<SourceEntry> list) {
|
---|
52 | boolean changed = false;
|
---|
53 |
|
---|
54 | Collection<String> knownDefaults = new TreeSet<>(Main.pref.getCollection("mappaint.style.known-defaults"));
|
---|
55 |
|
---|
56 | Collection<ExtendedSourceEntry> defaults = getDefault();
|
---|
57 | int insertionIdx = 0;
|
---|
58 | for (final SourceEntry def : defaults) {
|
---|
59 | int i = Utils.indexOf(list, se -> Objects.equals(def.url, se.url));
|
---|
60 | if (i == -1 && !knownDefaults.contains(def.url)) {
|
---|
61 | def.active = false;
|
---|
62 | list.add(insertionIdx, def);
|
---|
63 | insertionIdx++;
|
---|
64 | changed = true;
|
---|
65 | } else {
|
---|
66 | if (i >= insertionIdx) {
|
---|
67 | insertionIdx = i + 1;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | knownDefaults.add(def.url);
|
---|
71 | }
|
---|
72 | Main.pref.putCollection("mappaint.style.known-defaults", knownDefaults);
|
---|
73 |
|
---|
74 | // XML style is not bundled anymore
|
---|
75 | list.remove(Utils.find(list, se -> "resource://styles/standard/elemstyles.xml".equals(se.url)));
|
---|
76 |
|
---|
77 | return changed;
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
82 | ExtendedSourceEntry defJosmMapcss = new ExtendedSourceEntry(type, "elemstyles.mapcss", "resource://styles/standard/elemstyles.mapcss");
|
---|
83 | defJosmMapcss.active = true;
|
---|
84 | defJosmMapcss.name = "standard";
|
---|
85 | defJosmMapcss.title = tr("JOSM default (MapCSS)");
|
---|
86 | defJosmMapcss.description = tr("Internal style to be used as base for runtime switchable overlay styles");
|
---|
87 | ExtendedSourceEntry defPL2 = new ExtendedSourceEntry(type, "potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
|
---|
88 | defPL2.active = false;
|
---|
89 | defPL2.name = "standard";
|
---|
90 | defPL2.title = tr("Potlatch 2");
|
---|
91 | defPL2.description = tr("the main Potlatch 2 style");
|
---|
92 |
|
---|
93 | return Arrays.asList(defJosmMapcss, defPL2);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public Map<String, String> serialize(SourceEntry entry) {
|
---|
98 | Map<String, String> res = new HashMap<>();
|
---|
99 | res.put("url", entry.url == null ? "" : entry.url);
|
---|
100 | res.put("title", entry.title == null ? "" : entry.title);
|
---|
101 | res.put("active", Boolean.toString(entry.active));
|
---|
102 | if (entry.name != null) {
|
---|
103 | res.put("ptoken", entry.name);
|
---|
104 | }
|
---|
105 | return res;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public SourceEntry deserialize(Map<String, String> s) {
|
---|
110 | return new SourceEntry(type, s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
|
---|
111 | }
|
---|
112 | }
|
---|