Changeset 9114 in josm for trunk/src/org


Ignore:
Timestamp:
2015-12-13T20:28:00+01:00 (4 years ago)
Author:
bastiK
Message:

mapcss: make partial fill threshold an advanced preference option (see #12104)

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
2 edited

Legend:

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

    r9078 r9114  
    66import java.util.Collection;
    77import java.util.Collections;
     8import java.util.HashMap;
    89import java.util.List;
     10import java.util.Map;
    911import java.util.Map.Entry;
    1012
    1113import org.openstreetmap.josm.Main;
     14import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
     15import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
    1216import org.openstreetmap.josm.data.osm.Node;
    1317import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2327import org.openstreetmap.josm.tools.Utils;
    2428
    25 public class ElemStyles {
     29public class ElemStyles implements PreferenceChangedListener {
    2630    private final List<StyleSource> styleSources;
    2731    private boolean drawMultipolygon;
     
    3236    private int defaultNodesIdx, defaultLinesIdx;
    3337
     38    private final Map<String, String> preferenceCache = new HashMap<>();
     39
    3440    /**
    3541     * Constructs a new {@code ElemStyles}.
     
    3743    public ElemStyles() {
    3844        styleSources = new ArrayList<>();
     45        Main.pref.addPreferenceChangeListener(this);
    3946    }
    4047
     
    4855            public void run() {
    4956                cacheIdx++;
     57                preferenceCache.clear();
    5058            }
    5159        });
     
    487495        }
    488496    }
     497
     498    /**
     499     * Looks up a preference value and ensures the style cache is invalidated
     500     * as soon as this preference value is changed by the user.
     501     *
     502     * In addition, it adds an intermediate cache for the preference values,
     503     * as frequent preference lookup (using <code>Main.pref.get()</code>) for
     504     * each primitive can be slow during rendering.
     505     *
     506     * @param key preference key
     507     * @param def default value
     508     * @return the corresponding preference value
     509     * @see org.openstreetmap.josm.data.Preferences#get(String, String)
     510     */
     511    public String getPreferenceCached(String key, String def) {
     512        String res;
     513        if (preferenceCache.containsKey(key)) {
     514            res = preferenceCache.get(key);
     515        } else {
     516            res = Main.pref.get(key, null);
     517            preferenceCache.put(key, res);
     518        }
     519        return res != null ? res : def;
     520    }
     521
     522    @Override
     523    public void preferenceChanged(PreferenceChangeEvent e) {
     524        if (preferenceCache.containsKey(e.getKey())) {
     525            clearCached();
     526        }
     527    }
    489528}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r9078 r9114  
    3030import org.openstreetmap.josm.gui.mappaint.Cascade;
    3131import org.openstreetmap.josm.gui.mappaint.Environment;
     32import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    3233import org.openstreetmap.josm.gui.util.RotationAngle;
    3334import org.openstreetmap.josm.io.XmlWriter;
     
    678679         * @param def Default value
    679680         * @return value for key, or default value if not found
    680          * @see org.openstreetmap.josm.data.Preferences#get(String, String)
    681          */
    682         public static String JOSM_pref(String key, String def) {
    683             String res = Main.pref.get(key, null);
    684             return res != null ? res : def;
     681         */
     682        public static String JOSM_pref(Environment env, String key, String def) {
     683            return MapPaintStyles.getStyles().getPreferenceCached(key, def);
    685684        }
    686685
Note: See TracChangeset for help on using the changeset viewer.