source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSetting.java@ 13691

Last change on this file since 13691 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • Property svn:eol-style set to native
File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import org.openstreetmap.josm.spi.preferences.Config;
5import org.openstreetmap.josm.tools.Logging;
6
7/**
8 * Setting to customize a MapPaint style.
9 *
10 * Can be changed by the user in the right click menu of the mappaint style
11 * dialog.
12 *
13 * Defined in the MapCSS style, e.g.
14 * <pre>
15 * setting::highway_casing {
16 * type: boolean;
17 * label: tr("Draw highway casing");
18 * default: true;
19 * }
20 *
21 * way[highway][setting("highway_casing")] {
22 * casing-width: 2;
23 * casing-color: white;
24 * }
25 * </pre>
26 */
27public interface StyleSetting {
28
29 /**
30 * gets the value for this setting
31 * @return The value the user selected
32 */
33 Object getValue();
34
35 /**
36 * A style setting for boolean value (yes / no).
37 */
38 class BooleanStyleSetting implements StyleSetting {
39 public final StyleSource parentStyle;
40 public final String prefKey;
41 public final String label;
42 public final boolean def;
43
44 public BooleanStyleSetting(StyleSource parentStyle, String prefKey, String label, boolean def) {
45 this.parentStyle = parentStyle;
46 this.prefKey = prefKey;
47 this.label = label;
48 this.def = def;
49 }
50
51 public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
52 String label = c.get("label", null, String.class);
53 if (label == null) {
54 Logging.warn("property 'label' required for boolean style setting");
55 return null;
56 }
57 Boolean def = c.get("default", null, Boolean.class);
58 if (def == null) {
59 Logging.warn("property 'default' required for boolean style setting");
60 return null;
61 }
62 String prefKey = parentStyle.url + ":boolean:" + key;
63 return new BooleanStyleSetting(parentStyle, prefKey, label, def);
64 }
65
66 @Override
67 public Object getValue() {
68 String val = Config.getPref().get(prefKey, null);
69 if (val == null) return def;
70 return Boolean.valueOf(val);
71 }
72
73 public void setValue(Object o) {
74 if (!(o instanceof Boolean)) {
75 throw new IllegalArgumentException();
76 }
77 boolean b = (Boolean) o;
78 if (b == def) {
79 Config.getPref().put(prefKey, null);
80 } else {
81 Config.getPref().putBoolean(prefKey, b);
82 }
83 }
84 }
85}
Note: See TracBrowser for help on using the repository browser.