Chapter 12. Preferences

Table of Contents

Preferences
PreferenceDialog
A Basic Set
Setting up a preferences.xml file
Application Integration
Initialization
Registering Listeners
Saving
Preference Utilities

Almost all but the smallest applications have a set of values that affect how the application behaves at runtime. In order for these values to persist across application starts. Preferences of a Tensegrity API based Application are made persistent in a XML file; The set of persistent values are defined in a XML file.

Preferences

A Preferences object is useful when it comes to managing application-wide configuration settings and making them persistent. Upon instantiation, these configuration settings are loaded from an existing preferences.xml file located in the dir directory.

You may provide a default preference file so that configuration settings can be restored across application restarts. In doing so, you will have runtime access to a read-only Preference object, helpful in situations where you do not have access to the file system, such as in an applet.

To use preferences within your application follow these steps:

  • Create a new / copy an existing "preferences.xml".(The Skeleton Framework contains an example file).

  • Instantiate your preferences with a call to the static method createPreferences(String, Class, String) of class PreferenceFactory.

  • Register PreferenceListener to get notified about changes.

  • On application exit save your settings with a call to method flush()

To access the key and values of a Preferences instance, the following methods are available:

  • boolean containsKey(String)

    Returns true if the preferences contain the specified key.

  • String get(String, String)

    Returns the String corresponding to the given key.

  • String keys()

    Returns all keys specified in the preference XML-file.

  • void put(String, String)

    Associates the key key to the given String.

Note

There are convenience methods for every primitive Java® type defined in interface Preferences, for example: putBoolean() and getInt().

PreferenceDialog

This dialog offers a view on the attributes stored in a Preferences object. In other words, this dialog reflects the structure of the used xml file.

Figure 12.1. Screenshot PreferenceDialog

Screenshot PreferenceDialog

A Basic Set

If your Skeleton Framework-based application simply needs common persistent settings, have a look at the basic set and determine if it fits your needs. The following list reflects the structure of file defaultprefs.xml located in the com.tensegrity.skeleton.resource.xml package.

  • Setttings:

    • Language: German or English

    • Number of undo/redo steps

    • Look and Feel: Metal, Tensegrity, Motif, Frog, Windows

    • Show Tooltips

    • Restore frame size

    • Restore container state

  • Files and Folders:

    • Last used directory

    • Size of recently used documents list

    • Save preview

  • Render Settings:

    • Suppress details for selection

    • Suppress all details

    • Suppress selection markers

    • Suppress text

  • Ruler and Grid

    • Ruler Metric: mm, cm, inch, intern

    • Grid Metric: mm, cm, inch, intern

    • Grid Style: Line, Point

    • Subdivisions: 1 - 9

    • Snap Strength: 25%, 50%, 100%

    • Grid and Subdivision stroke

If you are not satisfied with the default Preferences configuration provided by the Skeleton Framework, the following sections will provide a step-by-step guide to configuring customized ones.