Class Preferences

java.lang.Object
org.omegat.util.Preferences

public final class Preferences extends Object
Class to load & save global OmegaT preferences.

Initially this class was implemented with static members and methods directly implementing the interface, backed by XML storage. However, this was bad for testing and extensibility, or allowing different persistence formats.

This class's static methods remain for compatibility, but now they wrap a singleton instance of a concrete implementation of Preferences.IPreferences.

  • Field Details

  • Method Details

    • getPreference

      @Nullable public static @Nullable String getPreference(String key)
      Returns the defaultValue of some preference out of OmegaT's preferences file.

      If the key is not found, returns the empty string.

      Parameters:
      key - key of the key to look up, usually a static string from this class
      Returns:
      preference defaultValue as a string
    • existsPreference

      public static boolean existsPreference(String key)
      Returns true if the preference is in OmegaT's preferences file.

      If the key is not found return false

      Parameters:
      key - key of the key to look up, usually a static string from this class
      Returns:
      true if preferences exists
    • isPreference

      public static boolean isPreference(String key)
      Returns the boolean defaultValue of some preference.

      Returns true if the preference exists and is equal to "true", false otherwise (no such preference, or it's equal to "false", etc).

      Parameters:
      key - preference key, usually a static string from this class
      Returns:
      preference defaultValue as a boolean
    • isPreferenceDefault

      public static boolean isPreferenceDefault(String key, boolean defaultValue)
      Returns the boolean value of some preference out of OmegaT's preferences file, if it exists.

      If the key is not found, returns the default value provided and sets the preference to the default value.

      Parameters:
      key - name of the key to look up, usually a static string from this class
      defaultValue - default value for the key
      Returns:
      preference value as an boolean
    • getPreferenceDefault

      public static String getPreferenceDefault(String key, String defaultValue)
      Returns the value of some preference out of OmegaT's preferences file, if it exists.

      If the key is not found, returns the default value provided and sets the preference to the default value.

      Parameters:
      key - name of the key to look up, usually a static string from this class
      defaultValue - default value for the key
      Returns:
      preference value as a string
    • getPreferenceEnumDefault

      public static <T extends Enum<T>> T getPreferenceEnumDefault(String key, T defaultValue)
      Returns the value of some preference out of OmegaT's preferences file, if it exists.

      If the key is not found, returns the default value provided and sets the preference to the default value.

      Parameters:
      key - name of the key to look up, usually a static string from this class
      defaultValue - default value for the key
      Returns:
      preference value as enum
    • getPreferenceDefault

      public static int getPreferenceDefault(String key, int defaultValue)
      Returns the integer value of some preference out of OmegaT's preferences file, if it exists.

      If the key is not found, returns the default value provided and sets the preference to the default value.

      Parameters:
      key - name of the key to look up, usually a static string from this class
      defaultValue - default value for the key
      Returns:
      preference value as an integer
    • setPreference

      public static void setPreference(String name, Object value)
      Sets the value of some preference. The value will be persisted to disk as XML, serialized via value.toString().
      Parameters:
      name - preference key name, usually Preferences.PREF_...
      value - preference value as an object
    • addPropertyChangeListener

      public static void addPropertyChangeListener(PropertyChangeListener listener)
      Register to receive notifications when preferences change.

      Note: The value returned by PropertyChangeEvent#getNewValue() will be of the "correct" type (Integer, Boolean, Enum, etc.) but the value returned by PropertyChangeEvent#getOldValue() will be the String equivalent for storing in XML.

    • addPropertyChangeListener

      public static void addPropertyChangeListener(String property, PropertyChangeListener listener)
      Register to receive notifications when the specified preference changes.

      Note: The value returned by getNewValue() will be of the "correct" type (Integer, Boolean, Enum, etc.) but the value returned by getOldValue() will be the String equivalent for storing in XML.

    • setFilters

      public static void setFilters(Filters newFilters)
    • getFilters

      public static Filters getFilters()
    • setSRX

      public static void setSRX(SRX newSrx)
    • getSRX

      public static SRX getSRX()
    • save

      public static void save()
    • isFirstRun

      public static boolean isFirstRun()
      Returns true if this looks like the first run (no existing omegat.prefs when initialized). If the underlying persistence does not provide the information, returns false.
    • init

      public static void init()
      Initialize the preferences system. This will load
      • general user prefs
      • user filter settings
      • user segmentation settings
      from existing files in StaticUtils.getConfigDir() (and others for general prefs; see getPreferencesFile()) and set things up to create them via save() if they don't yet exist.

      When the preferences system is required but actual user preferences shouldn't be loaded or altered (testing scenarios), use org.omegat.util.TestPreferencesInitializer methods or be sure to set the config dir with RuntimePreferences.setConfigDir(String) before calling this method.

    • initFilters

      public static void initFilters()
    • initSegmentation

      public static void initSegmentation()