Search code examples
gwtdependency-injectionuibindergwt-gin

GWT & GIN: how to inject PlaceController into a widget using UIBinder


I have an InlineLabel subclass that I used in UIBinder. How can I inject a PlaceController into it via GIN, so that the widget can be used in UIBinder? (recall that UIBinder requires a no-args constructor.)

If that's not possible, what's the cleanest way to make a PlaceController available to a widget so that it can be used by the widget during onClick() events?

Edit:

I'm not sure MVP is really the best solution in this case (I'm happy to have you change my mind, however.)

I will have dozens of these InlineLabel instances declared in my UIBinder foo.ui.xml file. If I implement MVP, that means declaring each of these instances as @UiField members in the view. That gets rather unwieldy when I have so many of them. That's why I was hoping to inject the PlaceController into each of the InlineLabels in a semi-automated way, and avoid having to manually wire them into the view.

It would also be acceptable if there were a way to inject the presenter into each of the InlineLabels... then the delegation could be done something like:

   public class MyInlineLabelSubclass {
       // ...

       public void onClick(ClickEvent event)
       {
         presenter.labelClicked(this);
       }
    }

Solution

  • You can use the @UiHandler annotation to add a handler to a UiBinder element without having a @UiField reference:

    <ui:UiBinder>
      <g:InlineLabel ui:field="name"/>
      <g:InlineLabel ui:field="address"/>
      <g:InlineLabel ui:field="zipCode"/>
    </ui:UiBinder>
    

    And in the view:

    @UiHandler({"name","address","zipCode"})
    void onClick(ClickEvent event) {
      // Source will be one of the three InlineLabels.
      presenter.labelClicked(event.getSource());
    }
    

    Don't give your widget a direct handle to a PlaceController - delegate to the view's Presenter (via. a callback or a Presenter interface). See http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html#Views