如何为ListEditor设置支持列表

时间:2014-07-21 12:51:21

标签: gwt

我已经说过here我目前正在尝试进入gwt编辑器。 我想我错过了一个支持列表来保存我操纵的数据。 我尝试从父视图分配带有setValue调用的支持列表。现在编译器抱怨它缺少groupList的getter。 据我所知,按照惯例,groupList属性是通过命名编辑器groupListdditor来派生的。附加清单的正确方法是什么?似乎我需要以某种方式调用setValue与列表,否则它似乎不起作用。什么是正确的方法呢?

我的编辑器看起来像这样:

public class GroupListEditor extends Composite implements
        IsEditor<ListEditor<String, GroupItemEditor>> {

    private static StringListEditorUiBinder uiBinder = GWT
            .create(StringListEditorUiBinder.class);

    interface StringListEditorUiBinder extends
            UiBinder<Widget, GroupListEditor> {
    }

    @UiField
    FlowPanel pWidget;

    @UiField
    PushButton bAdd;

    @UiField
    FlowPanel pList;


    private class StringItemEditorSource extends EditorSource<GroupItemEditor> {
        @Override
        public GroupItemEditor create(final int index) {
            GroupItemEditor subEditor = new GroupItemEditor();
            pList.insert(subEditor, index);
            subEditor
                    .addDeleteHandler(new EditorDeleteEvent.EditorDeleteHandler() {
                        public void onEditorDeleteEvent(EditorDeleteEvent event) {
                            remove(index);
                        }
                    });
            return subEditor;
        }

        @Override
        public void dispose(GroupItemEditor subEditor) {
            subEditor.removeFromParent();
        }

        @Override
        public void setIndex(GroupItemEditor editor, int index) {
            pList.insert(editor, index);
        }
    }

    private ListEditor<String, GroupItemEditor> editor = ListEditor
            .of(new StringItemEditorSource());



    public GroupListEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }


    @UiHandler("bAdd")
    void onBAddClick(ClickEvent event) {
        Log.debug("Add button clicked");
        add();
    }

    private void add() {
        String s = "";
        //TODO: Problem is there is no backing list, FIx this
        editor.getList().add(s);
    }

    @Override
    public ListEditor<String, GroupItemEditor> asEditor() {
        return editor;
    }

    private void remove(final int index) {
        editor.getList().remove(index);
    }

}

编辑器在编辑器小部件中用作子编辑器,如下所示: 我试图设置支持列表:

public class ContainerEditorDialogPresenterWidget extends PresenterWidget<ContainerEditorDialogPresenterWidget.MyView> implements
                ContainerEditorDialogUiHandlers {


    private final PlaceManager placeManager;

    @Inject
    ContainerEditorDialogPresenterWidget(EventBus eventBus,
                               MyView view, PlaceManager placeManager) {
        super(eventBus, view);
        getView().setUiHandlers(this);
        this.eventBus = eventBus;
        this.placeManager = placeManager;
    }
    /**
     * {@link LocalDialogPresenterWidget}'s PopupView.
     */
    public interface MyView extends PopupView, ContainerEditView<ContainerDto>, HasUiHandlers<ContainerEditorDialogUiHandlers> {
    }

    private ContainerDto currentContainerDTO = null;
    private DeviceDto currentDeviceDTO = null;
    private final EventBus eventBus;

    private SimpleBeanEditorDriver<ContainerDto, ?> driver;

    public ContainerDto getCurrentContainerDTO() {
        return currentContainerDTO;
    }

    public void setCurrentContainerDTO(ContainerDto currentContainerDTO) {
        this.currentContainerDTO = currentContainerDTO;
    }

    public void setCurrentDeviceDTO(DeviceDto currentDeviceDTO) {
        this.currentDeviceDTO = currentDeviceDTO;
    }


    @Override
    public void onReveal() {
        super.onReveal();
        driver.edit(currentContainerDTO); 
    }

    @Override
    protected void onBind() {
        super.onBind();
        driver = getView().createEditorDriver();
    }

    @Override
    public void updateContainer() {
        ContainerDto dev = driver.flush();
        eventBus.fireEvent(new ContainerUpdatedEvent(dev));
    }

}

我尝试在视图中分配支持列表(只是一个空字符串列表):

public class ContainerEditorDialogView extends
        PopupViewWithUiHandlers<ContainerEditorDialogUiHandlers> implements
        ContainerEditorDialogPresenterWidget.MyView, Editor<ContainerDto> {
    interface Binder extends UiBinder<PopupPanel, ContainerEditorDialogView> {
    }
    public interface Driver extends SimpleBeanEditorDriver<ContainerDto, ContainerEditorDialogView> {
    }

    @UiField
    TextBox uuid;   
    @UiField
    TextBox name;   
    @UiField
    TextBox groups;
    //@UiField
    //TextBox device;
    //public TextBox getDevice() {
    //  return device;
    //}
    @UiField     
    GroupListEditor groupListEditor;
    @UiField
    TextBox imei;   
    @UiField
    TextBox type;   
    @UiField
    TextBox user;   
    @UiField
    Button okButton;
    @UiField
    Button cancelButton;


    @Inject
    ContainerEditorDialogView(Binder uiBinder, EventBus eventBus) {
        super(eventBus);

        initWidget(uiBinder.createAndBindUi(this));
        ListEditor<String, GroupItemEditor> ed = null;
        groupListEditor.asEditor().setValue(new ArrayList<String>());
    }

    @Override
    public SimpleBeanEditorDriver<ContainerDto, ?> createEditorDriver() {
        Driver driver = GWT.create(Driver.class);
        driver.initialize(this);
        return driver;
    }

    //Should this handled by a presenter?
    @UiHandler("okButton")
    void okButtonClicked(ClickEvent event) {

        getUiHandlers().updateContainer();

        hide();
    }

    @UiHandler("cancelButton")
    void cancelButtonClicked(ClickEvent event) {

        hide();
    }

}

