如何在GWT平台中将参数传递给UiBinder的构造函数视图(通过使用setInSlot)?

时间:2014-01-07 03:16:24

标签: gwt gwt-platform gwtp

很难找到有关GWTP(GWT平台)的问题。

好的,这是我的故事。我正在使用GWTP& eclipse自动创建Presenter-View结构。

示例,我在eclipse中创建了一个TestPresenter,&它创建了3个文件:TestPresenter.java,TestView.java,TestView.xml

在TestView.xml中,我有:

  <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" /> 

现在我想为每个TestView自动设置GroupName,所以在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;
    }
}

在TestPresenter.java中,

public class TestPresenter extends
        PresenterWidget<TestPresenter.MyView> {

    public interface MyView extends View {

        public RadioButton getFirstRadioButton();

        public RadioButton getSecondRadioButton();

        public RadioButton getBothRadioButton();
    }
}

好的,最后我想在MainPresenter.java中使用很多TestPresenter(通过使用setInLot)

所以,在MainPresenter.java中,我有:

    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
在MainView.java中的

,我有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
}

现在,如果我只是这样做,那么我不能为每个TestPresenter&amp;分别传递groupName。这是不好的。所以我想为每个TestPresenter传递groupName字符串,以便每个都有自己的groupName。这样的东西

    @Inject TestPresenter testPresenter1 ("group1");  

    @Inject TestPresenter testPresenter2 ("group2");

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

但我不知道怎么做,所以请告诉我如何在GWTP中正确使用它?

2 个答案:

答案 0 :(得分:2)

如果要在TestPresenter的构造函数中指定groupName,可能需要使用的是assisted injection"

然后你可能会得到这样的东西(我没有测试过代码):

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

并在您的杜松子酒模块中:

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

然后代替:

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

您将在 ParentPresenter 的构造函数中注入 TestPresenterFactory 以创建所有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");
}

TestPresenter.java 中的@Assisted注释:

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

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

但是你真的需要你的TestPresenters知道他们的视图中RadioButtons使用的groupName吗?

答案 1 :(得分:0)

好的,我还没有测试过Alexis的解决方案,但他对“setGroupName”的想法引发了我的注意,所以我可以调整我的代码abit&amp;它工作正常。

在TestPresenter.java中,我有这个方法

    public void setGroupName(String groupName) {
        getView().getFirstRadioButton().setName(groupName);
        getView().getSecondRadioButton().setName(groupName);
        getView().getBothRadioButton().setName(groupName);
    }
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");
    ....
}