001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import org.openstreetmap.josm.spi.preferences.Config;
005import org.openstreetmap.josm.tools.Logging;
006
007/**
008 * Setting to customize a MapPaint style.
009 *
010 * Can be changed by the user in the right click menu of the mappaint style
011 * dialog.
012 *
013 * Defined in the MapCSS style, e.g.
014 * <pre>
015 * setting::highway_casing {
016 *   type: boolean;
017 *   label: tr("Draw highway casing");
018 *   default: true;
019 * }
020 *
021 * way[highway][setting("highway_casing")] {
022 *   casing-width: 2;
023 *   casing-color: white;
024 * }
025 * </pre>
026 */
027public interface StyleSetting {
028
029    /**
030     * gets the value for this setting
031     * @return The value the user selected
032     */
033    Object getValue();
034
035    /**
036     * A style setting for boolean value (yes / no).
037     */
038    class BooleanStyleSetting implements StyleSetting, Comparable<BooleanStyleSetting> {
039        public final StyleSource parentStyle;
040        public final String prefKey;
041        public final String label;
042        public final boolean def;
043
044        public BooleanStyleSetting(StyleSource parentStyle, String prefKey, String label, boolean def) {
045            this.parentStyle = parentStyle;
046            this.prefKey = prefKey;
047            this.label = label;
048            this.def = def;
049        }
050
051        public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
052            String label = c.get("label", null, String.class);
053            if (label == null) {
054                Logging.warn("property 'label' required for boolean style setting");
055                return null;
056            }
057            Boolean def = c.get("default", null, Boolean.class);
058            if (def == null) {
059                Logging.warn("property 'default' required for boolean style setting");
060                return null;
061            }
062            String prefKey = parentStyle.url + ":boolean:" + key;
063            return new BooleanStyleSetting(parentStyle, prefKey, label, def);
064        }
065
066        @Override
067        public Object getValue() {
068            String val = Config.getPref().get(prefKey, null);
069            if (val == null) return def;
070            return Boolean.valueOf(val);
071        }
072
073        public void setValue(Object o) {
074            if (!(o instanceof Boolean)) {
075                throw new IllegalArgumentException();
076            }
077            boolean b = (Boolean) o;
078            if (b == def) {
079                Config.getPref().put(prefKey, null);
080            } else {
081                Config.getPref().putBoolean(prefKey, b);
082            }
083        }
084
085        @Override
086        public int compareTo(BooleanStyleSetting o) {
087            return label.compareTo(o.label);
088        }
089    }
090}