谢谢!

更新 没有单独的ArrayList的GroupListEditor的版本看起来像这样,并且是我开始使用的:

public class GroupListEditor extends Composite implements
        IsEditor<ListEditor<String, GroupItemEditor>> {

    private static StringListEditorUiBinder uiBinder = GWT
            .create(StringListEditorUiBinder.class);

    interface StringListEditorUiBinder extends
            UiBinder<Widget, GroupListEditor> {
    }

    @UiField
    FlowPanel pWidget;

    @UiField
    PushButton bAdd;

    @UiField
    FlowPanel pList;

    private class StringItemEditorSource extends EditorSource<GroupItemEditor> {
        @Override
        public GroupItemEditor create(final int index) {
            GroupItemEditor subEditor = new GroupItemEditor();
            pList.insert(subEditor, index);
            subEditor
                    .addDeleteHandler(new EditorDeleteEvent.EditorDeleteHandler() {
                        public void onEditorDeleteEvent(EditorDeleteEvent event) {
                            remove(index);
                        }
                    });
            return subEditor;
        }

        @Override
        public void dispose(GroupItemEditor subEditor) {
            subEditor.removeFromParent();
        }

        @Override
        public void setIndex(GroupItemEditor editor, int index) {
            pList.insert(editor, index);
        }
    }

    private ListEditor<String, GroupItemEditor> editor = ListEditor
            .of(new StringItemEditorSource());



    public GroupListEditor() {
        initWidget(uiBinder.createAndBindUi(this));
    }


    @UiHandler("bAdd")
    void onBAddClick(ClickEvent event) {
        Log.debug("Add button clicked");
        add();
    }

    private void add() {
        String s = "Stuff";
        editor.getList().add(s);
    }

    @Override
    public ListEditor<String, GroupItemEditor> asEditor() {
        return editor;
    }

    private void remove(final int index) {
        editor.getList().remove(index);
    }

}

因为我将编辑器声明为

@UiField     
GroupListEditor groupListEditor;

ContainerDto所需的getter是不是应该命名为getGroupList()? 你指的是DeviceDto,它只是随身携带,不应该干扰编辑器。 由于GroupListEditor实现

IsEditor<ListEditor<String, GroupItemEditor>>

它应该是一个List,对吗? 我的ContainerDto有字段

protected ArrayList<String> groupList;

所以我认为应该没问题。这是我尝试手动调用setValue之前的起点。 当我这样时,我得到这个错误,点击“添加”按钮。

Caused by: java.lang.NullPointerException: null     at 
testproject.client.application.containers.editor.GroupListEditor.add(GroupListEditor.java:81)
at testproject.client.application.containers.editor.GroupListEditor.onBAddClick(GroupListEditor.java:76)

引用editor.getList().add(s),表示没有列表...

更新2: 我将UIField的声明更改为:

@UiField
@Path("groupList")
GroupListEditor groupListEditor;

但是在尝试像以前一样向列表添加内容时,我仍然会得到NullPointerException:editor.getList().add(s);

1 个答案:

答案 0 :(得分:0)

您无需手动呼叫setList即可将列表传递给SubEditor。这应由父/容器编辑器为您处理。

然而,编辑器将使用UiField名称来匹配您的支持DTO中的相应getter

在上面的示例中,只有当您的DeviceDTO有一个getGroupListEditor() getter返回您的ListEditor期望的类型(由于@UiField GroupListEditor groupListEditor;时)时才会有效。

如果您的DeviceDTO不包含相应的getter,则可以执行以下操作:

  1. 重命名@UiField GroupListEditor groupListEditor;(即groupItems
  2. getter中的DeviceDTO重命名为getGroupListEditor
  3. Path('groupItems')添加到您的@UiField GroupListEditor groupListEditor;
  4. 解决方案3通常是要走的路:

    @UiField     
    @Path('groupItems')
    GroupListEditor groupListEditor;
    

    注意:更改groupItems必须与getter中返回组项的DeviceDTO相匹配。

    <强>更新

    这就是DeviceDTO的外观,以及您如何使用Path将编辑器指向正确的getter。只要您的groupList类变量初始化为空ArrayList,一切都应该可以正常工作。

    class DeviceDTO {
      protected List<String> groupList = new ArrayList<String>();
      public List<String> getGroupList() {
          return groupList;
      }
    }
    
    public class ContainerEditorDialogView extends
        PopupViewWithUiHandlers<ContainerEditorDialogUiHandlers> implements
        ContainerEditorDialogPresenterWidget.MyView, Editor<ContainerDto> {
    
        ....
        @UiField     
        @Path("groupList")
        GroupListEditor groupListEditor;     
    }