Question

I'm trying to bind a GWT view with its presentation layer, but it doesn't seem to be doing anything.

It's a Spring Roo GWT generated project and I'm trying to use the scaffold given as far as possible. The view is a simple button (R.ui.xml) and the rest of the view is defined in R.java:

public class R extends Composite implements RPresenter.Display {

    interface MyUiBinder extends UiBinder<Widget, R> {}
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField Button myButton;

    private ClickHandler buttonClickHandler = null;

    public R(){
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("myButton")
    void onButtonClick(ClickEvent event){
        GWT.log('Button clicked');
        if (buttonClickHandler != null){    
            GWT.log("buttonClickHandler event triggered");
            buttonClickHandler.onClick(event);
        }
    }

    @Override
    public void setButtonClickHandler(ClickHandler buttonClickHandler) {

        GWT.log("setButtonClickHandler");
        this.buttonClickHandler = buttonClickHandler;
    }

}

The presenter:

public class RPresenter {

    public interface Display extends IsWidget {
        void setButtonClickHandler(ClickHandler buttonClickHandler);
    }

    private final Display display;
    private final EventBus eventBus;

    @Inject
    public RPresenter(EventBus eventBus, Display display){
        this.display = display;
        this.eventBus = eventBus;
        bind();
    }

    private void bind(){
        display.setButtonClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                GWT.log("onClick event triggered");
            }
        });
    }

    public void go(HasWidgets container){
        container.add(display.asWidget());
    }

}

And for my GIN module I use the generated ScaffoldModule in the ...client.scaffold.ioc package:

public class ScaffoldModule extends AbstractGinModule {

    @Override
    protected void configure() {

        GWT.log("ScaffoldModule configure");

        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(ApplicationRequestFactory.class).toProvider(RequestFactoryProvider.class).in(Singleton.class);
        bind(PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);

        //bind(RPresenter.Display.class).to(R.class).in(Singleton.class);
        bind(RPresenter.Display.class).to(R.class);
    }

    static class PlaceControllerProvider implements Provider<PlaceController> {

        private final EventBus eventBus;

        @Inject
        public PlaceControllerProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public PlaceController get() {
            return new PlaceController(eventBus);
        }
    }

    static class RequestFactoryProvider implements Provider<ApplicationRequestFactory> {

        private final EventBus eventBus;

        @Inject
        public RequestFactoryProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public ApplicationRequestFactory get() {
            ApplicationRequestFactory requestFactory = GWT.create(ApplicationRequestFactory.class);
            requestFactory.initialize(eventBus);
            return requestFactory;
        }
    }
}

In the GWT development mode console, the "ScaffoldModule configure" never displays, yet the generated scaffold seems to binding just fine as the events get passed along from component to component without a hitch, unless the binding is magically happening somewhere else and that is dead code.

When I put my bind(RPresenter.Display.class).to(R.class) in, it doesn't seem to do the binding. The only output I get in the GWT console is "Button clicked" which is called in the view and then nothing further. I'm clearly missing something, any ideas?

Was it helpful?

Solution

The call to GWT.log() will not output anything from an AbstractGinModule - classes that extend AbstractGinModule (ScaffoldModule in your situation) are used by gin at compile time to decide which concrete implementations to use for injected interfaces. From the rest of your description (i.e. that the UI shows up in the application) it appears that your dependency injection is working correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top