jastrachan | 4 Apr 09:04 2005
Picon

Re: Closures and listeners

On 2 Apr 2005, at 17:33, Martin C. Martin wrote:
> Hi all,
>
> Is there an easy way to create a listener using a closure, outside of 
> a builder?  In Java I'd do this:
>
> Processor deviceProc = ...
> deviceProc.addControllerListener(new ControllerListener() {
>    public void controllerUpdate(ControllerEvent ce) {
>       ...
>    }
> }
>
> What's the groovy way?

deviceProc.controllerUpdate = { ce -> println "I was just called with 
event $ce" }

i.e. we use the javabeans introspector to make event listener methods 
available as properties which can be set with a closure

In CVS HEAD we also support method pointer syntax now; so you could 
refer to some bean as well.

e.g.

class Foo {
     void onEvent(event) {
	println "I was just called with event $ce"
     }
}
foo = new Foo()

deviceProc.controllerUpdate = foo.&onEvent

James
-------
http://radio.weblogs.com/0112098/


Gmane