DWT event handling
Frank Benoit
keinfarbton at googlemail.com
Sun May 18 06:51:01 PDT 2008
bobef schrieb:
> Lester L. Martin II Wrote:
>
>> 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
>>
>
> This was basically my point too. This way seems shorter than having to write all these arguments and having to remember them :)
The current code that look like this:
void handleMyEvent( Event e /+optional args follow+/, int myval ){
}
addListener( DWT.Selection, dgListener( &handleMyEvent, avalue ));
What happends, that the function dgListener does create a Listener
class, and the handleEvent is forwareded to your supplied delegate.
What am i missing?
More information about the Digitalmars-d-dwt
mailing list