Effective JavaFX architecture with FxObjects

skshenoy 1,999 views 28 slides Jul 31, 2010
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Developing enterprise applications today using JavaFX is a challenge. The industry has not matured enough to identify patterns and practices. Consequently practioners (architects and developers alike) commit the same mistakes again and again. There is a complete lack of non-UI frameworks that make J...


Slide Content

Effective JavaFX
Architecture with
Presented by
SrikanthShenoy
ObjectSource
(Covers FxObjects0.1)

Introduction
Who am I?
A hands-on architect
Experienced and very knowledgeable in Java and
Java EE
Authored a book on Struts
Several articles on Java EE for leading journals
Director of Enterprise Java Practice at
ObjectSource
Creator of FxObjectsframework

Introduction
What is FxObjects? (Currently 0.1)
An open source JavaFX application development
framework (Apache License)
Why FxObjects?
Enterprise JavaFX architect/developer starts from
scratch, reinventing the wheel, wasting time and
repeating mistakes
Enterprise JavaFX applications need a strategic
and functional framework (not a point solution).
FxObjectsis that strategic functional framework !
Learn FxObjectsin 25 slides !

30K ft. Overview

Introduction
FxObjectsis the end to end application
development framework in the UI tier.
Designed to simplify large JavaFX application
development
Makes your application elegant by using
proven patterns, principles and practices
If you use FxObjectsas defined in the user
guide, your application becomes inherently
test friendly

Introduction
FxObjectsis Dependency Injection (DI)
friendly from the grounds-up.
Tested with Spring 2.5
Dependency Injection + FxObjectsmakes a
very powerful and agile programming model
FxObjectsmakes TDD possible with JavaFX
by using Model-View-Presenter (MVP)
FxObjectsis NOT a UI component library
(and never intends to be).

Model-View-Presenter
(Passive View)
Note the sequence of events in MVP passive
View
Actual UI is lean, mean and clean 
Presenter does the heavy lifting on the UI
side. But Presenter is not really a UI !!
Hence Presenter can be easily tested and
asserted

MVP with FxObjects
NodePresenteris the FxObjectsclass.
MyNodePresenterand MyNodeare app
classes & hold reference to each other
When a event occurs, the MyNodesimply
calls doOnSearchButtonClick()

Presenter Code
doSearchButtonClickmethod
Validates UI (for mandatory fields)
Calls the service asynchronously using FxObjects
Command classes
That’s it!
You will see FxObjectsCommands next
For aiding the asyncservice calls
Update the UI when asynccalls return data
Show error message when service call fails

Challenges in Service
Invocation
Asynccalls are fire and forget
They should execute off the Event Dispatch
Thread (EDT)
All UI updates should then again occur ON
the EDT.
Can be confusing and errror-prone
Swing provided SwingWorker
JavaFX provides a asyncframework
Slightly complex, but FXObjectssimplifies this

JavaFX AsyncFramework

Challenges in Service
Invocation (contd.)
HttpRequest(core JavaFX class) has some
solution (asynccall is off-EDT & callbacks are
on-EDT), but it is the foundation for a full
solution
FxObjectsHttpCommandsprovide the
missing pieces to make this simple
How to easily go to another page v/s stay on
same page after service invocation?
FxObjectssolves this, as we shall see soon
(controller chain)

Command objects
Command objects are configured first and
then executed [with execute(..) method]
varcmd= DefaultAsyncCommand {
runInBackground: function(modelObj:Object):Object {
//call service here
}
onSuccess: function(resultObj:Object):Void {
//update uiwidgets here OR navigate to another UI
}
onError: function(e:Exception):Void {
//show the error
}
}

Automating XML to JavaFX
No Xml tag handling code necessary
Use XmlResponseBuilder
Called ResponseBuildersince it was initially
built to construct JavaFX objects from XML
content (returned by REST services)
Can handle XML of any type and of any
depth
Scales well for all kinds of xml
Best suited for small JavaFX apps though
Larger apps can use JAXBResponseBuilder

Automating JavaFX to XML
No code necessary
Use XmlRequestBuilder
Called RequestBuildersince it was initially
built to construct XML from JavaFX objects to
be sent as request body to REST services
Can handle JavaFX objects of any type and
of any depth
Scales well for JavaFX objects of any kind
Best suited for small JavaFX apps
Larger apps can use JAXBRequestBuilder

