Skip to content

Settings manager

ljacqu edited this page Jan 18, 2020 · 1 revision

After defining the properties of your configuration, you can create a settings manager in order to load the user's config file and to interact with it. This is the only file you need to work with after you've created it.

Creating the settings manager

We use the SettingsManagerBuilder to create a SettingsManager. The easiest way of creating a settings manager is to pass it the YAML file and to provide your settings holder classes, like so:

File configFile = new File("config.yml"); // path to config file
SettingsManager settingsManager = SettingsManagerBuilder
  .withYamlFile(configFile)
  .configurationData(TitleConfig.class, SomeOtherConfig.class)
  .useDefaultMigrationService()
  .create();

System.out.println("Title: " + settingsManager.getProperty(TitleConfig.TITLE_TEXT));

As detailed before, when you create the settings manager, you can define the three components it will make use of:

  • The property resource (or YAML file): handles reading and writing to the configuration file.
  • The settings holder classes: all properties that make up your configuration. If you need more control over how the ConfigurationData is created, you can provide a finished one with an alternative method on the builder.
  • The migration service, discussed in detail on the Migration service page. For now, suffice to say that you can call useDefaultMigrationService(), which means the settings manager will make sure that all properties are available in the property resource (the YAML file). Otherwise, it will write to the property resource, adding any missing properties with their default value. Note that you can skip setting a migration service, in which case ConfigMe will never automatically write to the property resource.

Settings manager features

Read a property

Trivially, as seen in the example above, you read property's values using the settings manager.

Change a property

You can change the value of a property:

settingsManager.setProperty(TitleConfig.TITLE_TEXT, "New title");

Any subsequent calls like settingsManager.getProperty(TITLE_TEXT) will return this value. Important: This does not update the property resource! You need to call settingsManager.save() after you've changed the properties you wanted in order for the changes to be reflected in the file! Otherwise the file is unchanged and you'll get the values from the file again when your application restarts.

Save config

Mentioned just above, settingsManager.save() saves all properties and their associated value to the property resource. Missing properties will be added (with their default value), comments are written as configured in the settings holder classes.

// Save to the file. Adds any missing properties with their default value.
settingsManager.save();

Reload settings

You may also reload all settings from the property resource. This is basically the opposite of saving: ConfigMe will forget about all of the property values it has in memory and will reload them from the property resource. It's a useful feature for your users to be able to reload the settings after they've modified their config file.

// Read from the file and set all properties according to it
settingsManager.reload();

Further reading

This page concludes the "Getting started" portion of the ConfigMe wiki. Further topics can be read depending on interest; see the sidebar on the right.


Navigation

« Defining properties The settings manager Migration service »