DWT event handling

Frank Benoit keinfarbton at googlemail.com
Sun May 18 04:13:03 PDT 2008


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?



More information about the Digitalmars-d-dwt mailing list