Search code examples
gwtmvpgwtpgwt-platform

GWT: Adding a SubmitHandler to a Form without putting logic in View


I have a GWTP PresenterWidget and View pair that contains a simple search form.

Currently I am adding the SubmitHandler to the form by calling the getSearchForm() method of my View, which is bad practice as it references the actual class instead of an interface:

public class HeaderPresenter extends PresenterWidget<HeaderPresenter.MyView>
{
    public interface MyView extends View
    {
        void submitForm();
        Form getSearchForm();
    }

    // ...omitted for brevity

    @Inject
    public HeaderPresenter(EventBus eventBus, DispatchAsync dispatchAsync, MyView view, PlaceManager placeManager)
    {
         // ...omitted for brevity
    }

    @Override
    protected void onBind()
    {
        super.onBind();         

        getView().getSearchForm().addSubmitHandler(new SubmitHandler()
        {
            @Override
            public void onSubmit(SubmitEvent event)
            {
                // stops the form submission
                event.cancel();

                // now we can do our stuff
                String query = getView().getSearchQuery();

                if(query != "") // don't search for a blank string
                {
                    PlaceRequest request = new PlaceRequest.Builder().nameToken(NameTokens.search).with("q", query).build();
                    placeManager.revealPlace(request);
                }
            }
        });
}

Is there a way to add the SubmitHandler in the HeaderPresenter, or will I have to put that code in the View?

I'd like to keep as much logic in the Presenter as possible.


Solution

  • I found a discussion on this here, and ended up using option 4 as suggested by Thomas Broyer:

    https://groups.google.com/forum/#!topic/google-web-toolkit/Fbo-SEDjRa4