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

Last change on this file since 17318 was 16857, checked in by Klumbumbus, 4 years ago

see #19574 - MapCSS color setting: Use logic from MapCSSParser to create the NamedColorProperty (patch by taylor.smock)

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