Search code examples
event-handlinggwtpgwt-platform

GWTP typesafe with @UiHandler for ui elements


Before GWTP, I used aproach like this:

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

But gwtp framework maks me use this aproach:

@UiField 
Button button;

@UiHandler("button")
void onButtonClick(ClickEvent e){
    getUiHandlers().buttonClickAction();
}

I wonder is there any TYPESAFE solution for this?

How to give to @UiHandler() type safe value f.e.:

@UiHandler(button);

?


Solution

  • First of all @UiHandler is a GWT UiBinder feature. It has nothing to do with GWTP. If you want to ensure no one changes the type of ui:field in the xml file, you can map it with @UiField first.

    @UiField 
    Button button;
    
    @UiHandler("button")
    void onButtonClick(ClickEvent e){
      getUiHandlers().buttonClickAction();
    }
    

    This way, in case someone puts ui:field not to a button in ui.xml file - It will fail at compile time. Hope it helped