GWT ListEditor实现HasEditorErrors,ListEditor的自定义错误标记

时间:2012-07-21 19:29:52

标签: validation gwt bean-validation

我有这个ListEditor负责编辑客户的许可证。在我的CustomerEditor中,LicensesEditor绑定了路径许可证,类型为List<License>>

  

客户(1) - &gt; (很多)许可证(java.util.List)

现在,验证规则规定每个客户至少需要一个许可证。约束违规工作完美。但是如何为ListEditor实现HasEditorErrors ...我想提供自己的错误标记。

我为HasEditorErrors的通用参数T注入了什么类型&lt; T&gt; ??

public interface HasEditorErrors<T> extends Editor<T>

ListEditor的签名如下......

public class LicensesEditor extends Composite implements IsEditor<ListEditor<License, LicenseInListEditor>>

我尝试实现HasEditorErrors<List<License>>,它在运行时提供错误:

  

[DEBUG] [klawtapp] - 下降到许可证   [错误] [klawtapp] - 在代理类型java.util.List中找不到路径编辑器的getter

如果我实施HasEditorErrors<LicensesEditor>

  

[ERROR] [klawtapp]无法在代理中找到路径编辑器的getter   输入com.klawt.screen.ui.customers.LicensesEditor

将HasEditorErrors中的问号保留为已实现的接口会产生编译时错误:

  

LicensesEditor类型无法扩展或实现HasEditorErrors。   超类型可能不指定任何通配符LicensesEditor.java

有人吗?

更新,完整代码如下:

public class LicensesEditor extends Composite implements IsEditor<ListEditor<License, LicenseInListEditor>> {

    @UiField
    Image validationErrorIcon;

    interface LicensesEditorUiBinder extends UiBinder<Widget, LicensesEditor> {
    }

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

    @UiField
    VerticalPanel container;

    ListEditor<License, LicenseInListEditor> editor;

    public LicensesEditor() {
        initWidget(uiBinder.createAndBindUi(this));
        editor = ListEditor.of(new LicenseInListEditorSource());
        clearErrors();
    }

    @Override
    public ListEditor<License, LicenseInListEditor> asEditor() {
        return editor;
    }

    public void addLicense(License emailAddress) {
        editor.getList().add(emailAddress);
        if (emailAddress.getAdministrator()) {
            setPrimary(editor.getList().size() - 1);
        }
    }

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

    public void update(int index, License emailAddress) {
        editor.getList().remove(index);
        editor.getList().add(index, emailAddress);
    }

    /**
     * make the phonenumber at the index the primary phonenumber and the other
     * phone numbers not primary.
     * 
     * @param index
     */
    public void setPrimary(int index) {
        int loop = 0;

        for (License emailAddress : Collections.unmodifiableList(editor.getList())) {
            emailAddress.setAdministrator(index == loop);
            update(loop, emailAddress);
            loop++;
        }
    }

    private class LicenseInListEditorSource extends EditorSource<LicenseInListEditor> {

        @Override
        public LicenseInListEditor create(final int index) {
            LicenseInListEditor editor = new LicenseInListEditor();
            editor.addDeleteHandler(new ListEditorDeleteEventHandler() {
                @Override
                public void onEditorEvent(ListEditorDeleteEvent event) {
                    remove(index);
                }
            });
            editor.addUpdateHandler(new EditorUpdateEventHandler() {
                @Override
                public void onEditorUpdate(EditorUpdateEvent event) {
                    License emailAddress = (License) event.getUpdated();
                    update(index, emailAddress);
                    if (emailAddress.getAdministrator()) {
                        setPrimary(index);
                    }
                }
            });
            container.insert(editor, index);
            updateOddEven();
            return editor;
        }

        @Override
        public void dispose(LicenseInListEditor subEditor) {
            container.remove(subEditor);
            updateOddEven();
        }

        @Override
        public void setIndex(LicenseInListEditor editor, int index) {
            container.insert(editor, index);
            updateOddEven();
        }

        public void updateOddEven() {
            for (int widgetIndex = 0; widgetIndex < container.getWidgetCount(); widgetIndex++) {
                container.getWidget(widgetIndex).setStyleName(KlawtResources.INSTANCE.form().listEditorEven(),
                        (widgetIndex % 2 == 0));
                container.getWidget(widgetIndex).setStyleName(KlawtResources.INSTANCE.form().listEditorOdd(),
                        (widgetIndex % 2 == 1));
            }
        }

    }

    // @Override
    // public void showErrors(List<EditorError> errors) {
    // StringBuilder sb = new StringBuilder();
    // int errorCount = 0;
    // for (EditorError editorError : errors) {
    // if (errorCount > 0) {
    // sb.append("\n");
    // }
    // errorCount++;
    // sb.append(editorError.getMessage());
    // }
    // if (errorCount == 0) {
    // clearErrors();
    // } else {
    // container.setStyleName(KlawtResources.INSTANCE.form().formError(), true);
    // validationErrorIcon.setVisible(true);
    // validationErrorIcon.setTitle(sb.toString());
    // }
    // }

    private void clearErrors() {
        container.setStyleName(KlawtResources.INSTANCE.form().formError(), false);
        validationErrorIcon.setVisible(false);
        validationErrorIcon.setTitle("");
    }

}

1 个答案:

答案 0 :(得分:3)

T不是问题,混合和匹配EditorIsEditor是合并的(我怀疑 - 没有发布超过类型decls很难说)一些内部of LicensesEditor(即ListEditor成员的访问修饰符,我认为你称之为editor)。

IsEditor<E>接口用于表示“我自己不是编辑,但我可以提供一个”,其实现Editor的非私有成员将被忽略,但框架仍然会进入E中的非私人编辑。因为(我怀疑)你的LicensesEditor内部实际上有一个非私人编辑成员,

那么这会让你离开?几个似乎对我有用的选择:

  • 停止使用IsEditor,以便您可以实施HasEditorErrors。相反,make LicensesEditor extends Composite implements Editor<List<Licenses>>, HasEditorErrors<List<Licenses>>(虽然技术上第一个Editor被第二个覆盖,但我发现它有助于提高可读性)。现在你仍然需要一个ListEditor成员(你几乎肯定已经有了),但是用@Path("")注释它以表明它应该编辑整个编辑器得到的相同值。

  • 使用IsEditor,但在HasEditorErrorsListEditor上使其成为通用的 - 创建一个同样实现HasEditorErrors并且有错误支持的ListEditor子类。这允许您仍然在包装类中扩展Composite。转过来,你就......

  • 扩展ListEditor,并实施IsWidget,而不是扩展复合。这实现了与第一个选项相同的效果(您可以通过删除LicensesEditor)来HasEditorErrors实现IsEditor,而不使用@Path来使事情变得有意义。现在,不要调用Composite.initWidget,而是保留对窗口小部件的引用,并将其返回IsWidget.asWidget()

  • 最后,什么都不做。来自IsEditor Javadocs:

      

    类型实现Editor和IsEditor是合法的。在这种情况下,   从{@link #asEditor()}返回的编辑将是该编辑的共同编辑   IsEditor实例。

    这意味着LicensesEditor应该是有效的Editor - any non-private Editor member should either be changed to private or tagged with @ Ignore`。