| 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.Collection;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 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 | return changed;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | @Override
|
|---|
| 79 | public Collection<ExtendedSourceEntry> getDefault() {
|
|---|
| 80 | ExtendedSourceEntry defJosmMapcss = new ExtendedSourceEntry(type, "elemstyles.mapcss", "resource://styles/standard/elemstyles.mapcss");
|
|---|
| 81 | defJosmMapcss.active = true;
|
|---|
| 82 | defJosmMapcss.name = "standard";
|
|---|
| 83 | defJosmMapcss.icon = new ImageProvider("logo").getResource();
|
|---|
| 84 | defJosmMapcss.title = tr("JOSM default (MapCSS)");
|
|---|
| 85 | defJosmMapcss.description = tr("Internal style to be used as base for runtime switchable overlay styles");
|
|---|
| 86 | return Collections.singletonList(defJosmMapcss);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | public Map<String, String> serialize(SourceEntry entry) {
|
|---|
| 91 | Map<String, String> res = super.serialize(entry);
|
|---|
| 92 | res.put("active", Boolean.toString(entry.active));
|
|---|
| 93 | if (entry.name != null) {
|
|---|
| 94 | res.put("ptoken", entry.name);
|
|---|
| 95 | }
|
|---|
| 96 | return res;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | @Override
|
|---|
| 100 | public SourceEntry deserialize(Map<String, String> s) {
|
|---|
| 101 | return new SourceEntry(type, s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|