Ignore:
Timestamp:
2020-01-19T17:33:40+01:00 (4 years ago)
Author:
simon04
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSetting.java

    r15293 r15731  
    77import javax.swing.Icon;
    88
    9 import org.openstreetmap.josm.spi.preferences.Config;
     9import org.openstreetmap.josm.data.preferences.AbstractToStringProperty;
    1010import org.openstreetmap.josm.tools.ImageProvider;
    1111import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
     
    3939     */
    4040    Object getValue();
     41
     42    /**
     43     * Create a matching {@link StyleSettingGui} instances for a given {@link StyleSetting} object.
     44     * @return matching {@code StyleSettingGui}
     45     * @throws UnsupportedOperationException when class of {@link StyleSetting} is not supported
     46     */
     47    default StyleSettingGui getStyleSettingGui() {
     48        throw new UnsupportedOperationException(getClass() + " not supported");
     49    }
    4150
    4251    /**
     
    8493        public final Icon icon;
    8594
    86         public StyleSettingGroup(StyleSource parentStyle, String label, String key, Icon icon) {
     95        StyleSettingGroup(StyleSource parentStyle, String label, String key, Icon icon) {
    8796            super(parentStyle, label);
    8897            this.key = Objects.requireNonNull(key);
     
    100109            String label = c.get("label", null, String.class);
    101110            if (label == null) {
    102                 Logging.warn("property 'label' required for boolean style setting");
     111                Logging.warn("property 'label' required for StyleSettingGroup");
    103112                return null;
    104113            }
     
    109118    }
    110119
     120    class PropertyStyleSetting<T> extends LabeledStyleSetting implements StyleSetting {
     121        private final Class<T> type;
     122        private final AbstractToStringProperty<T> property;
     123
     124        PropertyStyleSetting(StyleSource parentStyle, String label, Class<T> type, AbstractToStringProperty<T> property) {
     125            super(parentStyle, label);
     126            this.type = type;
     127            this.property = property;
     128        }
     129
     130        /**
     131         * Replies the property key.
     132         * @return The property key
     133         */
     134        public String getKey() {
     135            return property.getKey();
     136        }
     137
     138        @Override
     139        public T getValue() {
     140            return property.get();
     141        }
     142
     143        /**
     144         * Sets this property to the specified value.
     145         * @param value The new value of this property
     146         */
     147        public void setValue(T value) {
     148            property.put(value);
     149        }
     150
     151        /**
     152         * Sets this property to the specified string value.
     153         * @param value The new string value of this property
     154         */
     155        public void setStringValue(String value) {
     156            setValue(Cascade.convertTo(value, type));
     157        }
     158
     159        @Override
     160        public StyleSettingGui getStyleSettingGui() {
     161            return new PropertyStyleSettingGui<>(this);
     162        }
     163    }
     164
    111165    /**
    112166     * A style setting for boolean value (yes / no).
    113167     */
    114     class BooleanStyleSetting extends LabeledStyleSetting implements StyleSetting {
    115         public final String prefKey;
    116         public final boolean def;
    117 
    118         public BooleanStyleSetting(StyleSource parentStyle, String prefKey, String label, boolean def) {
    119             super(parentStyle, label);
    120             this.prefKey = Objects.requireNonNull(prefKey);
    121             this.def = def;
    122         }
    123 
    124         /**
    125          * Creates a new {@code BooleanStyleSetting}.
    126          * @param c cascade
    127          * @param parentStyle parent style source
    128          * @param key setting identifier
    129          * @return newly created {@code BooleanStyleSetting}
    130          */
    131         public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
    132             String label = c.get("label", null, String.class);
    133             if (label == null) {
    134                 Logging.warn("property 'label' required for boolean style setting");
    135                 return null;
    136             }
    137             Boolean def = c.get("default", null, Boolean.class);
    138             if (def == null) {
    139                 Logging.warn("property 'default' required for boolean style setting");
    140                 return null;
    141             }
    142             String prefKey = parentStyle.url + ":boolean:" + key;
    143             return new BooleanStyleSetting(parentStyle, prefKey, label, def);
     168    class BooleanStyleSetting extends PropertyStyleSetting<Boolean> {
     169        BooleanStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Boolean> property) {
     170            super(parentStyle, label, Boolean.class, property);
    144171        }
    145172
    146173        @Override
    147         public Object getValue() {
    148             String val = Config.getPref().get(prefKey, null);
    149             if (val == null) return def;
    150             return Boolean.valueOf(val);
    151         }
    152 
    153         public void setValue(Object o) {
    154             if (!(o instanceof Boolean)) {
    155                 throw new IllegalArgumentException();
    156             }
    157             boolean b = (Boolean) o;
    158             if (b == def) {
    159                 Config.getPref().put(prefKey, null);
    160             } else {
    161                 Config.getPref().putBoolean(prefKey, b);
    162             }
     174        public StyleSettingGui getStyleSettingGui() {
     175            return new BooleanStyleSettingGui(this);
    163176        }
    164177    }
Note: See TracChangeset for help on using the changeset viewer.