Class ModelSharingControllerFactory
- java.lang.Object
-
- org.minifx.fxmlloading.factories.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 ofcreateController(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
Example usage:enum
fields, the class makes a conversion when necessary e.g. when an integer field's value is injected from a System property (String).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)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
call(java.lang.Class<?> controllerClass)
void
clearDependencies()
Clears dependencies cache.<T> T
createController(java.lang.Class<T> controllerClass)
Creates a new instance of the given controller class.static ModelSharingControllerFactory
fromPropertiesProvider(java.util.function.Function<java.lang.String,java.lang.Object> propertiesProvider)
java.util.function.Function<java.lang.Class<?>,java.lang.Object>
getInstanceProvider()
Returns the currently used instance provider.java.util.function.Function<java.lang.String,java.lang.Object>
getPropertiesProvider()
Returns currently used properties provider.static ModelSharingControllerFactory
newDefault()
void
setDependency(java.lang.Class<?> type, java.lang.Object dependency)
Stores given dependency in a cache.
-
-
-
Method Detail
-
newDefault
public static ModelSharingControllerFactory newDefault()
-
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 fieldsdependency
- the instance to be injected into fields of specified type- Throws:
java.lang.NullPointerException
- if thetype
isnull
-
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 interfacejavafx.util.Callback<java.lang.Class<?>,java.lang.Object>
-
-