2012-07-12 64 views
2

我已经搜索过,并试图弄清楚我是否可以使用带有多态类型的编辑器框架。我发现这篇文章在Using GWT Editors with a complex usecase这接近我想要做的。 我相当新的编辑框架,所以任何帮助将不胜感激。带有多态类型的ListEditor

例如,下面是一些代码,

数据传输对象:

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

我已经更新上面的代码到现在包含ContactEditorWrapper类托马斯建议。

回答

0

该代码很有可能中断:不能保证由EditorSource返回的编辑器不会用于编辑列表中的其他值。

您应该创建一个包装编辑器,实现ValueAwareEditor,并将实际编辑器作为子编辑器使用@Path("");并在setValue方法中创建适当的ContactBaseEditor

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,并在setValueflush方法中手动处理。