【发布时间】:2012-07-21 19:29:51
【问题描述】:
我有这个 ListEditor 负责为客户编辑许可证。在我的CustomerEditor 中,LicensesEditor 绑定到路径许可证,其类型为List<License>>。
客户 (1)--> (许多) 许可证 (java.util.List)
现在,验证规则规定每位客户至少需要一份许可。约束违规工作完美。但是如何为 ListEditor 实现 HasEditorErrors ...我想提供自己的错误标记。
我为 HasEditorErrors 的泛型参数 T 注入什么类型??
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>
[错误] [klawtapp] 在代理中找不到路径编辑器的吸气剂 输入 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("");
}
}
【问题讨论】:
-
这是因为它是一个列表接口,我需要一个像 ArrayList 这样的实现吗?
标签: validation gwt bean-validation