Re: [Xswt-developer] event handlers
Brought to you by:
dvorme
|
From: <ha...@id...> - 2006-06-19 06:40:36
|
David,
> > I've never liked anonymous classes used for creating listeners (new
> > <interface>{ ... }), as they remind me to much of closures without
> > their elegance.
> Agreed.
> > I'd rather automatically create a wrapper using Proxy which calls a
> > function. E.g. define a Function interface, with (scripting) language
> > specific implementations (MethodFunction, PnutsFunction, ...), and
> > IDataParsers for them. Then write a generic IDataParser for listener
> > interfaces, that uses parse(source, Function.class) and wraps it using
> > Proxy/InvokationHandler. Sounds like a nice hack, doesn't it?
> >
> I'm not quite following you here. Could you provide a quick
> example of the kind of code this would let you write?
The code for this is already checked in. It consists of several parts:
1) A Function interface, with IDataParsers for Java methods (and pnuts
functions in the pnuts extension).
2) A generic IDataParser for ANY interface, which reads the source as a
Function and then returns a Proxy for the desired interface which calls the
Function for the interface's methods. This is used in the example I sent
some days ago:
<button text="0" selectionListener="handlers.digit"/>
<script x:id="handlers" lang="pnuts">
function digit(event)
calc::appendDigit(event.widget.text);
</script>
In this example, the pnuts script defines the digit(event) function, which
may be referred to and parsed as a Function by "handlers.digit" ("handlers"
refers to the script and "digit" to the digit function). The button gets a
selectionListener that is a proxy for the appropriate class, that calls the
digit function, since the generic IDataParser for interfaces knows how to
wrap a Function by a Proxy, so it in effect may implement any specific
interface. Since the digit function takes one argument, is may handle the
single method in the SelectionListener interface. BTW, the generic
IDataParser for interfaces may also wrap a Bindings object (Script
implements Bindings) and call the function of the same name as the interface
method that is invoked.
I'll try to explain this more completely, once it works as I want it to.
Hallvard
|