Changeset 12891 in josm for trunk


Ignore:
Timestamp:
2017-09-23T23:20:03+02:00 (2 years ago)
Author:
bastiK
Message:

see #15229 - move non-essential helper methods from Preferences to PreferencesUtils

Location:
trunk/src/org/openstreetmap/josm
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java

    r12846 r12891  
    3232
    3333import org.openstreetmap.josm.Main;
     34import org.openstreetmap.josm.data.PreferencesUtils;
    3435import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    3536import org.openstreetmap.josm.gui.MainApplication;
     
    336337                fileHistory.removeAll(failedAll);
    337338                int maxsize = Math.max(0, Config.getPref().getInt("file-open.history.max-size", 15));
    338                 Main.pref.putCollectionBounded("file-open.history", maxsize, fileHistory);
     339                PreferencesUtils.putCollectionBounded(Config.getPref(), "file-open.history", maxsize, fileHistory);
    339340            }
    340341        }
  • trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java

    r12846 r12891  
    1616
    1717import org.openstreetmap.josm.Main;
     18import org.openstreetmap.josm.data.PreferencesUtils;
    1819import org.openstreetmap.josm.gui.ExtendedDialog;
    1920import org.openstreetmap.josm.gui.io.importexport.FileExporter;
     
    235236        history.remove(filepath);
    236237        history.add(0, filepath);
    237         Main.pref.putCollectionBounded("file-open.history", maxsize, history);
     238        PreferencesUtils.putCollectionBounded(Config.getPref(), "file-open.history", maxsize, history);
    238239    }
    239240}
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r12885 r12891  
    660660     * @param def The default value
    661661     * @return The boolean value or the default value if it could not be parsed
    662      */
     662     * @deprecated use {@link PreferencesUtils#getBoolean(IPreferences, String, String, boolean)}
     663     */
     664    @Deprecated
    663665    public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
    664666        boolean generic = getBoolean(key, def);
     
    10761078     * @param def The default value
    10771079     * @return The integer value or the default value if it could not be parsed
    1078      */
     1080     * @deprecated use {@link PreferencesUtils#getInteger(IPreferences, String, String, int)
     1081     */
     1082    @Deprecated
    10791083    public synchronized int getInteger(String key, String specName, int def) {
    10801084        String v = get(key+'.'+specName);
     
    11221126     * @param value The value that should be removed in the collection
    11231127     * @see #getList(String)
    1124      */
     1128     * @deprecated use {@link PreferencesUtils#removeFromCollection(IPreferences, String, String)}
     1129     */
     1130    @Deprecated
    11251131    public synchronized void removeFromCollection(String key, String value) {
    11261132        List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));
     
    12301236     * @param val value
    12311237     * @return {@code true}, if something has changed (i.e. value is different than before)
    1232      */
     1238     * @deprecated use {@link PreferencesUtils#putCollectionBounded(IPreferences, String, int, Collection)}
     1239     */
     1240    @Deprecated
    12331241    public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
    12341242        List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
     
    12481256     * @param def default array value
    12491257     * @return array value
    1250      */
     1258     * @deprecated use {@link #getListOfLists(java.lang.String, java.util.List)}
     1259     */
     1260    @Deprecated
    12511261    @SuppressWarnings({ "unchecked", "rawtypes" })
    12521262    public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
  • trunk/src/org/openstreetmap/josm/data/PreferencesUtils.java

    r12881 r12891  
    66import java.util.ArrayList;
    77import java.util.Collection;
     8import java.util.Collections;
    89import java.util.HashMap;
    910import java.util.Iterator;
     
    1819
    1920import org.openstreetmap.josm.Main;
     21import org.openstreetmap.josm.spi.preferences.IPreferences;
    2022import org.openstreetmap.josm.spi.preferences.ListListSetting;
    2123import org.openstreetmap.josm.spi.preferences.ListSetting;
     
    476478        engine.eval(init);
    477479    }
     480
     481    /**
     482     * Gets an boolean that may be specialized
     483     * @param prefs the preferences
     484     * @param key The basic key
     485     * @param specName The sub-key to append to the key
     486     * @param def The default value
     487     * @return The boolean value or the default value if it could not be parsed
     488     * @since 12891
     489     */
     490    public static boolean getBoolean(IPreferences prefs, final String key, final String specName, final boolean def) {
     491        synchronized (prefs) {
     492            boolean generic = prefs.getBoolean(key, def);
     493            String skey = key+'.'+specName;
     494            String svalue = prefs.get(skey, null);
     495            if (svalue != null)
     496                return Boolean.parseBoolean(svalue);
     497            else
     498                return generic;
     499        }
     500    }
     501
     502    /**
     503     * Gets an integer that may be specialized
     504     * @param prefs the preferences
     505     * @param key The basic key
     506     * @param specName The sub-key to append to the key
     507     * @param def The default value
     508     * @return The integer value or the default value if it could not be parsed
     509     * @since 12891
     510     */
     511    public static int getInteger(IPreferences prefs, String key, String specName, int def) {
     512        synchronized (prefs) {
     513            String v = prefs.get(key+'.'+specName);
     514            if (v.isEmpty())
     515                v = prefs.get(key, Integer.toString(def));
     516            if (v.isEmpty())
     517                return def;
     518
     519            try {
     520                return Integer.parseInt(v);
     521            } catch (NumberFormatException e) {
     522                // fall out
     523                Logging.trace(e);
     524            }
     525            return def;
     526        }
     527    }
     528
     529    /**
     530     * Removes a value from a given String collection
     531     * @param prefs the preferences
     532     * @param key The preference key the collection is stored with
     533     * @param value The value that should be removed in the collection
     534     * @see #getList(String)
     535     * @since 12891
     536     */
     537    public static void removeFromCollection(IPreferences prefs, String key, String value) {
     538        synchronized (prefs) {
     539            List<String> a = new ArrayList<>(prefs.getList(key, Collections.<String>emptyList()));
     540            a.remove(value);
     541            prefs.putList(key, a);
     542        }
     543    }
     544
     545    /**
     546     * Saves at most {@code maxsize} items of collection {@code val}.
     547     * @param prefs the preferences
     548     * @param key key
     549     * @param maxsize max number of items to save
     550     * @param val value
     551     * @return {@code true}, if something has changed (i.e. value is different than before)
     552     * @since 12891
     553     */
     554    public static boolean putCollectionBounded(IPreferences prefs, String key, int maxsize, Collection<String> val) {
     555        List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
     556        for (String i : val) {
     557            if (newCollection.size() >= maxsize) {
     558                break;
     559            }
     560            newCollection.add(i);
     561        }
     562        return prefs.putList(key, newCollection);
     563    }
     564
    478565}
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r12846 r12891  
    3333import javax.swing.ImageIcon;
    3434
    35 import org.openstreetmap.josm.Main;
    3635import org.openstreetmap.josm.data.Bounds;
     36import org.openstreetmap.josm.data.PreferencesUtils;
    3737import org.openstreetmap.josm.data.SystemOfMeasurement;
    3838import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
     
    279279    public ColorMode getColorMode(String layerName) {
    280280        try {
    281             int i = Main.pref.getInteger("draw.rawgps.colors", specName(layerName), 0);
     281            int i = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colors", specName(layerName), 0);
    282282            return ColorMode.fromIndex(i);
    283283        } catch (IndexOutOfBoundsException e) {
     
    300300    public void readPreferences(String layerName) {
    301301        String spec = specName(layerName);
    302         forceLines = Main.pref.getBoolean("draw.rawgps.lines.force", spec, false);
    303         direction = Main.pref.getBoolean("draw.rawgps.direction", spec, false);
    304         lineWidth = Main.pref.getInteger("draw.rawgps.linewidth", spec, 0);
    305         alphaLines = Main.pref.getBoolean("draw.rawgps.lines.alpha-blend", spec, false);
     302        forceLines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.force", spec, false);
     303        direction = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.direction", spec, false);
     304        lineWidth = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.linewidth", spec, 0);
     305        alphaLines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.alpha-blend", spec, false);
    306306
    307307        if (!data.fromServer) {
    308             maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length.local", spec, -1);
    309             lines = Main.pref.getBoolean("draw.rawgps.lines.local", spec, true);
     308            maxLineLength = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.max-line-length.local", spec, -1);
     309            lines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.local", spec, true);
    310310        } else {
    311             maxLineLength = Main.pref.getInteger("draw.rawgps.max-line-length", spec, 200);
    312             lines = Main.pref.getBoolean("draw.rawgps.lines", spec, true);
    313         }
    314         large = Main.pref.getBoolean("draw.rawgps.large", spec, false);
    315         largesize = Main.pref.getInteger("draw.rawgps.large.size", spec, 3);
    316         hdopCircle = Main.pref.getBoolean("draw.rawgps.hdopcircle", spec, false);
     311            maxLineLength = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.max-line-length", spec, 200);
     312            lines = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines", spec, true);
     313        }
     314        large = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.large", spec, false);
     315        largesize = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.large.size", spec, 3);
     316        hdopCircle = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.hdopcircle", spec, false);
    317317        colored = getColorMode(layerName);
    318         alternateDirection = Main.pref.getBoolean("draw.rawgps.alternatedirection", spec, false);
    319         delta = Main.pref.getInteger("draw.rawgps.min-arrow-distance", spec, 40);
    320         colorTracksTune = Main.pref.getInteger("draw.rawgps.colorTracksTune", spec, 45);
    321         colorModeDynamic = Main.pref.getBoolean("draw.rawgps.colors.dynamic", spec, false);
     318        alternateDirection = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.alternatedirection", spec, false);
     319        delta = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.min-arrow-distance", spec, 40);
     320        colorTracksTune = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colorTracksTune", spec, 45);
     321        colorModeDynamic = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.colors.dynamic", spec, false);
    322322        /* good HDOP's are between 1 and 3, very bad HDOP's go into 3 digit values */
    323323        hdoprange = Config.getPref().getInt("hdop.range", 7);
     
    326326
    327327        // get heatmap parameters
    328         heatMapEnabled = Main.pref.getBoolean("draw.rawgps.heatmap.enabled", spec, false);
    329         heatMapDrawExtraLine = Main.pref.getBoolean("draw.rawgps.heatmap.line-extra", spec, false);
    330         heatMapDrawColorTableIdx = Main.pref.getInteger("draw.rawgps.heatmap.colormap", spec, 0);
    331         heatMapDrawPointMode = Main.pref.getBoolean("draw.rawgps.heatmap.use-points", spec, false);
    332         heatMapDrawGain = Main.pref.getInteger("draw.rawgps.heatmap.gain", spec, 0);
    333         heatMapDrawLowerLimit = Main.pref.getInteger("draw.rawgps.heatmap.lower-limit", spec, 0);
     328        heatMapEnabled = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.enabled", spec, false);
     329        heatMapDrawExtraLine = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.line-extra", spec, false);
     330        heatMapDrawColorTableIdx = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.colormap", spec, 0);
     331        heatMapDrawPointMode = PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.heatmap.use-points", spec, false);
     332        heatMapDrawGain = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.gain", spec, 0);
     333        heatMapDrawLowerLimit = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.heatmap.lower-limit", spec, 0);
    334334
    335335        // shrink to range
  • trunk/src/org/openstreetmap/josm/gui/preferences/display/GPXSettingsPanel.java

    r12846 r12891  
    2525import org.openstreetmap.josm.Main;
    2626import org.openstreetmap.josm.actions.ExpertToggleAction;
     27import org.openstreetmap.josm.data.PreferencesUtils;
    2728import org.openstreetmap.josm.gui.layer.gpx.GpxDrawHelper;
    2829import org.openstreetmap.josm.gui.layer.markerlayer.Marker;
     
    392393            drawRawGpsLinesGlobal.setSelected(true);
    393394        } else {
    394             Boolean lf = Main.pref.getBoolean("draw.rawgps.lines.local", layerName, true);
    395             if (Main.pref.getBoolean("draw.rawgps.lines", layerName, true)) {
     395            Boolean lf =  PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines.local", layerName, true);
     396            if (PreferencesUtils.getBoolean(Config.getPref(), "draw.rawgps.lines", layerName, true)) {
    396397                drawRawGpsLinesAll.setSelected(true);
    397398            } else if (lf) {
     
    402403        }
    403404
    404         drawRawGpsMaxLineLengthLocal.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length.local", layerName, -1)));
    405         drawRawGpsMaxLineLength.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.max-line-length", layerName, 200)));
    406         drawLineWidth.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.linewidth", layerName, 0)));
    407         drawLineWithAlpha.setSelected(Main.pref.getBoolean("draw.rawgps.lines.alpha-blend", layerName, false));
    408         forceRawGpsLines.setSelected(Main.pref.getBoolean("draw.rawgps.lines.force", layerName, false));
    409         drawGpsArrows.setSelected(Main.pref.getBoolean("draw.rawgps.direction", layerName, false));
    410         drawGpsArrowsFast.setSelected(Main.pref.getBoolean("draw.rawgps.alternatedirection", layerName, false));
    411         drawGpsArrowsMinDist.setText(Integer.toString(Main.pref.getInteger("draw.rawgps.min-arrow-distance", layerName, 40)));
    412         hdopCircleGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.hdopcircle", layerName, false));
    413         largeGpsPoints.setSelected(Main.pref.getBoolean("draw.rawgps.large", layerName, false));
     405        drawRawGpsMaxLineLengthLocal.setText(Integer.toString(PreferencesUtils.getInteger(Config.getPref(),
     406                "draw.rawgps.max-line-length.local", layerName, -1)));
     407        drawRawGpsMaxLineLength.setText(Integer.toString(PreferencesUtils.getInteger(Config.getPref(),
     408                "draw.rawgps.max-line-length", layerName, 200)));
     409        drawLineWidth.setText(Integer.toString(PreferencesUtils.getInteger(Config.getPref(),
     410                "draw.rawgps.linewidth", layerName, 0)));
     411        drawLineWithAlpha.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     412                "draw.rawgps.lines.alpha-blend", layerName, false));
     413        forceRawGpsLines.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     414                "draw.rawgps.lines.force", layerName, false));
     415        drawGpsArrows.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     416                "draw.rawgps.direction", layerName, false));
     417        drawGpsArrowsFast.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     418                "draw.rawgps.alternatedirection", layerName, false));
     419        drawGpsArrowsMinDist.setText(Integer.toString(PreferencesUtils.getInteger(Config.getPref(),
     420                "draw.rawgps.min-arrow-distance", layerName, 40)));
     421        hdopCircleGpsPoints.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     422                "draw.rawgps.hdopcircle", layerName, false));
     423        largeGpsPoints.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     424                "draw.rawgps.large", layerName, false));
    414425        useGpsAntialiasing.setSelected(Config.getPref().getBoolean("mappaint.gpx.use-antialiasing", false));
    415426
     
    424435            colorTypeHeatMapLowerLimit.setValue(0);
    425436        } else {
    426             int colorType = Main.pref.getInteger("draw.rawgps.colors", layerName, 0);
     437            int colorType = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colors", layerName, 0);
    427438            switch (colorType) {
    428439            case 0: colorTypeNone.setSelected(true); break;
     
    434445            default: Logging.warn("Unknown color type: " + colorType);
    435446            }
    436             int ccts = Main.pref.getInteger("draw.rawgps.colorTracksTune", layerName, 45);
     447            int ccts = PreferencesUtils.getInteger(Config.getPref(), "draw.rawgps.colorTracksTune", layerName, 45);
    437448            colorTypeVelocityTune.setSelectedIndex(ccts == 10 ? 2 : (ccts == 20 ? 1 : 0));
    438             colorTypeHeatMapTune.setSelectedIndex(Main.pref.getInteger("draw.rawgps.heatmap.colormap", layerName, 0));
    439             colorDynamic.setSelected(Main.pref.getBoolean("draw.rawgps.colors.dynamic", layerName, false));
    440             colorTypeHeatMapPoints.setSelected(Main.pref.getBoolean("draw.rawgps.heatmap.use-points", layerName, false));
    441             colorTypeHeatMapGain.setValue(Main.pref.getInteger("draw.rawgps.heatmap.gain", layerName, 0));
    442             colorTypeHeatMapLowerLimit.setValue(Main.pref.getInteger("draw.rawgps.heatmap.lower-limit", layerName, 0));
     449            colorTypeHeatMapTune.setSelectedIndex(PreferencesUtils.getInteger(Config.getPref(),
     450                    "draw.rawgps.heatmap.colormap", layerName, 0));
     451            colorDynamic.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     452                    "draw.rawgps.colors.dynamic", layerName, false));
     453            colorTypeHeatMapPoints.setSelected(PreferencesUtils.getBoolean(Config.getPref(),
     454                    "draw.rawgps.heatmap.use-points", layerName, false));
     455            colorTypeHeatMapGain.setValue(PreferencesUtils.getInteger(Config.getPref(),
     456                    "draw.rawgps.heatmap.gain", layerName, 0));
     457            colorTypeHeatMapLowerLimit.setValue(PreferencesUtils.getInteger(Config.getPref(),
     458                    "draw.rawgps.heatmap.lower-limit", layerName, 0));
    443459        }
    444460    }
  • trunk/src/org/openstreetmap/josm/io/UploadStrategy.java

    r12846 r12891  
    66import java.util.Locale;
    77
    8 import org.openstreetmap.josm.Main;
     8import org.openstreetmap.josm.data.PreferencesUtils;
    99import org.openstreetmap.josm.spi.preferences.Config;
    1010import org.openstreetmap.josm.tools.Logging;
     
    8585            v = Config.getPref().get("osm-server.atomic-upload", null);
    8686            if (v != null) {
    87                 Main.pref.removeFromCollection("osm-server.atomic-upload", v);
     87                PreferencesUtils.removeFromCollection(Config.getPref(), "osm-server.atomic-upload", v);
    8888            } else {
    8989                v = "";
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r12873 r12891  
    5454import org.openstreetmap.josm.Main;
    5555import org.openstreetmap.josm.actions.RestartAction;
     56import org.openstreetmap.josm.data.PreferencesUtils;
    5657import org.openstreetmap.josm.data.Version;
    5758import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     
    348349            if (plugins.contains(depr.name)) {
    349350                plugins.remove(depr.name);
    350                 Main.pref.removeFromCollection("plugins", depr.name);
     351                PreferencesUtils.removeFromCollection(Config.getPref(), "plugins", depr.name);
    351352                removedPlugins.add(depr);
    352353            }
     
    402403                    Utils.escapeReservedCharactersHTML(unmaintained));
    403404            if (confirmDisablePlugin(parent, msg, unmaintained)) {
    404                 Main.pref.removeFromCollection("plugins", unmaintained);
     405                PreferencesUtils.removeFromCollection(Config.getPref(), "plugins", unmaintained);
    405406                plugins.remove(unmaintained);
    406407            }
     
    800801        }
    801802        if (msg != null && confirmDisablePlugin(parent, msg, plugin.name)) {
    802             Main.pref.removeFromCollection("plugins", plugin.name);
     803            PreferencesUtils.removeFromCollection(Config.getPref(), "plugins", plugin.name);
    803804        }
    804805    }
Note: See TracChangeset for help on using the changeset viewer.