我一直在搜索并试图弄清楚我是否可以使用具有多态类型的编辑器框架。我在Using GWT Editors with a complex usecase找到了这篇文章,这篇文章与我要做的很接近。 我是编辑框架的新手,所以任何帮助都会非常感激。
例如,这里有一些代码,
数据传输对象:
public class Employee{
public List<Contact> contacts
}
public class Contact {
public class ContactEmail extends Contact {}
public class ContactAddress extends Contact {}
public class ContactPhoneNumber extends Contact {}
编辑:
public interface ContactBaseEditor<T extends Contact> extends Editor<T> {}
public class AddressEditor extends Composite implements Editor<ContactAddress>, ContactBaseEditor<ContactAddress>{}
public class EmailEditor extends Composite implements Editor<ContactEmail>, ContactBaseEditor<ContactEmail>{)
public class PhoneNumberEditor extends Composite implements Editor<ContactPhoneNumber>, ContactBaseEditor<ContactPhoneNumber>{}
ContactEditor类:
public class ContactEditor extends Composite implements IsEditor<ListEditor<Contact, ContactEditorWrapper>> {
private class ContactEditorSource extends EditorSource<ContactEditorWrapper> {
@Override
public ContactEditorWrapper create(final int index) {
ContactEditorWrapper contactEditor = new ContactEditorWrapper();
communicationContactsPanel.add(contactEditor);
return contactEditor;
}
@Override
public void dispose(ContactEditorWrapper subEditor) {
subEditor.removeFromParent();
}
@Override
public void setIndex(ContactEditorWrapper editor, int index) {
communicationContactsPanel.insert(editor, index);
}
}
private ListEditor<Contact, ContactEditorWrapper> editor = ListEditor.of(new ContactEditorSource());
public ListEditor<Contact, ContactEditorWrapper> asEditor() {
return editor;
}
}
ContactEditorWrapper:
class ContactEditorWrapper extends Composite implements ContactBaseEditor<Contact>, ValueAwareEditor<Contact> {
private SimplePanel panel = new SimplePanel();
@Path("") ContactBaseEditor<Contact> realEditor;
public ContactEditor() {
initWidget(panel);
}
@Override
public void setValue(Contact value) {
if (value instanceof Address) {
realEditor = new AddressEditor();
panel.setWidget((AddressEditor)realEditor);
}
else if (value instanceof Email) {
realEditor = new EmailEditor();
panel.setWidget((EmailEditor)realEditor);
}
else if (value instanceof PhoneNumber) {
realEditor = new PhoneNumberEditor();
panel.setWidget((PhoneNumberEditor)realEditor);
}
else {
realEditor = null;
}
}
}
主编辑课程:
public class AddEmployeeEditor extends Composite implements Editor<Employee> {
@UiField
ContactEditor contacts;
interface Driver extends SimpleBeanEditorDriver<Employee, AddEmployeeEditor> {
}
public AddEmployeeEditor(final Binder binder) {
driver = GWT.create(Driver.class);
driver.initialize(this);
List<Contact> list = new ArrayList<Contact>();
list.add(new Address());
list.add(new Email());
list.add(new PhoneNumber());
list.add(new PhoneNumber());
Employee employee = new Employee();
employee.setContacts(list);
driver.edit(employee);
}
}
任何人都可以告诉我这是否有效,我是朝着正确的方向前进的吗?
提前致谢, MAC
我已经更新了上面的代码,现在包含了Thomas建议的ContactEditorWrapper类。
答案 0 :(得分:0)
该代码有很好的破解机会:无法保证EditorSource
返回的编辑器不会用于编辑列表中的其他值。
您应该创建一个实现ValueAwareEditor
的包装器编辑器,并使用实际编辑器作为具有@Path("")
的子编辑器;并在ContactBaseEditor
方法中创建相应的setValue
。
class ContactEditor extends Composite implements ValueAwareEditor<Contact> {
private SimplePanel panel = new SimplePanel();
@Path("") ContactBaseEditor realEditor;
public ContactEditor() {
initWidget(panel);
}
@Override
public void setValue(Contact value) {
if (contact instanceof ContactAddress) {
realEditor = new AddressEditor();
}
else if (contact instanceof ContactEmail) {
realEditor = new EmailEditor();
}
else if (contact instanceof ContactPhoneNumber) {
realEditor = new PhoneNumberEditor();
}
else {
realEditor = null;
}
panel.setWidget(realEditor);
}
但请注意,只有ContactBaseEditor
中的字段/子编辑器才会被编辑,无论使用哪种实际实现。如果某个ContactBaseEditor
子类中还有其他字段/子编辑器,则必须实现ValueAwareEditor
并在setValue
和flush
方法中手动处理。