Search code examples
gwtgwt-platformgwtp

How to pass parameter into Constructor View of UiBinder (by using setInSlot) in GWT Platform?


It's very hard to find questions about GWTP (GWT Platform).

Ok, Here is my Story. I am using GWTP & eclipse to create the Presenter-View structure automatically.

Example, I created a TestPresenter in eclipse, & it created 3 files: TestPresenter.java, TestView.java, TestView.xml

In TestView.xml, i have:

  <g:RadioButton ui:field="firstRadioButton" value="false" text="1st" /> 

  <g:RadioButton ui:field="secondRadioButton"  value="false" text="2nd" /> 

  <g:RadioButton ui:field="bothRadioButton" value="true" text="Both" /> 

Now I want to set the GroupName automatically for each TestView, so in TestView.java

public class TestView extends ViewImpl implements
        TestPresenter.MyView {
    private final Widget widget;
    @UiField RadioButton firstRadioButton;
    @UiField RadioButton secondRadioButton;
    @UiField RadioButton bothRadioButton;
    private String groupName;
    @UiFactory
    RadioButton makeRadioButton() {
        return new RadioButton(groupName);
    }
    public interface Binder extends UiBinder<Widget, TestView> {
    }

    @Inject
    public TestView(final Binder binder) {
        widget = binder.createAndBindUi(this);
    }

    @Override
    public Widget asWidget() {
        return widget;
    }

    public RadioButton getFirstRadioButton() {
        return firstRadioButton;
    }

    public RadioButton getSecondRadioButton() {
        return secondRadioButton;
    }

    public RadioButton getBothRadioButton() {
        return bothRadioButton;
    }
}

In TestPresenter.java,

public class TestPresenter extends
        PresenterWidget<TestPresenter.MyView> {

    public interface MyView extends View {

        public RadioButton getFirstRadioButton();

        public RadioButton getSecondRadioButton();

        public RadioButton getBothRadioButton();
    }
}

Ok, finally I want to use many TestPresenter (by using setInLot) in MainPresenter.java

So, in MainPresenter.java, I have:

    public static final Object SLOT1=new Object();
    public static final Object SLOT2=new Object();
    public static final Object SLOT3=new Object();
    public static final Object SLOT4=new Object();

//...... more lot

    @Inject TestPresenter testPresenter1;       
    @Inject TestPresenter testPresenter2;  
    @Inject TestPresenter testPresenter3;  
    @Inject TestPresenter testPresenter4;
//.. more test presenter

in MainView.java, i have setInSlot

@UiField HTMLPanel mainHtmlPanel;
@Override
public void setInSlot(Object slot, Widget content){
      if(slot==MainPresenter.SLOT1){
            mainHtmlPanel.clear();
            if(content!=null){
                mainHtmlPanel.add(content);
            }
        }
        else if(slot==MainPresenter.SLOT2){
            mainHtmlPanel.clear();
            if(content!=null){
                mainHtmlPanel.add(content);
            }
        }
    //... more else if here
}

Now, if i just do like that then I can not pass the groupName separately for each TestPresenter & that is not good. So I want to pass the groupName string for each TestPresenter so that each will have their own groupName. SOmething like this

    @Inject TestPresenter testPresenter1 ("group1");  

    @Inject TestPresenter testPresenter2 ("group2");

    @Inject TestPresenter testPresenter3 ("group3");
    @Inject TestPresenter testPresenter4 ("group4");
...

but I don't know how to it properly, so please tell me how to it properly in GWTP?


Solution

  • Ok, I haven't test Alexis' solution, but his idea of "setGroupName" trigger my mind so I can adjust my code abit & it works fine.

    In TestPresenter.java, I have this method

        public void setGroupName(String groupName) {
            getView().getFirstRadioButton().setName(groupName);
            getView().getSecondRadioButton().setName(groupName);
            getView().getBothRadioButton().setName(groupName);
        }
    

    in MainPresenter.java

    @Inject TestPresenter testPresenter1;       
    @Inject TestPresenter testPresenter2; 
    ....
    @Override
    protected void onReset() {
            super.onReset();
        setInSlot(SLOT1, testPresenter1);
        setInSlot(SLOT2, testPresenter2);
        .....
        testPresenter1.setGroupName("group1");
        testPresenter2.setGroupName("group2");
        ....
    }