DWT event handling
Lester L. Martin II
lestermartin92 at gmail.com
Sun May 18 06:18:08 PDT 2008
Frank Benoit Wrote:
> bobef schrieb:
> > void handleTextEvent (Event e, Composite composite, TreeItem item, TreeEditor editor,Text text, int inset )
> >
> > This is longer to write than new class {blah blah} :)
>
> No, the additional arguments make the delegate a closure. If you would
> write that as a anonymous class it would look like that:
>
> In Java final vars are accessible for the anonymous class after the
> surrounding method ends:
>
> final Text text = ...
> final TreeEditor editor = ...
>
> Listener l = new Listener {
> public void handleEvent( Event e ){
> // use text, editor as you want
> }
> }
>
> In D1, this would create crashes. The workaround is, to create explicit
> member variable in the anonymous class. This is really ugly and very
> tedious and error-prone.
>
> Listener l = new class( composite, item, editor, text, inset ) Listener {
> Composite composite_;
> TreeEditor editor_;
> Text text_;
> int inset_;
> public this( Composite a, TreeItem b, TreeEditor c,Text d, int e ) {
> this.composite_ = a;
> this.editor_ = b;
> this.text_ = d;
> this.inset_ = e;
> }
> public void handleEvent( Event e ){
> // use the test_, editor_ ...
> // underscore to clearly separate them from
> // the variable used in the surounding method
> }
> }
>
> Now you see what is the advantage of the dgListener?
>
In our code we just created a class that accepted a delegate. This class inherited Listener. It defined a generic event handler that it would use if no event delegate was passed to it. What are the advantages of this template over such a design (If you need real code will post).
Lester L. Martin II
More information about the Digitalmars-d-dwt
mailing list