HttpCommandsand Builders
Just like DefaultAsyncCommand
No runInBackgroundnecessary. [Http
GET/POST themselves run in background !!]
Url& query parameters are configurable with
{ } syntax
e.g. http://server/app/search?zipCode={zipCode}
The paramsin {} can be passed in a Map
Otherwise reflection is used to query the object
Need RequestBuilderand ResponseBuilder
XML***Builder or JAXB***Builder

GetHttpCommand
varrespBuilder= new MyJAXBResponseBuilder ();
respBuilder.setSchema (…);
varcmd= GetHttpCommand{
urlTemplate: “http://.../Search?output=xml&zip={zipCode}&..
responseBuilder: respBuilder
onSuccess: function(resultData:Object) {
varresult = resultDataas ResultSet;
//update UI components here OR navigate to another UI
}
onError: function(e:Exception) {
..
}
}
..
function doSearchButtonClick() {
varparamMap= new HashMap();
paramMap.put(“zipCode”, node.zipCodeTxtBox.text );
cmd.execute(paramMap);
}

Custom JAXB Response
Builder
public class CafeSearchResponseBuilder extends
JAXBResponseBuilder<ResultSet>{
@Override
public ResultSetbuildResponse(String serverResponse){
ResultSetrs=
buildResponseObjectFromXml (serverResponse);
return rs;
}
@Override
public ResultSetbuildResponse(InputStreaminputStream){
ResultSetrs= buildResponseObjectFromXml (inputStream);
return rs;
}
}

BodyHttpCommand
Use for PUT and POST
Almost same as GetHttpCommand
Additionally needs RequestBuilder
To convert the presentation model object into
format expected by server (GeneralyXML)
Optionally a content pre-processor
To massage the output from the requestbuilder
and before sending to server

Navigation challenges
How to go to another page v/s stay on same
page after service invocation succeeds?
Default JavaFX model involves binding to a
new scenegraph
Couples the lower level UI to top level stage.
No idioms for organizing the application into
modules
Who decides what is the default node to
display?
Solution: FxObjectsControllerChain

Controller Chain

Navigation with FxObjects
Controller Chain
Each Application can hold many Presenters
Each Application can have many Modules
Each Module can hold many Presenters
Application has a Application Controller
Each Module has a Module Controller.
Module Controller has a Id
Each NodePresenterowns the Node
Node Presenter has a Id

Navigation with FxObjects
Controller Chain (contd.)
public MyNodePresenter extends NodePresenter
varcmd= DefaultAsyncCommand {
runInBackground:function (modelObj:Object):Object {
//call service here
}
onSuccess: function(resultObj:Object):Void {
//update uihere OR go to another page
this.changeNodePresenter (“someNodePresenterId”);
}
onError: function(e:Exception):Void {
//show the error
}
}
changeNodePresenter(..) is defined in NodePresenterclass (the parent)

FxObjectsand Dependency
Injection
FxObjectsis built with Dependency Injection
in mind.
DI is optional, but recommended
What can be wired
ApplicationController, ModuleController
NodePresenter(but not Nodes)
Commands, Services (Service Proxies)
Nodes should be maintained by Presenters.
This lets presenters choose appropriate node
handling strategy (cache, dispose after use etc.)

Bootstrapping FxObjects
varctx= new ClassPathXmlApplicationContext (“/your-spring.xml");
varappController=
ctx.getBean("appController") as ApplicationController ;
Stage {
title: "FxObjectsExample“
scene: Scene {
width: 600
height: 400
content: bind [
appController.currentNodePresenter.getNode ()
]
}
}
Node presenter configured as default is shown at the beginning
Every navigation call sets another NodePresenteras the “Current”
NodePresenterand that gets bound to the scenegraph

What’s coming in 0.2?
Lifecycle
Event
Event Bus
Security
JSON to JavaFX round-tripping
Suggestions welcome (Please post in the
discussion forums)
Timeframe: September 2010

FxObjectsSummary
Enterprise JavaFX applications are inherently
complex.
Architecting such apps is art and science
FxObjectscan help
The more complex the application, the more
value you get from FxObjects
End to end programming model for the UI
Does not force any programming model for
the server

Links
Project site –https://fxobjects/dev.java.net
Not a single person open source project
FxObjectsis developed and supported by
ObjectSource (http://www.objectsource.com)
Download, use, extend, redistribute, OEM
Discussion Forums on project site
We will answer your questions
Participation welcome
Post ideas, questions, concerns, comments
Contribute code