« July 2007 | Main | September 2007 »

August 2007 Archives

August 28, 2007

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!

About August 2007

This page contains all entries posted to ... And They Shall Know Me By My Speling Errors in August 2007. They are listed from oldest to newest.

July 2007 is the previous archive.

September 2007 is the next archive.

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

Powered by
Movable Type 3.33