DataFX 8 (JavaOne 2014)

66
Data FX From External Data to a UI Flow and Back 8

description

The DataFX 8 Keynote at JavaOne 2014

Transcript of DataFX 8 (JavaOne 2014)

Page 1: DataFX 8 (JavaOne 2014)

DataFXFrom External Data to a UI Flow

and Back

8

Page 2: DataFX 8 (JavaOne 2014)

About us

Hendrik [email protected]

Johan [email protected]..

Jonathan [email protected]

Page 3: DataFX 8 (JavaOne 2014)

Overview

Page 4: DataFX 8 (JavaOne 2014)

Overview

DataSources Websocket

Injection

Flow

Page 5: DataFX 8 (JavaOne 2014)

DataSources

Page 6: DataFX 8 (JavaOne 2014)

DataSources

Facilitate the interaction between a JavaFX Application and Enterprise Data, respecting the commonalities and differences between the Enterprise World and the Client World.

Goal:

Page 7: DataFX 8 (JavaOne 2014)

DataSourcesData

Observable /ObservableList

DataFX

DataProvider

DataReader

Converter

Page 8: DataFX 8 (JavaOne 2014)

DataSourcesiOS Desktop Client

Business Layer

Server

Persistence

REST

Android

Well-known,

Well-documented

Non-proprietary

protocols

Web

Page 9: DataFX 8 (JavaOne 2014)

ProtocolsRESTSSEWebSocketsXMLJSONJDBCFile

Page 10: DataFX 8 (JavaOne 2014)

JFX characteristicsObservable, ObservableList

Leverage dynamic updates to objects and lists. Modifications can be propagated immediately to JavaFX Controls. No error-prone wiring needed.

JavaFX Application Thread Modifactions that result in UI changes should only happen on the JavaFX Application Thread. Apart from those, nothing should happen on the JavaFX Application Thread

Page 11: DataFX 8 (JavaOne 2014)

DataSourcesRead Data

REST, SSE, WebSocket

Convert Data Into Java ObjectsXMLConverter, JsonConverter

Provide Data As Observable instances or ObservableList instances

Page 12: DataFX 8 (JavaOne 2014)

JavaFX IntegrationUsing DataSources, your data (Observable) and data containers (ObservableList) can be kept up-to-date.

JavaFX Controls often use Observable or ObservableList:

Label: Label.textProperty();ListView: ListView.setItems(ObservableList);

Page 13: DataFX 8 (JavaOne 2014)

ExamplesProject AvatarJS on the backendJS on the clientREST, SSE, WebSockets with JSON in betweenAvatarfx demonstrates avatar examples in JavaFX client

Page 14: DataFX 8 (JavaOne 2014)

RESTSimilarity with JAX-RS 2.0 Client APISimpleBuilders or get/setOAuth supportGET/POST/PUT/DELETEClasses:

io.datafx.io.RestSource and io.datafx.io.RestSourceBuilder

Page 15: DataFX 8 (JavaOne 2014)

Demo Time

Page 16: DataFX 8 (JavaOne 2014)

SSE

Wikipedia:Server-sent events is a standard describing how servers can initiate data transmission towards clients once an initial client connection has been established.

Page 17: DataFX 8 (JavaOne 2014)

SSEClient initiates connection

Server sends data, DataFX creates an Object with Observable fields

Every now and then, server sends updated dataDataFX makes sure the Observable fields are updated

Page 18: DataFX 8 (JavaOne 2014)

Demo Time

Page 19: DataFX 8 (JavaOne 2014)

WebSocketsLeverage client-part of JSR 356, Java standard for WebSocket protocol.

DataFX retrieves incoming messages, and populates an ObservableList

Works with any service that supports the WebSocket protocol

Page 20: DataFX 8 (JavaOne 2014)

Demo Time

Page 21: DataFX 8 (JavaOne 2014)

Add

Page 22: DataFX 8 (JavaOne 2014)

Concurrency

Page 23: DataFX 8 (JavaOne 2014)

Concurrency API

JavaFX is a single threaded toolkit

You should not block the platform thread

Rest calls may take some time...

...and could freeze the application

Page 24: DataFX 8 (JavaOne 2014)

DataFX Executor

supports title, message and progress for each servicesupports Runnable, Callable, Service & Task

cancel services on the gui

