Search code examples
gwtgwtp

In GWTP how can TabLayoutPanel events be handled?


I've followed Dani's GWTP Course but using TabLayoutPanel with presenters isn't covered.

I have a TabLayoutPanel with 3 tabs (each with a VerticalPanel on it). I've used @ProxyCodeSplit so that the code for each tab is loaded independently.

If in Eclipse, in GWT's Designer I add a handler for OnBeforeSelection then code is auto-added into my View. The View can then load up the appropriate presenter.

That doesn't feel like the right place for the code - but is it?

How are you handing different tabs within TabLayoutPanel and code splitting?


Solution

  • I think I've got this figured out.

    In your presenter with the TabLayoutPanel (let's call it MainPresenter):

    @ContentSlot public static final Type<RevealContentHandler<?>> SLOT_first = new Type<RevealContentHandler<?>>();
    @ContentSlot public static final Type<RevealContentHandler<?>> SLOT_second = new Type<RevealContentHandler<?>>();
    
    public interface MyView extends View {
        public void setMainPresenter(MainPresenter presenter);
        public TabLayoutPanel getTeamsPanel();
    }
    
    @Inject PlaceManager placeMananger;
    @Inject FirstPresenter firstPresenter;
    @Inject SecondPresenter secondPresenter;
    
    @ProxyCodeSplit
    public interface MyProxy extends Proxy<MainPresenter> {
    }
    
    @Inject
    public MainPresenter(final EventBus eventBus, final MyView view,
            final MyProxy proxy) {
        super(eventBus, view, proxy);
        view.setMainPresenter(this);
    }
    
    @Override
    protected void revealInParent() {
        RevealRootContentEvent.fire(this, this);
    }
    
    public void setTabContents(Integer tab) {
        if (tab == 0) {
            placeMananger.revealPlace(new PlaceRequest("first"));
        } else if (tab == 1) {
            placeMananger.revealPlace(new PlaceRequest("second"));
    }
    

    Then in your MainView implement the method setMainPresenter() to store a reference locally. Implement the usual setInSlot() and then add this tab handler:

    @UiHandler("mainTabs")
    void onMainTabsBeforeSelection(BeforeSelectionEvent<Integer> event) {
        mainPresenter.setTabContents(event.getItem());
    }
    

    The handler will call MainPresenter each time the user changes tabs. setTabContents() will then call revealInParent() for the appropriate "tab" Presenter.