The groovy-swt binding framework is inspired by the swing binding framework, but is based on the jface databinding. It is not meant to be a replacement of the jface databinding, and does not include all the features of the jface databinding. Think of it as an easy shortcut covering most cases, so you may still have to revert to the jface databinding framework.

An Appetizer

Here is just a simple example from the included examples:

person = new Person01(firstName:'John', lastName:'Doo', gender:'Male', age:12, married:true)
.....
label('Firstname:')
text(text:bind(model:person, modelProperty:'firstName'))
label('Age:')
text(text:bind(model:person, modelProperty:'age'))
label('Married:')
button(style: 'CHECK', selection:bind(model:person, modelProperty:'married') )

This will give you automatic two-way binding:

The bind object

 Properties:

Builder styles

There are (at least) two ways you can write your bindings using the groovy-swt builder:

The basic using a bind node:

text('', id:firstNameTextWidget)
bind(target: firstNameTextWidget, targetProperty: 'text', model: person, modelProperty: 'firstName')

or the shortcut to avoid all the typing:

text(text:bind(model:person, modelProperty:'firstName'))

If you find that the binding functionality provided in groovy-swt is not enough or doesn't fit your needs you can use create your own IObservable using the jface databinding framwork directly and then provide that as the model object, like:

def observable = BeansObservables.observeValue(person, "firstName")
text(text:bind(model:observable)

Viewers

If you want to bind the data to jface viewers (lists, combos, tables, etc) you should bind the input attribute like:

def people = new WritableList(Realm.default)
people.add(new Person02(name:"Wile E. Coyote", city:"Tucson"))
people.add(new Person02(name:"Road Runner", city:"Lost Horse"))
.....
list() {
   listViewer(input: bind(model:viewModel.people, modelProperty:'name'))
}



If you want to the viewer to reflect changes to the list it needs to be a list that send notification if the list changes, so use the jface-databinding class WritableList. Groovy-swt accepts ordinary groovy lists, but then it will be static data and the viewer will not reflect changes to the list:

class ViewModel {
def cities = ['NY', 'LA', 'NB']
}

....
combo(style:'READ_ONLY') {
    comboViewer(input: bind(model:viewModel, modelProperty:'cities'))
}



Master-Detail

If you use a jface viewer as a model if will create a master-detail observable of the selected item:

list() {
    listViewer(id:'v1', input: bind(people,  modelProperty:'name'))
}
// doing it manually:
text(text: bind(BeansObservables.observeDetailValue(ViewerProperties.singleSelection().observe(v1), 'city', String.class)))
// shortcut
text(text: bind(model: v1, modelProperty:'city'))



ComputedValues

To make simple one-way binding (possibly with some kind of calculations) you can use a bind closure:

 text(text:bind {viewModel.text })

Groovy-swt will (like the Swing builder) automatically find out which properties are used in the closure and create a ComputedValue binding for the closure with bindings to the properties used in the closure.

Or with a more complex example:

text(text:bind(model:viewModel, modelProperty:'text'))
// NOTE: Bind can not find the properties to bind to if they are hidden inside a GString.
label(text:bind {"You have written "+viewModel.text?.size()+ " characters"})
label('Characters left: ')
label(text:bind{(50-viewModel.text?.size()).toString()})

You can also use the model2target.converter to create a simple calculated value:

button('Remove', enabled: bind(model: peopleViewer, modelProperty:'name', 'model2target.converter': {t1.selectionCount>0})

 

Threads


TODO: Realms, updating models in the realm,