Configurable Thread Pool

Executor implementation

Page 25: DataFX 8 (JavaOne 2014)

Demo Time

Page 26: DataFX 8 (JavaOne 2014)

Let‘s wait

void ConcurrentUtils.runAndWait(Runnable runnable)

T ConcurrentUtils.runAndWait(Callable<T> callable)

like SwingUtilities.invokeAndWait(...)

we will collect all concurrent helper

methods here

Page 27: DataFX 8 (JavaOne 2014)

Stream Support

JDK 8 has Lambdas and the awesome Stream APIMap and reduce your collections in parallelBut how to turn into the JavaFX application thread?

Page 28: DataFX 8 (JavaOne 2014)

Stream SupportStreamFX<T> streamFX = new StreamFX<>(myStream);

it is a wrapper

ObservableList<T> list = ...; streamFX.publish(list); !!!streamFX.forEachOrdered(final Consumer<ObjectProperty<? super T>> action)

this will happen in the application thread

Page 29: DataFX 8 (JavaOne 2014)

Process Chain

Like SwingWorker on steroids

Support for an unlimited chain of background and application tasks

ExceptionHandling

Publisher support

Page 30: DataFX 8 (JavaOne 2014)

Process Chain

ProcessChain.create(). addRunnableInPlatformThread(() -> blockUI()). addSupplierInExecutor(() -> loadFromServer()). addConsumerInPlatformThread(d -> updateUI(d)). onException(e -> handleException(e)). withFinal(() -> unblockUI()). run();

Page 31: DataFX 8 (JavaOne 2014)

Demo Time

Page 32: DataFX 8 (JavaOne 2014)

Concurrency APIDataFX core contains all basic classes

Exception Handling

Can be integrated in all applications

Java8 Lambda supportConfiguration of thread poolAdd async operations the easy way

Page 33: DataFX 8 (JavaOne 2014)

Flow API

Page 34: DataFX 8 (JavaOne 2014)

JavaFX BasicsIn JavaFX you should use FXML to define your viewsYou can define a controller for the view

Link from (FXML-) view to the controller

<HBox fx:controller="com.guigarage.MyController">     <TextField fx:id="myTextfield"/>     <Button fx:id="backButton" text="back"/> </HBox>

Page 35: DataFX 8 (JavaOne 2014)

View ControllerSome kind of inversion of control

Define the FXML in your controller class

Create a view by using the controller class

Page 36: DataFX 8 (JavaOne 2014)

@FXMLController("Details.fxml") public class DetailViewController {            @FXML         private TextField myTextfield;            @FXML         private Button backButton;               @PostConstruct         public void init() {             myTextfield.setText("Hello!");         } }

define the FXML file

default Java annotation

View Controller

Page 37: DataFX 8 (JavaOne 2014)

View Context

Support of different contexts

Inject context by using Annotation

Register your model to the context

PlugIn mechanism

Page 38: DataFX 8 (JavaOne 2014)

Flow APIThe View Controller is good for one view

How to link different view?

Page 39: DataFX 8 (JavaOne 2014)

Flow APIopenView View

View

View

View

View

search

details

open

diagram

setting*

Page 40: DataFX 8 (JavaOne 2014)

Master Detailtwo views: master and detail

use FXML

switch from one view to the other one

delete data on user action

decoupling all this stuff!!

Page 41: DataFX 8 (JavaOne 2014)

Master Detail

MasterView DetailsViewback

detailsdelete

Page 42: DataFX 8 (JavaOne 2014)

Master Detail

MasterView DetailsViewback

details

FXML Controller FXML Controller

delete

Page 43: DataFX 8 (JavaOne 2014)

Master Detail

Flow flow = new Flow(MasterViewController.class). withLink(MasterViewController.class, "details", DetailsViewController.class). withLink(EditViewController.class, "back", MasterViewController.class).

direct link between the views

action id

MasterView DetailsViewback

detailsdelete

Page 44: DataFX 8 (JavaOne 2014)

Flow flow = new Flow(MasterViewController.class). . . . withTaskAction(MasterViewController.class, "delete", RemoveAction.class); !!!withTaskAction(MasterViewController.class, "delete", () -> . . .);

Master Detail

define a custom action …

action name

delete

MasterView DetailsViewback

detailsdelete

… or a Lambda

