cdi.commons.fx -JavaFX/CDI Bootstrap
The both technologies JavaFX and CDI are greate for itself.. but how to combine them? How I can get managed JavaFX - controller instances?
Starting from the JavaFX – side, we have to implement the Class extending javafx.application.Application. The importand method will be
With CDIContainerSingleton.getInstance() you will get an instance of the Weld-Container. After the normal JavaFX – init there will be an event fired with the primaryStage as attribute. Now the primaryStage is available in an managed environment.
JAvaFX –> CDI
(JavaFXBaseTest)
The final jUnit-Class (extends JavaFXBaseTest ) will implement the method –> testImpl(final Stage stage). The jUnit-Test will be a manged instance itself.
But how to get the manged controller class instance?
This is done by the
During the process of loading the fxml-file the used FxmlLoader will get an instance of an ControllerFactory-Callback.
The importand lines are the following
The ControllerFactory->Callback.call() will give back a manged instance of the controller class.
Now you can use CDI inside the pojo-fx-controller class.
So we are done.. Happy coding ;-)
UML-Diagramm
![](http://wiki.rapidpm.org/download/attachments/8093789/uml_fx_20130802_001.png?version=1&modificationDate=1375433878726&api=v2)
Starting from the JavaFX – side, we have to implement the Class extending javafx.application.Application. The importand method will be
public
void
start(Stage primaryStage)
throws
Exception
CDIJavaFXBaseApplication
![image image](http://lh5.ggpht.com/-IKNxCJX9Ma8/Uf12wwRsReI/AAAAAAABTJQ/MQTcwt1GbIU/image_thumb%25255B1%25255D.png?imgmax=800)
With CDIContainerSingleton.getInstance() you will get an instance of the Weld-Container. After the normal JavaFX – init there will be an event fired with the primaryStage as attribute. Now the primaryStage is available in an managed environment.
JAvaFX –> CDI
The observer will catch this event. Hownthis will be donw, I show with the excample jUnit-test.cdi.event()
.select(Stage.
class
,
new
AnnotationLiteral<CDIStartupScene>() {})
.fire(primaryStage);
(JavaFXBaseTest)
![image image](http://lh6.ggpht.com/-mYyW73L1y7A/Uf12xkbSleI/AAAAAAABTJg/GrkqVjLTMhM/image_thumb%25255B3%25255D.png?imgmax=800)
The final jUnit-Class (extends JavaFXBaseTest ) will implement the method –> testImpl(final Stage stage). The jUnit-Test will be a manged instance itself.
![image image](http://lh5.ggpht.com/-ANX-6HvYjEk/Uf12y-vIj3I/AAAAAAABTJw/sbkL-wTuD-A/image_thumb%25255B5%25255D.png?imgmax=800)
But how to get the manged controller class instance?
This is done by the
FXMLLoaderSingleton used from the jUnit-test.
final FXMLLoader fxmlLoader = fxmlLoaderSingleton.getFXMLLoader(LoginPane.class);
During the process of loading the fxml-file the used FxmlLoader will get an instance of an ControllerFactory-Callback.
![image image](http://lh5.ggpht.com/-snchFSsAGE8/Uf12zxccG1I/AAAAAAABTKA/WmMrwTqTh50/image_thumb%25255B7%25255D.png?imgmax=800)
The importand lines are the following
![image image](http://lh5.ggpht.com/-2vpQxC1vfnI/Uf1207BA-xI/AAAAAAABTKQ/f8jnqwb7CC0/image_thumb%25255B9%25255D.png?imgmax=800)
The ControllerFactory->Callback.call() will give back a manged instance of the controller class.
Now you can use CDI inside the pojo-fx-controller class.
public class LoginController implements CDIJavaFxBaseController { // Standard FXML injected fields @FXML TextField loginField; @FXML PasswordField passwordField; @FXML Text feedback; // CDI Injected field @Inject LoginService loginService; // Default application parameters @Inject @CDIJavaFXBaseApp Parameters applicationParameters; @FXML protected void handleSubmitButtonAction(ActionEvent event) { feedback.setText(loginService.login(loginField.getText(), passwordField.getText())); } @Override public void initialize(URL location, ResourceBundle resources) { loginField.setText(applicationParameters.getNamed().get("user")); } }
So we are done.. Happy coding ;-)
UML-Diagramm
![](http://wiki.rapidpm.org/download/attachments/8093789/uml_fx_20130802_001.png?version=1&modificationDate=1375433878726&api=v2)
Kommentare
Kommentar veröffentlichen