如何为类型的子类实现GWT编辑器?

假设我有一个像这样的对象层次结构:

帐户>网站>供应

账户是一个实际的公司,一个网站是他们拥有的建筑,而一个供应是ElecSupplyGasSupply 。 供应从未实例化,理论上可能是一个抽象的阶级。

我使用Objectify进行持久化,并有一个页面显示每个站点的供应列表,无论它们是ElecSupply还是GasSupply

现在我正在实现GWT编辑器框架 ,并遇到了这个多态实体的问题。 如何为这样的对象实现编辑器和子编辑器集?

 @Entity public class Supply implements Serializable { @Id protected Long id; @Embedded protected List billingPeriods = new ArrayList(); public Supply() { } // ... } 

子类:( ElecSupply有5个独特的字段,GasSupply只有一个)

 @Subclass public class ElecSupply extends Supply implements Serializable { private String profile; private String mtc; private String llf; private String area; private String core; public ElecSupply() { } } 

 @Subclass public class GasSupply extends Supply implements Serializable { private String mpr; public GasSupply() { } // ... } 

所以我想知道是否有人对这种结构有任何经验? 我曾尝试为ElecSupplyGasSupply制作单独的编辑器,然后在编辑页面中显示或隐藏它们。

我想要做的另一种方法是使用单个编辑器(对于Supply),然后根据我们正在编辑的对象类型加载不同的子编辑器。

任何灯棚都会感激不尽。

我已经遇到过这种情况,我已经实现了以下解决方案:

  • 首先创建一个名为AbstractSubTypeEditor的通用实用类,当您编辑一个子类对象时,它将激活特定的编辑器:

     import com.google.gwt.editor.client.CompositeEditor; import com.google.gwt.editor.client.Editor; import com.google.gwt.editor.client.EditorDelegate; import com.google.gwt.editor.client.LeafValueEditor; public abstract class AbstractSubTypeEditor> implements CompositeEditor, LeafValueEditor { private EditorChain chain; private T currentValue; private final E subEditor; /** * Construct an AbstractSubTypeEditor backed by the given sub-Editor. * * @param subEditor the sub-Editor that will be attached to the Editor * hierarchy */ public AbstractSubTypeEditor(E subEditor) { this.subEditor = subEditor; } /** * Returns the sub-Editor that the OptionalFieldEditor was constructed * with. * * @return an {@link Editor} of type E */ public E createEditorForTraversal() { return subEditor; } public void flush() { currentValue = chain.getValue(subEditor); } /** * Returns an empty string because there is only ever one sub-editor used. */ public String getPathElement(E subEditor) { return ""; } public T getValue() { return currentValue; } public void onPropertyChange(String... paths) { } public void setDelegate(EditorDelegate delegate) { } public void setEditorChain(EditorChain chain) { this.chain = chain; } public void setValue(T value, boolean instanceOf) { if (currentValue != null && value == null) { chain.detach(subEditor); } currentValue = value; if (value != null && instanceOf) { chain.attach((C)value, subEditor); } } } 
  • 现在,您可以创建一个Supply for Editor,包含两个子编辑器和两个AbstractSubTypeEditor(每个子类型一个):

     public class SupplyEditor extends Composite implements Editor { public class ElecSupplyEditor implements Editor { public final TextBox profile = new TextBox(); public final TextBox mtc = new TextBox(); public final TextBox llf = new TextBox(); public final TextBox area = new TextBox(); public final TextBox core = new TextBox(); } @Ignore final ElecSupplyEditor elecSupplyEditor = new ElecSupplyEditor(); @Path("") final AbstractSubTypeEditor elecSupplyEditorWrapper = new AbstractSubTypeEditor(elecSupplyEditor) { @Override public void setValue(final Supply value) { setValue(value, value instanceof ElecSupply); if (!(value instanceof ElecSupply)) { elecSupplyEditor.profile.setVisible(false); elecSupplyEditor.mtc.setVisible(false); elecSupplyEditor.llf.setVisible(false); elecSupplyEditor.area.setVisible(false); elecSupplyEditor.core.setVisible(false); } else { elecSupplyEditor.profile.setVisible(true); elecSupplyEditor.mtc.setVisible(true); elecSupplyEditor.llf.setVisible(true); elecSupplyEditor.area.setVisible(true); elecSupplyEditor.core.setVisible(true); } } }; public class GasSupplyEditor implements Editor { public final TextBox mpr = new TextBox(); } @Ignore final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor(); @Path("") final AbstractSubTypeEditor gasSupplyEditorWrapper = new AbstractSubTypeEditor(gasSupplyEditor) { @Override public void setValue(final Supply value) { setValue(value, value instanceof GasSupply); if (!(value instanceof GasSupply)) { gasSupplyEditor.mpr.setVisible(false); } else { gasSupplyEditor.mpr.setVisible(true); } } }; public SupplyEditor () { final VerticalPanel page = new VerticalPanel(); page.add(elecSupplyEditor.profile); page.add(elecSupplyEditor.mtc); page.add(elecSupplyEditor.llf); page.add(elecSupplyEditor.area); page.add(elecSupplyEditor.code); page.add(gasSupplyEditor.mpr); initWidget(page); } } 

这应该根据您正在编辑的子类显示/隐藏您的字段,并将属性绑定到好字段。

您可以让您的SupplyEditor实现ValueAwareEditor

这样,编辑器框架将传递在setValue(Supply supply)编辑的实际值; 在setValue(Supply supply)的实现中,您可以检查Supply的类型并选择显示/隐藏任何其他相关字段。