Sunday, January 29, 2017

Migrating to Vaadin 8 (beta, continued)

Today I finished my migration of a (small) demo app to the new Vaadin 8 API.

First of all: Vaadin 8 has a compatibility layer. Everything you may have used with Vaadin  7 will still work with Vaadin 8, you just have to change some package names in your import statements. (for e.g. ui gets ui.v7) and everything will work out of the box with Vaadin 8.

But my goal was to completely remove the compatibility layer. That needed quite a bit of work, as the new DataBinding API focuses on binding bean properties. My app used the PropertySetItem in a bunch of places. To move these I created backing beans and used the new API.


This was not too hard, but there was more work to be done as I had to migrate all of my custom Validator implementations. The new Validator signature tastes better than the v7 one, but changing them introduces even more work:

class PasswordMatchValidatorV8 constructor(private val src: PasswordField) : 
    Validator<String> {

    override fun apply(value: String, ctx: ValueContext): ValidationResult {
        val pw: String = src.value        
        if (pw != value) {
            return ValidationResult.error(NO_MATCH)
        return ValidationResult.ok()

    companion object {
        const val NO_MATCH = "Passwords don't match"    }

That did make up for most of the fields. Another place where I had to change the code was the old BeanItemContainer  that I used to bind Collections to Grids etc. Here we have new API too that looks like this:

grid = Grid()
grid.addColumn{ u -> }.setCaption("Username")

dataProvider = ListDataProvider(users)
grid.dataProvider = dataProvider
That's it. Took me about a day for 9 View Classes, but remember I was very slow getting used to the new binding style.

For my personal taste the new binding api is a great step forward, and it makes heavy use of SAM types. Using it with Kotlin is a breeze. It's is still far away from beeing as powerful as the good old eclipse databinding api, which is still my personal favorite when it comes to ui databindings.

Sunday, January 15, 2017

Kotlin Properties and the Vaadin 8 BeanBinder

Vaadin 8 (currently in beta) comes with a whole new DataBinding API that makes heavy use of the Java 8 Lambda Features.

Unfortunately Java has no concept of a Property, so one of the new ways to bind a Bean Property is writing something like (Taken from the documentation):

binder.bind(nameField, Person::getName, Person::setName);

Since Kotlin has a concept for a property you may want to write something like:


Thats possible with an extension method for the Vaadin Binder that could probably look like this:

fun <BEAN, T> Binder.BindingBuilder<BEAN, T>.bind(prop: KMutableProperty<T>) {
            ValueProvider { bean: BEAN -> },
            Setter { bean: BEAN, v: T ->, v) })

Have fun with Kotlin & Vaadin!