Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Configuration



The framework allows to serialize the application configuration when exiting the application, and deserialize it when starting. Configuration management is managed by the ConfigurationManager class. The management of configuration is automatically performed if using the default AbstractApplication class.

Serializing and deserializing the application configuration is performed through the prefs

Enabling configuration management

Configuration management is enabled if the application is initialized through the AbstractApplication.initConfiguration(Preferences) or AbstractApplication.initConfiguration(Preferences, File) with a not null Preferences. If this is the case: The application configuration will be set by calling the AbstractApplication.setConfiguration(Configuration) method, or by simply setting the value of the AbstractApplication.conf field.

For example:
      public class MyApplication extends AbstractMDIApplication {

        public MyApplication {
          super("MyApplication");
          conf = AppliConfiguration.getInstance();  
      
          Preferences pref = Preferences.userRoot();
          this.initConfiguration(pref);
        }  
      }  

Settings the application configuration

The application configuration will be set by calling the AbstractApplication.setConfiguration(Configuration) method, or by simply setting the value of the AbstractApplication.conf field.

For example:
      public class MyApplication extends AbstractMDIApplication {

        public MyApplication {
          super("MyApplication");
          conf = AppliConfiguration.getInstance();  
        }  
      }  
or even:
      public class MyApplication extends AbstractMDIApplication {

        public MyApplication {
          super("MyApplication");
          setConfiguration(AppliConfiguration.getInstance());  
        }  
      }  

Configuration deserialization

The configuration initialization and deserialization is performed by the two methods:

Configuration instance

Configuration is an interface which can be implemented by both the application and the Plugins. This interface has only two methods to implement: Note that you will have to handle the serializing an unserializing yourself, and also provide accessors for the properties. There is an extended interface called Configuration2 and an abstract class called AbstractConfiguration2 which handle all of this for you.

Serializing the configuration

The configuration will be serialized (if enabled) when existing the application, by using the AbstractApplication.disposeApplication() method. Both the application and the plugins configuration will be serialized.

Method for serializing the configuration

The method called for serializing the configuration is Configuration.getConfiguration(Preferences, File).

For example, we suppose a configuration where we store one boolean property and one int property.
      public void getConfiguration(Preferences pref, File dir) {
        boolean bValue = pref.getBoolean("myBooleanValue", false);
        int intValue = pref.getInt("myIntValue", 0);
      }     

Deserializing the configuration

The configuration will be deserialized (if enabled) when starting the application, in the AbstractApplication.initConfiguration(Preferences, File) method. Both the application and the plugins configuration will be deserialized.

Method for deserializing the configuration

The method called for deserializing the configuration is Configuration.putConfiguration(Preferences, File).

For example, we suppose a configuration where we store one boolean property and one int property.
      public void putConfiguration(Preferences pref, File dir) {
        pref.putBoolean("myBooleanValue", bValue);
        pref.putInt("myIntValue", intValue);
      }     

Plugins configuration

The configuration for a Plugin will be automatically retrieved by the application by calling the Plugin.getPluginConfiguration() method.

For example:
      public class OpenImagePlugin extends AbstractMDIPlugin {
      
        public OpenImagePlugin() {
        }   
      
        public Configuration getPluginConfiguration() {
          return MyConfiguration.getInstance();
        }           

Show and set the configuration properties

Main Article: Settings

It is possible to create a unified settings menu in the application, used by the main GUI application and the Plugins, and to bridge these settings to the application configuration.

Full configuration example

We suppose a configuration where we store one boolean property and one int property.
      public class MyConfiguration implements Configuration {
        private static MyConfiguration conf = null;
        public boolean myBool = false;
        public int myInt = 0;
        
        public static MyConfiguration getInstance() {
          if (conf == null) {
            conf = new MyConfiguration();
          }
          return conf;
        }
      
        public void getConfiguration(Preferences pref, File dir) {
          boolean bValue = pref.getBoolean("myBooleanValue", false);
          int intValue = pref.getInt("myIntValue", 0);
        }      
      
        public void putConfiguration(Preferences pref, File dir) {
          pref.putBoolean("myBooleanValue", bValue);
          pref.putInt("myIntValue", intValue);
        }          
      }

Using the configuration to setup the Application description


It is possible to set the content of the Application description from the configuration by implementing the Configuration.setupApplicationDesc(ApplicationDesc) method. This can be useful if you store the application name, version, build date, or alternate text from the configuration.

See also


Categories: conf | javafx | swing

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences