Setting up a preferences system
Nowadays, users expect every application to provide a preferences window, in which
the user may replace the default settings by his or her preferred custom ones.
The existing preferences system that is contained in the JDK is not sufficient,
as it can only handle strings, booleans and numbers. SweenJet's preferences system
using the SweenJet property framework in order to set up such a system, as this
tutorial will show.
- Defining the properties
- Creating a preferences pane
- Saving and restoring preferences
Firstly, a class is created that bundles all properties together. This class
consciously violates the rule of encapsulation, but in this case, this approach
makes the code more readable.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.municware.sweenjet.prop.FileProperty;
import org.municware.sweenjet.prop.PropertyBundle;
import org.municware.sweenjet.prop.StringProperty;
public class MyConfig {
private static final MyConfig defaultConfiguration = new MyConfig();
/**
* The default directory denotes the directory that file
* choosers etc. are initially set to. The directory is set
* to system property user.dir by default.
*/
public final FileProperty defaultDirectory;
/**
* Holds the user's name.
*/
public final StringProperty userName;
/**
* Holds the user's mail address.
*/
public final StringProperty userMailAddress;
/**
* Bundles all properties that are defined in this class.
*/
public PropertyBundle allProperties = new PropertyBundle(
"My Config", "mycfg");
/**
* Creates a new instance of MyConfig
*/
public MyConfig() {
this.defaultDirectory = new FileProperty("directory.default", "Default directory",
"File choosers are automatically set to the default directory.",
FileProperty.DIRECTORY, new File(System.getProperty("user.dir")));
allProperties.addProperty(defaultDirectory);
this.userName = new StringProperty("user.name", "Name",
"The user's name.", System.getProperty("user.name"));
allProperties.addProperty(userName);
this.userMailAddress = new StringProperty("user.mail", "E-mail address",
"The user's e-mail address.", "");
allProperties.addProperty(userMailAddress);
}
public void load(InputStream in) throws IOException {
allProperties.load(in);
}
public void store(OutputStream out) throws IOException {
allProperties.store(out);
}
public static MyConfig getDefault() {
return defaultConfiguration;
}
}
|
Create a simple panel that holds a set of property fields. Then you can pass this
panel to an instance of PreferencesAction.
The preferences can be edited by the user. You can save them to the disk so that
the settings can be restored when the application restarts.
PropertyBundle uiSettings = new PropertyBundle("UI Settings", "ui-settings");
try {
// The settings path points to the following file:
// <user.home>/.myapp/1.0-beta/config/<ui-settings>
File settingsFile = ResourceManager.getSettingsFile("myapp", "1.0-beta",
uiSettings, true);
InputStream in = new BufferedInputStream(new FileInputStream(settingsFile));
// Load the ui settings
uiSettings.load(in);
} catch (IOException ex) {
ex.printStackTrace();
}
|
|