source: josm/trunk/src/org/openstreetmap/josm/data/preferences/sources/MapPaintPrefHelper.java @ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 5 weeks ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences.sources;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.Objects;
13import java.util.TreeSet;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.tools.Utils;
17
18/**
19 * Helper class for map paint styles preferences.
20 * @since 12649 (extracted from gui.preferences package)
21 */
22public 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<>(Main.pref.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        Main.pref.putList("mappaint.style.known-defaults", new ArrayList<>(knownDefaults));
74
75        // XML style is not bundled anymore
76        list.remove(Utils.find(list, 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.title = tr("JOSM default (MapCSS)");
87        defJosmMapcss.description = tr("Internal style to be used as base for runtime switchable overlay styles");
88        ExtendedSourceEntry defPL2 = new ExtendedSourceEntry(type, "potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
89        defPL2.active = false;
90        defPL2.name = "standard";
91        defPL2.title = tr("Potlatch 2");
92        defPL2.description = tr("the main Potlatch 2 style");
93
94        return Arrays.asList(defJosmMapcss, defPL2);
95    }
96
97    @Override
98    public Map<String, String> serialize(SourceEntry entry) {
99        Map<String, String> res = new HashMap<>();
100        res.put("url", entry.url == null ? "" : entry.url);
101        res.put("title", entry.title == null ? "" : entry.title);
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}
Note: See TracBrowser for help on using the repository browser.