Class Preferences

All Implemented Interfaces:
IPreferences

public class Preferences extends AbstractPreferences
This class holds all preferences for JOSM. Other classes can register their beloved properties here. All properties will be saved upon set-access. Each property is a key=setting pair, where key is a String and setting can be one of 4 types: string, list, list of lists and list of maps. In addition, each key has a unique default value that is set when the value is first accessed using one of the get...() methods. You can use the same preference key in different parts of the code, but the default value must be the same everywhere. A default value of null means, the setting has been requested, but no default value was set. This is used in advanced preferences to present a list off all possible settings. At the moment, you cannot put the empty string for string properties. put(key, "") means, the property is removed.
Since:
74
  • Field Details

  • Constructor Details

    • Preferences

      public Preferences()
      Constructs a new Preferences.
    • Preferences

      Constructs a new Preferences.
      Parameters:
      dirs - the directories to use for saving the preferences
    • Preferences

      public Preferences(Preferences pref)
      Constructs a new Preferences from an existing instance.
      Parameters:
      pref - existing preferences to copy
      Since:
      12634
  • Method Details

    • getUpdatePrefKeys

      private static Map<String,String> getUpdatePrefKeys()
    • main

      public static Preferences main()
      Returns the main (default) preferences instance.
      Returns:
      the main (default) preferences instance
      Since:
      14149
    • addPreferenceChangeListener

      Adds a new preferences listener.
      Parameters:
      listener - The listener to add
      Since:
      12881
    • removePreferenceChangeListener

      Removes a preferences listener.
      Parameters:
      listener - The listener to remove
      Since:
      12881
    • addKeyPreferenceChangeListener

      Adds a listener that only listens to changes in one preference
      Parameters:
      key - The preference key to listen to
      listener - The listener to add.
      Since:
      12881
    • addWeakKeyPreferenceChangeListener

      Adds a weak listener that only listens to changes in one preference
      Parameters:
      key - The preference key to listen to
      listener - The listener to add.
      Since:
      10824
    • listenersForKey

    • removeKeyPreferenceChangeListener

      Removes a listener that only listens to changes in one preference
      Parameters:
      key - The preference key to listen to
      listener - The listener to add.
      Since:
      12881
    • firePreferenceChanged

      protected void firePreferenceChanged(String key, Setting<?> oldValue, Setting<?> newValue)
    • getJOSMDirectoryBaseName

      Get the base name of the JOSM directories for preferences, cache and user data. Default value is "JOSM", unless overridden by system property "josm.dir.name".
      Returns:
      the base name of the JOSM directories for preferences, cache and user data
    • getDirs

      Get the base directories associated with this preference instance.
      Returns:
      the base directories
    • getPreferenceFile

      Returns the user preferences file (preferences.xml).
      Returns:
      The user preferences file (preferences.xml)
    • getDefaultsCacheFile

      Returns the cache file for default preferences.
      Returns:
      the cache file for default preferences
    • getPluginsDirectory

      Returns the user plugin directory.
      Returns:
      The user plugin directory
    • addPossibleResourceDir

      private static void addPossibleResourceDir(Set<String> locations, String s)
    • getAllPossiblePreferenceDirs

      Returns a set of all existing directories where resources could be stored.
      Returns:
      A set of all existing directories where resources could be stored.
    • getAllNamedColors

      Get all named colors, including customized and the default ones.
      Returns:
      a map of all named colors (maps preference key to ColorInfo)
    • save

      public void save() throws IOException
      Called after every put. In case of a problem, do nothing but output the error in log.
      Throws:
      IOException - if any I/O error occurs
    • saveDefaults

      public void saveDefaults() throws IOException
      Stores the defaults to the defaults file
      Throws:
      IOException - If the file could not be saved
    • save

      protected void save(File prefFile, Stream<Map.Entry<String,Setting<?>>> settings, boolean defaults) throws IOException
      Throws:
      IOException
    • checkFileValidity

      private static void checkFileValidity(File file, boolean defaults, Preferences.ThrowingConsumer<File,IOException> consumer) throws IOException
      Ensure that a preferences file is "ok" before copying/moving it over another preferences file
      Parameters:
      file - The file to check
      defaults - true if the file is the cache file for default preferences
      consumer - The consumer that will perform the copy/move action
      Throws:
      IOException - If there is an issue reading/writing the file
    • setCorrectPermissions

      private static void setCorrectPermissions(File file)
    • load

      protected void load() throws IOException, SAXException, XMLStreamException
      Loads preferences from settings file.
      Throws:
      IOException - if any I/O error occurs while reading the file
      SAXException - if the settings file does not contain valid XML
      XMLStreamException - if an XML error occurs while parsing the file (after validation)
    • loadDefaults

      Loads default preferences from default settings cache file. Discards entries older than MAX_AGE_DEFAULT_PREFERENCES.
      Throws:
      IOException - if any I/O error occurs while reading the file
      SAXException - if the settings file does not contain valid XML
      XMLStreamException - if an XML error occurs while parsing the file (after validation)
    • fromXML

      public void fromXML(Reader in) throws XMLStreamException, IOException
      Loads preferences from XML reader.
      Parameters:
      in - XML reader
      Throws:
      XMLStreamException - if any XML stream error occurs
      IOException - if any I/O error occurs
    • init

      public void init(boolean reset)
      Initializes preferences.
      Parameters:
      reset - if true, current settings file is replaced by the default one
    • resetToInitialState

      public void resetToInitialState()
      Resets the preferences to their initial state. This resets all values and file associations. The default values and listeners are not removed.

      It is meant to be called before init(boolean)

      Since:
      10876
    • resetToDefault

      public final void resetToDefault()
      Reset all values stored in this map to the default values. This clears the preferences.
    • putSetting

      public boolean putSetting(String key, Setting<?> setting)
      Set a value for a certain setting. The changed setting is saved to the preference file immediately. Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
      Specified by:
      putSetting in class AbstractPreferences
      Parameters:
      key - the unique identifier for the setting
      setting - the value of the setting. In case it is null, the key-value entry will be removed.
      Returns:
      true, if something has changed (i.e. value is different than before)
    • getSetting

      public Setting<?> getSetting(String key, Setting<?> def)
      Get a setting of any type
      Parameters:
      key - The key for the setting
      def - The default value to use if it was not found
      Returns:
      The setting
    • getSetting

      public <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass)
      Get settings value for a certain key and provide default a value.
      Specified by:
      getSetting in class AbstractPreferences
      Type Parameters:
      T - the setting type
      Parameters:
      key - the identifier for the setting
      def - the default value. For each call of getSetting() with a given key, the default value must be the same. def must not be null, but the value of def can be null.
      klass - the setting type (same as T)
      Returns:
      the corresponding value if the property has been set before, def otherwise
    • getKeySet

      public Set<String> getKeySet()
      Description copied from interface: IPreferences
      Get the set of all keys that are mapped to a value in this preferences.
      Returns:
      the set of all keys
    • getAllSettings

      Description copied from class: AbstractPreferences
      Gets a map of all settings that are currently stored
      Specified by:
      getAllSettings in class AbstractPreferences
      Returns:
      The settings
    • getAllDefaults

      Gets a map of all currently known defaults
      Returns:
      The map (key/setting)
    • getPluginSites

      Replies the collection of plugin site URLs from where plugin lists can be downloaded.
      Returns:
      the collection of plugin site URLs
      See Also:
    • getOnlinePluginSites

      Returns the list of plugin sites available according to offline mode settings.
      Returns:
      the list of available plugin sites
      Since:
      8471
    • setPluginSites

      public void setPluginSites(Collection<String> sites)
      Sets the collection of plugin site URLs.
      Parameters:
      sites - the site URLs
    • toXML

      public String toXML(boolean nopass)
      Returns XML describing these preferences.
      Parameters:
      nopass - if password must be excluded
      Returns:
      XML
    • toXML

      public String toXML(Collection<Map.Entry<String,Setting<?>>> settings, boolean nopass, boolean defaults)
      Returns XML describing the given preferences.
      Parameters:
      settings - preferences settings
      nopass - if password must be excluded
      defaults - true, if default values are converted to XML, false for regular preferences
      Returns:
      XML
    • removeAndUpdateObsolete

      private void removeAndUpdateObsolete(int loadedVersion)
      Removes and updates obsolete preference settings. If you throw out a once-used preference setting, add it to the list here with an expiry date (written as comment). If you see something with an expiry date in the past, remove it from the list.
      Parameters:
      loadedVersion - JOSM version when the preferences file was written
    • updateMapPaintKnownDefaults

      Update the known defaults for the map paintstyles. This should be removed sometime after 2024-06-01.
    • enableSaveOnPut

      public final void enableSaveOnPut(boolean enable)
      Enables or not the preferences file auto-save mechanism (save each time a setting is changed). This behaviour is enabled by default.
      Parameters:
      enable - if true, makes JOSM save preferences file each time a setting is changed
      Since:
      7085