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