2

我四处搜索并试图弄清楚是否可以将编辑器框架与多态类型一起使用。我在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;
}
}

联系EditorWrapper:

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 类。

4

1 回答 1

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但是请注意,无论使用哪种实际实现,都只会编辑来自的字段/子编辑器。如果某些子类中有其他字段/子编辑器,则必须在and方法中手动ContactBaseEditor实现和处理。ValueAwareEditorsetValueflush

于 2012-07-12T11:03:32.860 回答