Changeset 16590 in josm


Ignore:
Timestamp:
2020-06-10T08:04:42+02:00 (2 months ago)
Author:
simon04
Message:

fix #19197 - MapCSS JOSM_pref: check if a pref could be converted to a color instead of a string (patch by taylor.smock, modified)

Location:
trunk
Files:
3 edited

Legend:

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

    r15719 r16590  
    2121import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
    2222import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
     23import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    2324import org.openstreetmap.josm.gui.MainApplication;
    2425import org.openstreetmap.josm.gui.NavigatableComponent;
     
    4041import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
    4142import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
     43import org.openstreetmap.josm.tools.ColorHelper;
    4244import org.openstreetmap.josm.tools.Pair;
    4345
     
    604606     * each primitive can be slow during rendering.
    605607     *
     608     * If the default value can be {@linkplain Cascade#convertTo converted} to a {@link Color},
     609     * the {@link NamedColorProperty} is retrieved as string.
     610     *
     611     * @param source style source
    606612     * @param key preference key
    607613     * @param def default value
     
    609615     * @see org.openstreetmap.josm.data.Preferences#get(String, String)
    610616     */
    611     public String getPreferenceCached(String key, String def) {
     617    public String getPreferenceCached(StyleSource source, String key, String def) {
    612618        String res;
    613619        if (preferenceCache.containsKey(key)) {
    614620            res = preferenceCache.get(key);
    615621        } else {
    616             res = Config.getPref().get(key, null);
     622            Color realDef = Cascade.convertTo(def, Color.class);
     623            if (realDef != null) {
     624                String prefName = source != null ? source.getFileNamePart() : "unknown";
     625                NamedColorProperty property = new NamedColorProperty(NamedColorProperty.COLOR_CATEGORY_MAPPAINT, prefName, key, realDef);
     626                res = ColorHelper.color2html(property.get());
     627            } else {
     628                res = Config.getPref().get(key, null);
     629            }
    617630            preferenceCache.put(key, res);
    618631        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

    r16553 r16590  
    2828import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
    2929import org.openstreetmap.josm.data.osm.search.SearchParseError;
     30import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    3031import org.openstreetmap.josm.gui.MainApplication;
    3132import org.openstreetmap.josm.gui.mappaint.Cascade;
     
    768769     * Obtains the JOSM'key {@link org.openstreetmap.josm.data.Preferences} string for key {@code key},
    769770     * and defaults to {@code def} if that is null.
     771     *
     772     * If the default value can be {@linkplain Cascade#convertTo converted} to a {@link Color},
     773     * the {@link NamedColorProperty} is retrieved as string.
     774     *
    770775     * @param env the environment
    771776     * @param key Key in JOSM preference
     
    774779     */
    775780    public static String JOSM_pref(Environment env, String key, String def) { // NO_UCD (unused code)
    776         return MapPaintStyles.getStyles().getPreferenceCached(key, def);
     781        return MapPaintStyles.getStyles().getPreferenceCached(env != null ? env.source : null, key, def);
    777782    }
    778783
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java

    r16583 r16590  
    66import static org.openstreetmap.josm.data.osm.OsmPrimitiveType.NODE;
    77
     8import java.util.Collections;
     9
    810import org.junit.Rule;
    911import org.junit.Test;
     
    1214import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1315import org.openstreetmap.josm.data.osm.User;
     16import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1417import org.openstreetmap.josm.gui.mappaint.Environment;
    1518import org.openstreetmap.josm.gui.util.GuiHelper;
     
    113116    public void testPref() {
    114117        String key = "Functions.JOSM_pref";
     118        Config.getPref().put(key, null);
    115119        assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
    116120        Config.getPref().put(key, "baz");
     
    124128        });
    125129        assertEquals("foobar", Functions.JOSM_pref(null, key, "foobar"));
     130        Config.getPref().put(key, null);
    126131    }
     132
     133    /**
     134     * Unit test of {@link Functions#JOSM_pref}, color handling
     135     */
     136    @Test
     137    public void testPrefColor() {
     138        String key = "Functions.JOSM_pref";
     139        String colorKey = NamedColorProperty.NAMED_COLOR_PREFIX + NamedColorProperty.COLOR_CATEGORY_MAPPAINT + ".unknown." + key;
     140        Config.getPref().put(colorKey, null);
     141        assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
     142        Config.getPref().putList(colorKey, Collections.singletonList("#00FF00"));
     143        GuiHelper.runInEDTAndWait(() -> {
     144            // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
     145        });
     146        assertEquals("#00FF00", Functions.JOSM_pref(null, key, "#000000"));
     147        Config.getPref().put(colorKey, null);
     148        GuiHelper.runInEDTAndWait(() -> {
     149            // await org.openstreetmap.josm.gui.mappaint.ElemStyles.clearCached
     150        });
     151        assertEquals("#000000", Functions.JOSM_pref(null, key, "#000000"));
     152        Config.getPref().put(colorKey, null);
     153    }
     154
    127155}
Note: See TracChangeset for help on using the changeset viewer.