Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

prefs



The prefs package contains additional classes which work with the Java prefs package.

Network preferences

The NetworkPreferences class is an implementation of the Java Preferences class, allowing to use these preferences in a network. These preferences have the following properties:
  • the user preferences are serialized in an xml file, under a directory specific of the application
  • the system preferences are serialized in an xml file
Contrary to the default Java preferences, this ensures that the preferences will follow the "nomad" user[1]
For example, on Windows, the default backing store in the Java preferences implementation is the Windows registry, which is modified even if a custom PreferencesFactory is used
.

Preferences helper

The PreferencesHelper class add a few utility methods to store some types of values in Preferences, not supported directly in the default Java implementation:
  • Boolean arrays
  • Files and arrays of Files (absolute or relative)
  • Lists of numbers
  • Colors
  • Objects. Note that only serializable objects can be used there

Getting a map of values for a preference

The PreferencesHelper.getMap(Preferences, String, Class, Object) method allows to get a Map of values for a preference:
  • The node argument is the key of the Preferences node to use for the map
  • The Class argument is the class to use to convert the String value of the array associated to the node to the appropriate Object
  • The default argument is the default value to use if the String value can not be converted to the appropritate class


For example:
   Map<String, Object> map = PreferencesHelper.getMap(pref, "enabled", Boolean.class, false);
It is possible to get a Map with values of a particular class type by using the PreferencesHelper.getMapForClass(Preferences, String, Class, Object) method:

For example:
  Map<String, Boolean> map = PreferencesHelper.getMapForClass(pref, "enabled", Boolean.class, false);

Putting a map of values for a preference

The PreferencesHelper.putMap(Preferences, String, Map) method allows to put a Map of values for a preference.

For example:
  Map<String, Object> map = ...
  PreferencesHelper.putMap(pref, "enabled", map);
It is possible to put a Map with values of a particular class type by using the PreferencesHelper.putMapForClass(Preferences, String, Map) method:

For example:
  Map<String, Boolean> map = ...
  PreferencesHelper.putMapForClass(pref, "enabled", map);

Notes

  1. ^ For example, on Windows, the default backing store in the Java preferences implementation is the Windows registry, which is modified even if a custom PreferencesFactory is used

Categories: packages | prefs

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences