【问题标题】:Using GWT-Editors without UiBinder在没有 UiBinder 的情况下使用 GWT 编辑器
【发布时间】:2015-01-10 10:00:59
【问题描述】:

我想通过使用编辑器绑定 POJO 来改进我的 gwt 项目的代码,我过去常常手动地来回解析到小部件。但是我发现documentation 令人困惑,主要是因为它引用了ui binder,这是我还没有弄清楚的另一个功能。

在没有 ui binder 的情况下使用编辑器有意义吗?我的 ParentDTO 包含许多 childDTO。以下 sn-p 显示了我如何尝试将一些由 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 类型的父编辑器直接保存子 dto?

【问题讨论】:

    标签: java user-interface gwt uibinder


    【解决方案1】:

    在没有 ui binder 的情况下使用编辑器有意义吗?

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

    我什至需要单独的编辑器还是只需要 ListEditor 类型的父编辑器直接保存子 dto?

    你可以直接创建一个ListEditor 实现,但是如果你告诉它如何通过扩展EditorSource&lt;ChildDTOEditor&gt; 并使用ListEditor.of(new YourChildDTOEditorSourceImpl()) 来创建和销毁Editor&lt;ChildDTO&gt;,GWT 可以为你做这件事

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 2018-04-13
      • 2013-03-29
      • 1970-01-01
      相关资源
      最近更新 更多