source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSettingFactory.java@ 15934

Last change on this file since 15934 was 15731, checked in by simon04, 4 years ago

see #10435, fix #18095 - MapCSS: add settings of type string/double

File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.util.function.BiFunction;
5
6import org.openstreetmap.josm.data.preferences.BooleanProperty;
7import org.openstreetmap.josm.data.preferences.DoubleProperty;
8import org.openstreetmap.josm.data.preferences.StringProperty;
9import org.openstreetmap.josm.tools.Logging;
10
11/**
12 * Factory to create matching {@link StyleSetting} instances.
13 * @since 15731
14 */
15public final class StyleSettingFactory {
16
17 private StyleSettingFactory() {
18 // private constructor for factory classes
19 }
20
21 /**
22 * Creates a new {@code StyleSetting} based on the specified type by {@code c}.
23 * The type must be supported by {@link Cascade#convertTo} as well as {@link org.openstreetmap.josm.data.preferences.AbstractProperty}.
24 * @param c cascade
25 * @param parentStyle parent style source
26 * @param key setting identifier
27 * @return newly created {@code StyleSetting}
28 */
29 public static StyleSetting create(Cascade c, StyleSource parentStyle, String key) {
30 final String type = c.get("type", null, String.class);
31 final String qualifiedKey = String.join(":", parentStyle.url, type, key);
32 switch (type) {
33 case "boolean":
34 return forLabelAndDefault(c, Boolean.class, (label, defaultValue) -> {
35 final BooleanProperty property = new BooleanProperty(qualifiedKey, defaultValue);
36 return new StyleSetting.BooleanStyleSetting(parentStyle, label, property);
37 });
38 case "double":
39 return forLabelAndDefault(c, Double.class, (label, defaultValue) -> {
40 final DoubleProperty property = new DoubleProperty(qualifiedKey, defaultValue);
41 return new StyleSetting.PropertyStyleSetting<>(parentStyle, label, Double.class, property);
42 });
43 case "string":
44 return forLabelAndDefault(c, String.class, (label, defaultValue) -> {
45 final StringProperty property = new StringProperty(qualifiedKey, defaultValue);
46 return new StyleSetting.PropertyStyleSetting<>(parentStyle, label, String.class, property);
47 });
48 default:
49 Logging.warn("Unknown setting type {0} for style {1}", type, parentStyle.url);
50 return null;
51 }
52 }
53
54 private static <T> StyleSetting forLabelAndDefault(Cascade c, final Class<T> type, BiFunction<String, T, StyleSetting> function) {
55 String label = c.get("label", null, String.class);
56 if (label == null) {
57 Logging.warn("property 'label' required for style setting of type " + type);
58 return null;
59 }
60 T defaultValue = c.get("default", null, type);
61 if (defaultValue == null) {
62 Logging.warn("property 'default' required for style setting of type " + type);
63 return null;
64 }
65 return function.apply(label, defaultValue);
66 }
67}
Note: See TracBrowser for help on using the repository browser.