« Groovy and Swing Bindings: Change is Inevitable | Main | New and Mildly Entertaining features in SwingBuidler »

bind() and animate(), new and exciting features in SwingBuilder

Two new node types have been added to Groovy's SwingBuilder (and as a consequence SwingXBuilder and JideBuilder, if you grab a current snapshot): bind(...) and animate(...). These two features don't add new widgets, but a new orthogonal functionality, namely to update properties as other properties are changed, and to update properties over time.

Despite my earlier interest in JSR-295 bind(...) is not based off of any of that code (at least directly). Several reasons contribute to that. First, the draft code underwent a total reboot when the EL portions got abstracted away. Second the new code in CVS is very generic heavy. That is too bad IMHO, because if I am typing that many angle brackets it better be because I am writing an XML document. It will be very typesafe when all is said and done, but then there will still be too much typing, and I like to write long class titles too!

bind(...) is currently a very stripped down version of what JSR-295 will have to offer. It is based of the three pillars I discussed earlier: a trigger, a query, and a notification. Most of the magic occurs in the trigger section, this is where all of the listeners live. The query and the notification are very simple, and once Joint compilation in Groovy is working in the main codebass I may switch out the implementations so that they actually are just Closures, and the world revolves around the trigger. But don't trust any of this while the implementation code lives in the org.codehaus.groovy packages, just trust the properties that you can set on bind(...)

import groovy.swing.SwingBuilder
import java.awt.Insets

swing = new SwingBuilder()
frame = swing.frame {
  vbox {
    slider(id:'slider', value:5)
    button('Big Button?!',
           margin: bind(source:slider, 
                        sourceProperty:'value', 
                        converter:{[it, it, it, it] as Insets}),
    )
  }
}
frame.pack()
frame.size = [frame.width + 200, frame.height + 200] 
frame.show()

animate(...) uses the same backing classes as bind(...), except instead of triggering off of miscellaneous events the queries and notifications trigger off of a javax.swing.Timer. The query is also just a calculation to where it is in the list passed in (currently it only supports a list, interpolation between primitives and some complex types will likely be added before too long).

import groovy.swing.SwingBuilder

swing = new SwingBuilder()
frame = swing.frame {
  vbox {
    slider(value:animate(0..100, id:'anime'))
    button('Again! Again!', actionPerformed: {anime.rebind()})
    slider(value:animate(0..100, repeat:true, interval:100, duration:1000))
  }
}
frame.pack()
frame.show()

This isn't a sign I am abandoning JSR-295. There are lots of nice things that it adds, such as optional bi-directional bindings and incomplete path notifications (i.e. half bound bindings behaving sensibly). What I saw as the core mechanic seemed relatively simple to implement, in the code. So there it is!

Comments (6)

Ivan:

Is this some kind of attempt to catch up with JavaFX?

Not really, this effort was started before JavaFX was announced @ JavaOne 2007, but definitely there is some overlap.

Peter:

Very nice, I'm really starting to like Groovy. I wished I could use Gant, SwingBuilder and Grails at work...

I would argue that JavaFX is trying to catch up with Groovy. JavaFX is mostly talk and a few tech demos, Groovy's SwingBuilder has been around for a long time and continues to get better.

Peter:

Hi,

The example binds two components, can you also bind a component to a model?

In Ruby you can do:

model = Person.new('John')

name_label = JLabel.new
name_label.bind_to model, :name

In JavaFX you can also bind to an 'attribute' which represents a bean property.

But I don't think you can identify a property in Groovy like Ruby or JavaFX does.

(I also posted this question on Graeme's blog: http://graemerocher.blogspot.com/2007/09/new-meta-programming-apis-in-groovy-11.html)

Best regards,

Peter

Peter:

I'm sorry I just now read your article "Groovy SwingBuilder and Bindings, a Hole to be Filled" where you discuss the same thing, I'll just follow your blog for updates :)

Post a comment


About

This page contains a single entry from the blog posted on August 28, 2007 8:41 PM.

The previous post in this blog was Groovy and Swing Bindings: Change is Inevitable.

The next post in this blog is New and Mildly Entertaining features in SwingBuidler.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33