org.fife.ui.app
Class Prefs

java.lang.Object
  extended by org.fife.ui.app.Prefs
Direct Known Subclasses:
ConsolePrefs, FileSystemTreePrefs, HeapIndicatorPrefs, HtmlOptions, JsonOptions, LangSupportPreferences, MacroPrefs, PluginPrefs, ProjectPluginPrefs, SourceBrowserPrefs, TasksPrefs, ToolsPrefs, XmlOptions

public abstract class Prefs
extends java.lang.Object

A simple wrapper for preferences for some object. This class can be used as a simpler replacement for the Java Preferences API for the following reasons:

  1. No need to call putXXX() methods for each individual preference when saving preferences and getXXX() methods while loading them; all public fields are loaded and stored appropriately automatically.
  2. Uses a simple properties file on all platforms. The standard Preferences API inconveniently hides its data in the registry on Windows.
  3. As opposed to using java.util.Properties directly, using this class keeps you from having to convert each type of preference to and from String. java.awt.Color, for example, is handled automatically.
Subclasses should declare all fields as public. All public instance fields will be saved and loaded in properties file format automatically. While public fields are usually considered bad OO design, since this class is solely used for preferences and should usually only be read and used (once) by a single class, this really isn't such a big deal.

Common usage should be as follows: say a class Foo needs to store preferences between runs of the application. A class could be created for its preferences, say FooPrefs, that extends Prefs. The Foo instance could then loads its preferences like so:

 public void loadPreferences() throws IOException {
    FooPrefs prefs = new FooPrefs(); // Initializes to defaults
    prefs.load(new File((String)System.getProperty("user.home"), ".foo.prefs"));
    this.count = prefs.count;
    this.id = prefs.id;
    this.background = prefs.bgColor;
 }
 
and save its preferences similarly:
 public void savePreferences() throws IOException {
    FooPrefs prefs = new FooPrefs();
    prefs.count = this.count;
    prefs.id = this.id;
    prefs.bgColor = this.background;
    prefs.save(new File((String)System.getProperty("user.home"), ".foo.prefs"));
 }
 
Alternatively (and perhaps more simply), the Foo instance could instantiate and keep the FooPrefs as a private member. Its getters and setters that modify its preferences could manipulate the FooPrefs's values directly. Then at shutdown time, the Foo instance would simply have to call prefs.save(File) to save any changes.

Modification of the generated properties files by hand is discouraged unless you are familiar with the details of that specific concrete Prefs implementation.

This class currently handles fields of type:

Version:
1.0
Author:
Robert Futrell

Constructor Summary
Prefs()
          Constructor.
 
Method Summary
 void load(java.io.File file)
          Loads this preferences class from a file.
 void load(java.io.InputStream in)
          Loads this preferences class from an input stream.
 void load(java.util.Properties props)
          Loads this preferences class from a properties object.
 void save(java.io.File file)
          Saves these preferences to a file.
 void save(java.io.OutputStream out)
          Saves these preferences to an output stream.
 void save(java.util.Properties props)
          Saves these preferences to a properties object.
abstract  void setDefaults()
          Sets all fields in this class to their default values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Prefs

public Prefs()
Constructor. Sets the value of all preferences to their defaults by calling setDefaults().

Method Detail

load

public void load(java.io.File file)
          throws java.io.IOException
Loads this preferences class from a file.

Parameters:
file - The file.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
load(InputStream), load(Properties)

load

public void load(java.io.InputStream in)
          throws java.io.IOException
Loads this preferences class from an input stream.

Parameters:
in - The input stream. It is the caller's responsibility to close this stream.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
load(File), load(Properties)

load

public void load(java.util.Properties props)
          throws java.io.IOException
Loads this preferences class from a properties object. This is useful if you have multiple preferences objects stored in a single instance of Properties.

Parameters:
props - The properties to load from.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
load(File), load(InputStream)

save

public void save(java.io.File file)
          throws java.io.IOException
Saves these preferences to a file.

Parameters:
file - The file to save to.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
save(Properties), save(OutputStream)

save

public void save(java.util.Properties props)
          throws java.io.IOException
Saves these preferences to a properties object. Useful if you want to save multiple instances of Prefs into a single Properties.

Parameters:
props - The properties to save to.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
save(File), save(OutputStream)

save

public void save(java.io.OutputStream out)
          throws java.io.IOException
Saves these preferences to an output stream. The stream will still be open after this call is made.

Parameters:
out - The stream to write to.
Throws:
java.io.IOException - If an IO error occurs.
See Also:
save(File), save(Properties)

setDefaults

public abstract void setDefaults()
Sets all fields in this class to their default values.