...
As The Xul Framework has progressed, the need for a clean MVC architecture underlying applications has become clear. MVC (, or Model-View-Controller) , provides for a clean separation of between application data (models), view /application logic (controllers) and the Xul user interface (views). This separation creates loosely couple coupled components that are easier to maintain and most more importantly , testtestable. For a more in-depth look at building Pentaho Xul application with the MVC patterpattern, please refer to the excellent article by Aaron Philips: MVC in Pentaho Xul Applications
...
A Xul Binding object consists of four primary pieces of data and two optional:
- Two XulEventSource objects, one "Source" the other "Target" and a property for each to "Bind" together.
- A binding strategy, one-way, bi-directional, bind-once.
- An optional A Convertor Object that to manages data translation between the source and targettwo.
Creating a Binding Object
There are several methods provided for in the Xul Framework for creating a Binding Object. Some are as simple as calling bind() with four Strings from an Event Handler. We'll not cover all of those here as they're addressed in detail in MVC in Pentaho Xul Applications. Instead, let's have a look from the ground up.
...
As Java lacks first class support for Properties and writing Objects with direct member access is frowned upon, a Binding will Bindings access data by way of the Javabean standard. A discussion of Javabeans is outside the scope of this article and we will proceed with the assumption that you are familiar with them.
At the Javabean level our binding looks something like this:
...
By default a binding object represents a synchronization bi-directionally. Any change to the source will update the target and visa-versa. You can optionally prescribe for a one-way bindings between source and target by passing to the binding ONE_WAY like thisas seen below:
Code Block |
---|
binding.setBindingType(ONE_WAY); |
...
Future versions will introduce the concept of a "bind-once" strategy that will in essence "flash" as snapshot of data between objects at the time of the bindings instantiation.
Conversions
We can further extend the flexibility of our Bindings by providing a BindingConvertor object to manage the translation of data between objects. The BindingConvertor class itself is small that it's worth taking a look at right here.
...
Code Block |
---|
Binding binding = new Binding(textbox, "value", dropdown, "selectedIndex"); BindingConvertor conversion = new BindingConvertor<String, Integer>(){ @Override public Integer sourceToTarget(String value) { return Integer.parseInt(value); } @Override public String targetToSource(Integer value) { return value.toString(); } } binding.setConversion(conversion); |
The Binding Context
Bindings in and of themselves don't actually do anything. They simply describe the Binding relationship between objects. The actual establishing of a binding is performed by a BindingContext object. However, you never have to deal with a BindingContext directly as every XulDomContainer has one. To add a new Binding object to the context, simply pass the Binding to the addBinding() method of the XulDomContainer.