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

StackOverflow https://stackoverflow.com/questions/20963292

  •  25-09-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution 2

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");
    ....
}

OTHER TIPS

If you want to specify a groupName in constructors of TestPresenter why you might need to use is assisted injection".

Then you would probably end up with something like this (I have not tested the code):

public interface TestPresenterFactory {
    TestPresenter create(String groupName);
}

and in your Gin Module:

@Override
protected void configure() {
    ...
    install(new GinFactoryModuleBuilder().build(TestPresenterFactory.class));
}

Then instead of:

@Inject TestPresenter testPresenter1 ("group1");  
@Inject TestPresenter testPresenter2 ("group2");
@Inject TestPresenter testPresenter3 ("group3");
@Inject TestPresenter testPresenter4 ("group4");
...

you would inject the TestPresenterFactory in the constructor of ParentPresenter to create all TestPresenters:

private TestPresenter testPresenter1;  
private TestPresenter testPresenter2;
private TestPresenter testPresenter3;
private TestPresenter testPresenter4;

@Inject
public ParentPresenter(final EventBus eventBus, final ParentView view, final ParentProxy proxy, final TestPresenterFactory factory)   
{
    ...
    testPresenter1 = factory.create("group1");
    testPresenter2 = factory.create("group2");
    testPresenter3 = factory.create("group3");
    testPresenter4 = factory.create("group4");
}

And the @Assisted annotation in the TestPresenter.java:

public interface MyView extends View {
    ...
    public void setRadioButtonsGroupName(String groupName);
}

@Inject
public TestPresenter(final EventBus eventBus, final MyView view, @Assisted String groupName)   
{
    ...
    view.setRadioButtonsGroupName(groupName);
}

And TestView.java:

public class TestView extends ViewImpl implements TestPresenter.MyView {
    private final Widget widget;
    @UiField RadioButton firstRadioButton;
    @UiField RadioButton secondRadioButton;
    @UiField RadioButton bothRadioButton;

    ...

    public void setRadioButtonsGroupName(String groupName) {
        firstRadioButton.setName(groupName);
        secondRadioButton.setName(groupName);
        bothRadioButton.setName(groupName);
    }
}

But do you really need your TestPresenters to be aware of the groupName used by the RadioButtons in their views ?

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