具有多态类型的ListEditor [英] ListEditor with polymorphic types

查看:99
本文介绍了具有多态类型的ListEditor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了四周,并试图弄清楚我是否可以使用带有多态类型的编辑器框架。我在使用GWT编辑器和一个复杂的用例找到了这篇文章,这与我正在尝试做的很接近。
我对编辑器框架相当陌生,所以任何帮助都将非常感激。



例如,下面是一些代码,



数据传输对象:

  public class Employee {
public List<联系与GT;联系人
}
公共类联系人{
public class ContactEmail extends Contact {}
public class ContactAddress extends Contact {}
public class ContactPhoneNumber extends {}

编辑:

  public interface ContactBaseEditor< T extends Contact>扩展Editor< T> {} 
public class AddressEditor extends Composite编辑器< ContactAddress> ;, ContactBaseEditor< ContactAddress> {}
public class EmailEditor扩展Composite implements Editor< ContactEmail>,ContactBaseEditor< ContactEmail> {)
public class PhoneNumberEditor扩展了Composite implements Editor< ContactPhoneNumber>,ContactBaseEditor< ContactPhoneNumber> {}

ContactEditor类:

  public class ContactEditor扩展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(){
返回编辑器;


$ / code $ / pre
$ b $ p ContactEditorWrapper:

  class ContactEditorWrapper扩展了Composite implements ContactBaseEditor< Contact> ;, ValueAwareEditor< Contact> {

私人SimplePanel面板=新SimplePanel();

@Path()ContactBaseEditor<联系人> 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;



主编辑器类: p>

  public class AddEmployeeEditor扩展了Composite implements Editor< Employee> {


@UiField
ContactEditor联系人;

接口Driver扩展了SimpleBeanEditorDriver< Employee,AddEmployeeEditor> {
}
public AddEmployeeEditor(final Binder binder){
driver = GWT.create(Driver.class);
driver.initialize(this);

列表<联系人> 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);






$ b

任何人都可以告诉我这是否可行,是我进行正确的方向或?

在此先感谢,
Mac



我已更新上面的代码现在包含Thomas建议的ContactEditorWrapper类。

解决方案

该代码有很好的破解机会:由 EditorSource 返回的编辑器将不会用于编辑列表中的其他值。



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

  class ContactEditor extends Composite implements ValueAwareEditor< Contact> {

私人SimplePanel面板=新SimplePanel();

@Path()ContactBaseEditor realEditor;

public ContactEditor(){
initWidget(panel);

$ b @Override
public void setValue(Contact value){
if(contact instanceAddress ContactAddress){
realEditor = new AddressEditor();
}
else if(contact instanceof ContactEmail){
realEditor = new EmailEditor();
}
else if(contact instance of ContactPhoneNumber){
realEditor = new PhoneNumberEditor();
}
else {
realEditor = null;
}
panel.setWidget(realEditor);
}

请注意,只有来自将编辑ContactBaseEditor ,无论使用哪个实际实现。如果在 ContactBaseEditor 子类中有其他字段/子编辑器,则必须实现 ValueAwareEditor 并处理直接在 setValue flush 方法中。


I have searched around and been trying to figure out if I can use the editor framework with polymorphic types. I found this post at Using GWT Editors with a complex usecase which is close to what I am trying to do. I am fairly new to the editor framework, so any help would be much appreciated.

For example, here is some of the code,

Data Transfer Objects:

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 {}

Editors:

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 class:

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;
      }
   }
 }

Main Editor class:

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);
   }
}

Can anyone tell me if this will work, am I going in the right direction or ?

Thanks in advance, Mac

I have updated the code above to now contain the ContactEditorWrapper class that Thomas advised.

解决方案

That code has good chances to break: there's no guarantee that an editor returned by the EditorSource won't be used to edit another value in the list.

You should create a wrapper editor implementing ValueAwareEditor and with the actual editor as a child editor with @Path(""); and create the appropriate ContactBaseEditor in the setValue method.

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);
   }

Note however that only the field/sub-editors from ContactBaseEditor will be edited, whichever the actual implementation being used. If there are additional fields/sub-editors in some ContactBaseEditor subclass, you'd have to implement ValueAwareEditor and handle things by-hand in the setValue and flush methods.

这篇关于具有多态类型的ListEditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