很难找到有关GWTP(GWT平台)的问题。
好的,这是我的故事。我正在使用GWTP&eclipse自动创建Presenter视图结构。
例如,我在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中,我设置了InSlot
@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&这不好。因此,我想为每个TestPresenter传递groupName字符串,这样每个人都有自己的groupName。有点像这个
@Inject TestPresenter testPresenter1 ("group1");
@Inject TestPresenter testPresenter2 ("group2");
@Inject TestPresenter testPresenter3 ("group3");
@Inject TestPresenter testPresenter4 ("group4");
...
但我不知道如何正确使用它,所以请告诉我如何在GWTP中正确使用它?
如果你想在TestPresenter的构造函数中指定一个groupName,为什么你可能需要使用辅助注入
然后你可能会得到这样的东西(我还没有测试代码):
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以创建所有TestPresenter:
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);
}
}
但是,您真的需要您的TestPresenter了解RadioButtons在其视图中使用的groupName吗?
好吧,我还没有测试Alexis的解决方案,但他关于"setGroupName"的想法触发了我的想法,这样我就可以调整我的代码了;它运行良好。
在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");
....
}