在没有 UiBinder 的情况下使用 GWT-Editors

Using GWT-Editors without UiBinder

我想通过使用编辑器绑定 POJO 来改进我的 gwt 项目的代码,我过去常常手动将其来回解析到小部件。但是我发现了 documentation confusing, mostly because it references ui binder,另一个我还没有弄清楚的功能。

在没有 ui 活页夹的情况下使用编辑器是否有意义?我的 ParentDTO 包含许多 childDTO。以下代码段显示了我如何尝试将一些由 TextArea 扩展的 ChildEditor 嵌套到我的 ParentEditor 中(试图将其简化为基本要素):

public class MyEditorPage {

    // editors
    class ParentDTOEditor implements Editor<ParentDTO> {
        Integer dataBaseId;
        List<ChildDTOEditor> childs;

        public void attach(RootPanel rootPanel) {
            for (ChildDTOEditor widget : childs) {
                rootPanel.add(widget);
            }
        }
    }
    class ChildDTOEditor implements Editor<ChildDTO> extends TextArea {}

    // driver
    interface Driver extends SimpleBeanEditorDriver<ParentDTO, ParentDTOEditor> {}
    Driver driver = GWT.create(Driver.class);

    // load set widgets to the root panel
    public void loadPage(RootPanel rootPanel) {

        // get pojos from server
        myService.getSuff(...
            ...
            public void onSuccess(ParentDTO result) {
                ParentDTOEditor editor = new ParentDTOEditor();
                driver.initialize(editor);
                driver.edit(result);
                editor.attach(rootPanel);                   
            }
    }

    // save
    public void save() {
        ParentDTO dto = driver.flush();
        ... // call myService.saveStuff(dto,...
    }
}

我什至需要单独的编辑器还是只需要一个 ListEditor 类型的父编辑器直接包含子 dtos?

Does it make sense to use editors without ui binder?

是的,这些功能是独立的,每个功能都可以单独使用。

Do I even need separate editors or just a parent editor of type ListEditor directly holds the child dtos?

您可以直接创建一个 ListEditor 实现,但是如果您告诉 GWT 如何通过扩展 EditorSource<ChildDTOEditor> 和使用 ListEditor.of(new YourChildDTOEditorSourceImpl())

有一个不错的例子here,但我会在这里提出一个最低限度的实现。

public class FooEditor extends Composite implements Editor<Foo> {

    // Implement one of uibinder+fields, fields, methods, or LeafValueEditor.set/getValue()

    public FooEditor() {
        initWidget(/* root widget or call to uiBinder.createAndBindUi(this) */)
    }

}

public class FooListEditor extends Composite implements IsEditor<ListEditor<Foo, FooEditor>> {

    private class FooEditorSource extends EditorSource<FooEditor> {
        @Override
        public FooEditor create(int index) {
            FooEditor subEditor = new FooEditor();

            // any additional per-item config can go here, e.g wiring up delete handler

            listPanel.insert(subEditor, index);

            return subEditor;
        }

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

        @Override
        public void setIndex(FooEditor subEditor, int index) {
            listPanel.insert(subEditor, index);
        }
    }

    // FlowPanel or anything else you want to use to hold the sub-editors.
    // Instantiated explicitly or through uibinder.
    FlowPanel listPanel = new FlowPanel(); 


    // Let GWT handle the ListEdiotr implementation
    ListEditor<Foo, FooEditor> editor = ListEditor.of(new FooEditorSource());

    public FooListEditor() {
        initWidget(listPanel /* or uiBinder.createAndBindUi(this) */);
    }

    @Override
    public ListEditor<Foo, FooEditor> asEditor() {
        return editor;
    }

}

现在您可以创建一个编辑器驱动程序并像使用普通编辑器一样使用它,但将其传递给一个列表。

interface Driver extends SimpleBeanEditorDriver<List<Foo>, ListEditor<Foo, FooEditor>> {}
Driver driver = GWT.create(Driver.class);
FooListEditor fooListEditor = new FooListEditor();

/* snip */

driver.initialize(FooListEditor);
driver.edit(someListOfFoo);