Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT removeHandler on first event notification

I want to remove a GWT event handler the first time I receive an event. I also want to avoid polluting my class with tracking registration objects that aren't really necessary. I currently have it coded as:

final HandlerRegistration[] registrationRef = new HandlerRegistration[1];
registrationRef[0] = dialog.addFooHandler(new FooHandler()
{
    public void onFoo(FooEvent event)
    {
        HandlerRegistration removeMe = registrationRef[0];
        if(removeMe != null)
        {
            removeMe.removeHandler();
        }

        // do stuff here
    }
});

but the use of registrationRef makes the code less readable. Is there a better way to do this without adding variables to my class?

like image 930
Keith Avatar asked Apr 10 '10 18:04

Keith


1 Answers

I'd just make the HandlerRegistration object a field of the enclosing class, that way you won't be bothered by the compiler and it's more "elegant" than shuffling arrays and stuff:

public class TestWidget extends Composite {
    //...

    HandlerRegistration handler;

    public TestWidget() {
        // ...

        handler = button.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                // ...
                handler.removeHandler();                
            }
        });
    }

}
like image 87
Igor Klimer Avatar answered Oct 20 '22 01:10

Igor Klimer