Page 45: DataFX 8 (JavaOne 2014)

Master DetailUse controller API for all views

Define a Flow with all views

link with by adding an action

add custom actions to your flow

but how can I use them?

Page 46: DataFX 8 (JavaOne 2014)

Master Detail@FXMLController("Details.fxml") public class DetailViewController { ! @FXML @ActionTrigger("back") private Button backButton; @PostConstruct public void init() { //... } @PreDestroy public void destroy() { //... } }

controller API

defined

in FXMLbind your flow

actions by annotation

listen to the

flow

Page 47: DataFX 8 (JavaOne 2014)

Master Detail

bind it by id

@FXMLController("Details.fxml") public class DetailViewController { ! @FXML @ActionTrigger("delete") private Button backButton; @PostConstruct public void init() { //... } @ActionMethod("delete") public void delete() { //... } }

Page 48: DataFX 8 (JavaOne 2014)

Flow APIshare your data model by using contexts

ViewFlowContext added@FXMLViewFlowContext private ViewFlowContext context;

You can inject contexts in your

action classes, too

@PostConstruct and @PreDestroy are covered by the view livecycle

Page 49: DataFX 8 (JavaOne 2014)

Flow APIProvides several action types like BackAction

(Animated) Flow Container as a wrapper

DataFX Core ExceptionHandler is used

Flows are reusable

@BackAction private Button backButton;

Page 50: DataFX 8 (JavaOne 2014)

Demo Time

Page 51: DataFX 8 (JavaOne 2014)

Flow APIBy using the flow API you don‘t have dependencies between the viewsReuse views and actions Use annotations for configuration

Page 52: DataFX 8 (JavaOne 2014)

Injection API

Page 53: DataFX 8 (JavaOne 2014)

Injection APIBy using the flow api you can inject DataFX related classesBut how can you inject any data?

Page 54: DataFX 8 (JavaOne 2014)

Injection APIBased on JSR 330 and JSR 299

Developed as plugin of the flow API

Use default annotations

Page 55: DataFX 8 (JavaOne 2014)

Injection APIViewScope: Data only lives in one View

FlowScope: Data only lives in one Flow

ApplicationScope: Data lives in one App

DependentScope: Data will be recreated

Page 56: DataFX 8 (JavaOne 2014)

Demo Time

Page 57: DataFX 8 (JavaOne 2014)

Injection APIIt’s not a complete CDI implementation!

Qualifier, etc. are currently not supported

No default CDI implementation is used

Weld and OpenWebBeans core modules are Java EE specific

Page 58: DataFX 8 (JavaOne 2014)

Injection APIInject your data in the view controller

Define the scope of data types

Add your own scope

Page 59: DataFX 8 (JavaOne 2014)

DataFX Labs

Page 60: DataFX 8 (JavaOne 2014)

Validation APIUse of Java Bean Validation (JSR 303)

Supports the JavaFX property API

Developed as a Flow API plugin

public class ValidateableDataModel { @NotNull private StringProperty name; }

Page 61: DataFX 8 (JavaOne 2014)

Validation API@FXMLController("view.fxml") public class ValidationController { @Valid private ValidateableDataModel model; @Validator private ValidatorFX<ValidationController> validator; public void onAction() { validator.validateAllProperties(); } }

Page 62: DataFX 8 (JavaOne 2014)

EJB SupportAccess remote EJBs on your client

API & a experimental WildFly implementation

Developed as a Flow API plugin

Page 63: DataFX 8 (JavaOne 2014)

EJB Support@FXMLController("view.fxml") public class ViewController { ! @RemoteEjb() RemoteCalculator calc; ! @FXML @ActionTrigger("add") Button myButton; ! @ActionMethod("add") public void add() { runInBackground(() -> sysout(calc.add(3, 3))); } !}

Page 64: DataFX 8 (JavaOne 2014)

Feature TogglesIntegrate feature toggles in the view

Developed as a Flow API plugin@FXMLController("featureView.fxml") public class FeatureController { ! @FXML @HideByFeature("PLAY_FEATURE") private Button playButton; ! @FXML @DisabledByFeature("FEATURE2") private Button button2; !}

Page 65: DataFX 8 (JavaOne 2014)

www.datafx.io

Page 66: DataFX 8 (JavaOne 2014)

QA