Class ModelSharingControllerFactory

  • All Implemented Interfaces:
    javafx.util.Callback<java.lang.Class<?>,​java.lang.Object>, ControllerFactory

    public class ModelSharingControllerFactory
    extends java.lang.Object
    implements ControllerFactory
    A default factory for FXML controllers.

    This class supports a basic field injection using @Inject annotation. It can be used to initialize and configure controller, model or service instances, and bind them together. For every call of createController(Class) method - a new controller instance is created, but all the injected dependencies (models, services, etc) are instantiated only once, stored in cache and used as singletons.

    The factory first makes an attempt to match field to be injected with System properties and then with an optional properties provider. The matching is done using @Named annotation if present, otherwise using field's name. If no matching property is found, the factory consults dependencies cache trying find a match with the field by exact type. Otherwise it requests the instance provider to supply a new instance of given type, injects all its dependencies and puts into the cache.

    For primitive and enum fields, the class makes a conversion when necessary e.g. when an integer field's value is injected from a System property (String).

    Example usage:
     class PersonModel {
    
         @Inject
         @Named("person.age.visible")
         boolean defaultShowAge;
    
         @Inject
         Side personDetailsPaneSide;
     }
    
     class PersonService {
         Person findByName(String name) {
             // ...
         }
     }
    
     class PersonController {
         @Inject
         PersonModel model;
    
         @Inject
         PersonService service;
    
         @FXML
         void initialize() {
             // Initialize GUI with data from service, bind controls with model, etc.
         }
     }
     

    The class is not synchronized. If multiple threads access it concurrently, it must be synchronized externally.

    • Constructor Summary

      Constructors 
      Constructor Description
      ModelSharingControllerFactory​(java.util.function.Function<java.lang.Class<?>,​java.lang.Object> instanceProvider, java.util.function.Function<java.lang.String,​java.lang.Object> propertiesProvider)  
    • Constructor Detail

      • ModelSharingControllerFactory

        public ModelSharingControllerFactory​(java.util.function.Function<java.lang.Class<?>,​java.lang.Object> instanceProvider,
                                             java.util.function.Function<java.lang.String,​java.lang.Object> propertiesProvider)
    • Method Detail

      • fromPropertiesProvider

        public static ModelSharingControllerFactory fromPropertiesProvider​(java.util.function.Function<java.lang.String,​java.lang.Object> propertiesProvider)
      • createController

        public <T> T createController​(java.lang.Class<T> controllerClass)
        Creates a new instance of the given controller class.
        Type Parameters:
        T - the type of the controller to create
        Parameters:
        controllerClass - class of the FXML controller used to locate the FXML file
        Returns:
        the instance of the controller
      • setDependency

        public void setDependency​(java.lang.Class<?> type,
                                  java.lang.Object dependency)
        Stores given dependency in a cache.
        Parameters:
        type - class of the dependency used for matching with injectable fields
        dependency - the instance to be injected into fields of specified type
        Throws:
        java.lang.NullPointerException - if the type is null
      • clearDependencies

        public void clearDependencies()
        Clears dependencies cache.
      • getInstanceProvider

        public java.util.function.Function<java.lang.Class<?>,​java.lang.Object> getInstanceProvider()
        Returns the currently used instance provider.
        Returns:
        instance provider
      • getPropertiesProvider

        public java.util.function.Function<java.lang.String,​java.lang.Object> getPropertiesProvider()
        Returns currently used properties provider.
        Returns:
        properties provider
      • call

        public java.lang.Object call​(java.lang.Class<?> controllerClass)
        Specified by:
        call in interface javafx.util.Callback<java.lang.Class<?>,​java.lang.Object>