-
Notifications
You must be signed in to change notification settings - Fork 16
Technical overview
This page contains a technical description of ConfigMe's most important classes to get you started.
To initialize ConfigMe, you have to create a SettingsManager
. This is the main class of ConfigMe and allows
your application to interact with a user's configuration.
The classes needed to create a settings manager are shown in the picture below.
Typically, you supply the classes that are marked in bold and green in the chart below to the SettingsManagerBuilder
:
- The SettingsHolder classes are classes you create which contain all your configuration's property definitions.
With this information, a
ConfigurationData
instance is built, which will contain all properties. - The PropertyResource allows to read from a file and to write to it. This wraps a file like a config.yml, which users will be able to edit to configure your application. For YAML, you can supply a File to the builder.
- The MigrationService is optional and can help detect older versions of your configuration as it evolves. For instance, it can move a property from an old path to a newer one without losing the configured value. This will be discussed later on in Migration service.
An in-depth description of the classes is available in the technical documentation.
All properties that make up your configuration are defined in SettingsHolder
classes. It's an interface that your
classes should extend as it signals to ConfigMe that it contains properties it has to pick up on.
The properties are defined as constants. Example:
public final class TitleConfig implements SettingsHolder {
public static final Property<String> TITLE_TEXT =
new StringProperty("title.text", "-Default-");
public static final Property<Integer> TITLE_SIZE =
new IntegerProperty("title.size", 10);
private TitleConfig() {
// prevent instantiation
}
}
Each property is defined by the path it is saved under in the file and the default value to use if it is absent or invalid in the configuration file.
The SettingsManager
is the only class you need to interact with once you've created it. It allows you to
look up a property's value and to change it; you can also save and reload the entire configuration with it.
As first step, if we want to read from a YML file, we just need to provide our SettingsHolder classes to create a
settings manager by using the SettingsManagerBuilder
:
SettingsManager settingsManager = SettingsManagerBuilder
.withYamlFile(new File("config.yml"))
.configurationData(PluginSettings.class)
.useDefaultMigrationService()
.create();
This takes care of creating a ConfigurationData
instance for us and passes it to the settings manager as it
creates it.
The settings manager is ready to be queried for the properties' values as soon as it's returned from the builder:
String name = settingsManager.getProperty(PluginSettings.NAME);
int textSize = settingsManager.getProperty(PluginSettings.TEXT_SIZE);
Property values can be modified and the whole configuration saved with the settings manager, too:
private void incrementTextAndSave() {
int textSize = settingsManager.getProperty(PluginSettings.TEXT_SIZE);
settingsManager.setProperty(PluginSettings.TEXT_SIZE, textSize + 1);
settingsManager.save();
}
Navigation
« Introduction | Technical overview | Defining properties » |
Guide
- Introduction
- Getting started
- Migration service
- Bean properties
- Custom property types
- Technical documentation
Updating
Development (internal)