Changeset 15731 in josm


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

Location:
trunk
Files:
2 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r15612 r15731  
    2525import java.util.Collection;
    2626import java.util.List;
    27 import java.util.Map.Entry;
    28 import java.util.stream.Collectors;
    2927
    3028import javax.swing.AbstractAction;
     
    6361import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    6462import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
    65 import org.openstreetmap.josm.gui.mappaint.StyleSetting;
    66 import org.openstreetmap.josm.gui.mappaint.StyleSetting.StyleSettingGroup;
    6763import org.openstreetmap.josm.gui.mappaint.StyleSettingGroupGui;
    68 import org.openstreetmap.josm.gui.mappaint.StyleSettingGuiFactory;
    6964import org.openstreetmap.josm.gui.mappaint.StyleSource;
    7065import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
     
    694689            } else {
    695690                // Add settings groups
    696                 for (Entry<StyleSettingGroup, List<StyleSetting>> e : style.settingGroups.entrySet()) {
    697                     new StyleSettingGroupGui(e.getKey(), e.getValue()).addMenuEntry(setMenu);
    698                 }
     691                style.settingGroups.forEach((group, settings) -> new StyleSettingGroupGui(group, settings).addMenuEntry(setMenu));
    699692                // Add settings not in groups
    700                 final List<StyleSetting> allStylesInGroups = style.settingGroups.values().stream()
    701                         .flatMap(List<StyleSetting>::stream).collect(Collectors.toList());
    702                 for (StyleSetting s : style.settings.stream()
    703                         .filter(s -> !allStylesInGroups.contains(s)).collect(Collectors.toList())) {
    704                     StyleSettingGuiFactory.getStyleSettingGui(s).addMenuEntry(setMenu);
    705                 }
     693                style.settings.stream()
     694                        .filter(s -> style.settingGroups.values().stream().flatMap(List::stream).noneMatch(s::equals))
     695                        .forEach(s -> s.getStyleSettingGui().addMenuEntry(setMenu));
    706696            }
    707697
  • trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingHelper.java

    r14120 r15731  
    121121                }
    122122                for (String key : sd.settings.keySet()) {
    123                     StyleSetting.BooleanStyleSetting match = source.settings.stream()
    124                             .filter(s -> s instanceof StyleSetting.BooleanStyleSetting)
    125                             .map(s -> (StyleSetting.BooleanStyleSetting) s)
    126                             .filter(bs -> bs.prefKey.endsWith(":" + key))
     123                    StyleSetting.PropertyStyleSetting<?> match = source.settings.stream()
     124                            .filter(s -> s instanceof StyleSetting.PropertyStyleSetting)
     125                            .map(s -> (StyleSetting.PropertyStyleSetting<?>) s)
     126                            .filter(bs -> bs.getKey().endsWith(":" + key))
    127127                            .findFirst().orElse(null);
    128128                    if (match == null) {
    129129                        Logging.warn(tr("Style setting not found: ''{0}''", key));
    130130                    } else {
    131                         boolean value = Boolean.parseBoolean(sd.settings.get(key));
     131                        String value = sd.settings.get(key);
    132132                        Logging.trace("setting applied: ''{0}:{1}''", key, value);
    133                         match.setValue(value);
     133                        match.setStringValue(value);
    134134                    }
    135135                }
  • 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    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSettingGroupGui.java

    r15289 r15731  
    6464        // Add individual settings
    6565        for (StyleSetting s : settings) {
    66             StyleSettingGuiFactory.getStyleSettingGui(s).addMenuEntry(submenu);
     66            s.getStyleSettingGui().addMenuEntry(submenu);
    6767        }
    6868        menu.add(submenu);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r15717 r15731  
    4949import org.openstreetmap.josm.gui.mappaint.StyleKeys;
    5050import org.openstreetmap.josm.gui.mappaint.StyleSetting;
    51 import org.openstreetmap.josm.gui.mappaint.StyleSetting.BooleanStyleSetting;
    5251import org.openstreetmap.josm.gui.mappaint.StyleSetting.StyleSettingGroup;
     52import org.openstreetmap.josm.gui.mappaint.StyleSettingFactory;
    5353import org.openstreetmap.josm.gui.mappaint.StyleSource;
    5454import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.KeyCondition;
     
    624624            }
    625625            Cascade c = e.getValue();
    626             String type = c.get("type", null, String.class);
    627             StyleSetting set = null;
    628             if ("boolean".equals(type)) {
    629                 set = BooleanStyleSetting.create(c, this, e.getKey());
    630             } else {
    631                 Logging.warn("Unknown setting type: {0}", type);
    632             }
     626            StyleSetting set = StyleSettingFactory.create(c, this, e.getKey());
    633627            if (set != null) {
    634628                settings.add(set);
     
    636630                String groupId = c.get("group", null, String.class);
    637631                if (groupId != null) {
    638                     settingGroups.get(settingGroups.keySet().stream().filter(g -> g.key.equals(groupId)).findAny()
    639                             .orElseThrow(() -> new IllegalArgumentException("Unknown settings group: " + groupId))).add(set);
     632                    final StyleSettingGroup group = settingGroups.keySet().stream()
     633                            .filter(g -> g.key.equals(groupId))
     634                            .findAny()
     635                            .orElseThrow(() -> new IllegalArgumentException("Unknown settings group: " + groupId));
     636                    settingGroups.get(group).add(set);
    640637                }
    641638            }
  • trunk/test/performance/org/openstreetmap/josm/gui/mappaint/MapRendererPerformanceTest.java

    r14120 r15731  
    1919import javax.imageio.ImageIO;
    2020
     21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2122import org.junit.AfterClass;
    2223import org.junit.Assert;
     
    4849import org.openstreetmap.josm.tools.Logging;
    4950
    50 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    51 
    5251/**
    5352 * Performance test of map renderer.
     
    117116        for (StyleSetting set : filterStyle.settings) {
    118117            BooleanStyleSetting bset = (BooleanStyleSetting) set;
    119             String prefKey = bset.prefKey;
     118            String prefKey = bset.getKey();
    120119            boolean found = false;
    121120            for (Feature f : Feature.values()) {
     
    143142            if (set instanceof BooleanStyleSetting) {
    144143                BooleanStyleSetting bset = (BooleanStyleSetting) set;
    145                 if (bset.prefKey.endsWith(":hide_icons")) {
     144                if (bset.getKey().endsWith(":hide_icons")) {
    146145                    hideIconsSetting = bset;
    147146                }
Note: See TracChangeset for help on using the changeset viewer.