How to create an application

The application we are going to create in this post might not be the coolest one but we think that it gives a good overview of the Dolphin Platform concepts and APIs. The final application should calculate the sum of two values and look like this:

A user can type in 2 values in the textfields on the left and the result textfield will automatically be updated. In addition, we will add a "reset" button that resets the content of the textfields. In general, the complete functionality can be described like it is sketched in this picture:

As the complete controller logic will be defined on the server when using the Dolphin Platform, the addition of the values and the reset action will be defined in a server side controller. This might not be very useful for this small example but think about bigger applications with complex calculation or a persistence layer. In addition, you don't need to code the logic several times if you want to support desktop, mobile and web clients.

Before we start to create the application by using the Dolphin Platform, we will create a client only application by using JavaFX. That's why we decided to use an example that normally don't needs a server. We can create a client side only application by using only JavaFX. Thus, we can compare the 2 solutions later and the Dolphin Platform integration for JavaFX might become more clear. So, this post might not show all benefits of the Dolphin Platform but will give a good overview of its basic APIs.

The JavaFX calculator application

As a first step I will create the layout of the application by using the SceneBuilder that can be downloaded at Gluon. Here is a picture of my final layout in SceneBuilder:


Once this is is saved as a view.fxml file we can use it in JavaFX. Here is the content of the fxml file:

Next, we need a controller class that add some interaction and bindings to our view. In JavaFX, you can create a view by bundling a controller class and a fxml file so you can inject controls that are defined in the fxml description directly in your Java controller.

Let's start with a controller that injects all controls that we need for our use case:

In this class I already add a initialize() method that will automatically be called by JavaFX after all controls have been injected in the controller instance. Sadly, this is done by some kind of magic and JavaFX does not provide an default interface for this behavior. You need to know that you can add this method to your controller class by reading the FXML documentation. For this small example, we don't need to add any other methods to the controller. Everything we want to do can be defined in the init method. Now, we need to add an action to the reset button and define a binding for the values to automatically update the result value. I don't want to describe the JavaFX API in deep and just show the final source code of the method:

As a last step, we need to create a view and show it on the screen. Here is the main application class that does everything that we need:

Once this is done, we can start our application and it's working as expected:

The Dolphin Platform based calculator application

When creating an application based on the Dolphin Platform, we start to define the model and code the basic controller functionality. Let's start with the model definition (A first description of the Dolphin Platform model API ca be found here). In the given example the model is quite small and a matching Dolphin Platform model will look like this:

If you want to simply use getter and setter methods when working with the model you can add some convenience methods:

The model class should be defined in a module that is shared between the client and server sources.

Once the model is done we can start working on the controller. As described in the first overview, a controller will be managed by the web container on server side. Based on this all, the well known Spring and JavaEE specifications can be used in Dolphin Platform controllers. In this example we will make use of the @PostConstruct annotation that marks our init() method. This method will automatically be called once the controller has been created. In the init() method we can add some listeners to our model since the model instance is already created and injected in the controller instance when the @PostConstruct is handled.

Once this is done we need to add some additional features to the controller class. Let's start with the calculation. We can reuse most of the code that we already used in the JavaFX only example since the Dolphin Platform Property definition is similar to the one in JavaFX:

The last thing that is missing is the "reset" functionality. We create a Dolphin Action that can be triggered by a client. To do so, the @DolphinAction annotation can be used. Once we created the action our controller is done and will look like this:

Now, we are mostly done with the server side of our example. Since we want to use Spring Boot to run the server we need to add a main class to our application. The Dolphin Platform adds Spring Boot support and therefore such a class is quite small:

Spring Boot will automatically find the Dolphin controller and create a new instance whenever a client creates a matching view.

For the JavaFX client, we will create an application class that extends the javafx.application.Application as any other JavaFX application. We directly create the connection to our Dolphin Platform based server application that is defined by the ClientContext class:

For the view, we will use the same MVC and FXML based approach as in the first JavaFX example. We can reuse the FXML file ("view.fxml") since the UI won't change. But by using the Dolphin Platform, the controller will be much smaller. Since the real controller class is already defined on the server we only need to define some bindings here. To do so we can extend the Dolphin Platform class AbstractViewBinder that defines all information that we need to create an interactive UI. Here we will inject all UI elements by using the @FXML annotation like before. The AbstractViewBinder class defines the abstract init() method that we need to implement in our class:

We will define the content of the init() method later and finish the application class first. Now, we can load the FXML with the given controller and show it on screen:

As you can see, we only use well known JavaFX APIs here. Once this is done you can already start the server and client applications. But you won't see any data on the client since the binding is still missing. To create a binding in JavaFX, the Dolphin Platform offers a helper class called FXBinder. By using the class you can define (bidirectional) bindings between Dolphin Platform properties and JavaFX properties:

Last but not least, we need to define the reset function. To do so we can add an action handler to the button. In the handler we will trigger the Dolphin Action on server side. Here is the code of the final view binder class:

Now, we can use the client and the calculation and reset will be done on the server. Well, this might not be necessary for the given small example but for more complex applications this handling is awesome. There are several benefits:

You will get the benefit when thinking about an additional feature. Let's say the data of the application should be stored in a database whenever a calculation has been done. With JavaFX, we need to connect to a database, handle transactions in the client and do a lot more stuff. Server frameworks already contain all those features and since the Dolphin Platform example is based on Spring Boot we can simply use Spring Data here. In this case, we only need to inject a Spring Data repository in our controller class and simply store the calculation in the DB. All challenges like transaction support will be handled by Spring in this case. Your controller might look like this:

This was the first complete "getting started" example of the Dolphin Platform and I hope that you like the basic concepts.

Need commercial support?

The Canoo Engineering AG provides commercial support and enterprise modules based on Dolphin Platform.