如何为类型的子类实现 GWT 编辑器? [英] How to implement GWT editors for subclasses of a type?
问题描述
假设我有一个这样的对象层次结构:
帐户 > 站点 > 供应
帐户是实际的公司,站点是他们拥有的建筑物,供应是 ElecSupply
或 GasSupply
.Supply 永远不会被实例化,理论上可以是一个抽象类.
我正在使用 Objectify 进行持久化,并且有一个页面显示每个站点的耗材列表,无论它们是 ElecSupply
还是 GasSupply
.>
现在我正在实施 GWT 编辑器框架并且已经出现针对这个多态实体的问题.如何为这样的对象实现一个编辑器和一组子编辑器?
@Entity公共类 Supply 实现可序列化{@ID受保护的长 ID;@嵌入式受保护的列表<BillingPeriod>billingPeriods = new ArrayList();公共供应(){}//...}
子类:(ElecSupply 有 5 个唯一字段,GasSupply 只有一个)
@Subclass公共类 ElecSupply 扩展 Supply 实现可序列化{私人字符串配置文件;私人字符串 mtc;私人字符串 llf;私人字符串区域;私有字符串核心;公共电力供应(){}}
<小时>
@Subclass公共类 GasSupply 扩展 Supply 实现可序列化{私人字符串 mpr;公共气体供应(){}//...}
所以我想知道是否有人对这种结构有任何经验?我尝试为 ElecSupply
和 GasSupply
制作单独的编辑器,然后将它们显示或隐藏为编辑页面的一部分.
我考虑的另一种方法是使用一个编辑器(用于 Supply),然后根据我们正在编辑的对象类型加载不同的子编辑器.
我们将不胜感激.
我已经遇到过这种情况,我已经实施了以下解决方案:
首先创建一个名为 AbstractSubTypeEditor 的通用实用类,当您编辑其中一个子类对象时,它将激活一个特定的编辑器:
import com.google.gwt.editor.client.CompositeEditor;导入 com.google.gwt.editor.client.Editor;导入 com.google.gwt.editor.client.EditorDelegate;导入 com.google.gwt.editor.client.LeafValueEditor;公共抽象类 AbstractSubTypeEditor
>实现 CompositeEditor , LeafValueEditor ;{private EditorChain<C, E>链;私人 T currentValue;私人最终 E 子编辑器;/*** 构造一个由给定子编辑器支持的 AbstractSubTypeEditor.** @param subEditor 将附加到编辑器的子编辑器* 等级制度*/公共 AbstractSubTypeEditor(E subEditor) {this.subEditor = subEditor;}/*** 返回构造 OptionalFieldEditor 的子编辑器* 和.** @return 类型 E 的 {@link Editor}*/公共 E createEditorForTraversal() {返回子编辑器;}公共无效冲洗(){currentValue = chain.getValue(subEditor);}/*** 返回一个空字符串,因为只使用了一个子编辑器.*/公共字符串 getPathElement(E subEditor) {返回 "";}公共 T getValue() {返回当前值;}公共无效onPropertyChange(字符串...路径){}公共无效 setDelegate(EditorDelegate 委托){}public void setEditorChain(EditorChain<C, E> chain) {this.chain = 链;}public void setValue(T value, boolean instanceOf) {if (currentValue != null && value == null) {chain.detach(subEditor);}当前值 = 值;如果(值!= null && instanceOf){chain.attach((C)value, subEditor);}}} 现在您可以为 Supply 创建一个编辑器,其中包含两个子编辑器和两个 AbstractSubTypeEditor(每个子类型一个):
public class SupplyEditor extends Composite implements Editor
{公共类 ElecSupplyEditor 实现 Editor {public final TextBox profile = new TextBox();public final TextBox mtc = new TextBox();公共最终文本框 llf = 新文本框();public final TextBox area = new TextBox();public final TextBox core = new TextBox();}@忽略最终的 ElecSupplyEditor elecSupplyEditor = new ElecSupplyEditor();@小路("")final AbstractSubTypeEditor elecSupplyEditorWrapper = new AbstractSubTypeEditor (elecSupplyEditor) {@覆盖public void setValue(最终供应值){setValue(value, value instanceof ElecSupply);如果(!(ElecSupply 的值实例)){elecSupplyEditor.profile.setVisible(false);elecSupplyEditor.mtc.setVisible(false);elecSupplyEditor.llf.setVisible(false);elecSupplyEditor.area.setVisible(false);elecSupplyEditor.core.setVisible(false);} 别的 {elecSupplyEditor.profile.setVisible(true);elecSupplyEditor.mtc.setVisible(true);elecSupplyEditor.llf.setVisible(true);elecSupplyEditor.area.setVisible(true);elecSupplyEditor.core.setVisible(true);}}};公共类 GasSupplyEditor 实现 Editor {public final TextBox mpr = new TextBox();}@忽略最终的 GasSupplyEditor gasSupplyEditor = new GasSupplyEditor();@小路("")final AbstractSubTypeEditor gasSupplyEditorWrapper = new AbstractSubTypeEditor (gasSupplyEditor) {@覆盖public void setValue(最终供应值){setValue(value, value instanceof GasSupply);if (!(value instanceof GasSupply)) {gasSupplyEditor.mpr.setVisible(false);} 别的 {gasSupplyEditor.mpr.setVisible(true);}}};公共供应编辑器(){最终的 VerticalPanel 页面 = 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(页面);}}
这应该根据您正在编辑的子类显示/隐藏您的字段,并将属性绑定到好的字段.
Let's say I have an object hierarchy like this:
Account > Site > Supply
An Account is an actual company, a Site is a building they have, and a Supply is either an ElecSupply
or GasSupply
. Supply is never instantiated and could be an abstract class in theory.
I am using Objectify for persistence, and have a page that displays the list of Supplies for each Site, regardless of whether they are an ElecSupply
or GasSupply
.
Now I am implementing the GWT Editor Framework and have come up against a problem with this polymorphic entity. How do I implement an Editor and set of sub-editors for an object like this?
@Entity
public class Supply implements Serializable
{
@Id
protected Long id;
@Embedded
protected List<BillingPeriod> billingPeriods = new ArrayList<BillingPeriod>();
public Supply()
{
}
// ...
}
The subclasses: (ElecSupply has 5 unique fields and GasSupply has just one)
@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()
{
}
// ...
}
So I would like to know if anyone has any experience with this kind of structure? I have tried to make separate editors for ElecSupply
and GasSupply
, and then show or hide them as part of the edit page.
The other way I was thinking about doing it would be to have a single editor (for Supply), and then load different sub-editors depending on which type of object we are editing.
Any light shed will be gratefully received.
I've already been in this case, and I've implemented the following solution :
First create an generic utilitary class named AbstractSubTypeEditor which will activate a specific editor when you edit one of your subclass object :
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<T, C extends T, E extends Editor<C>> implements CompositeEditor<T, C, E>, LeafValueEditor<T> { private EditorChain<C, E> 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<T> delegate) { } public void setEditorChain(EditorChain<C, E> 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); } } }
Now you can create an Editor for Supply, containing two sub-editors and two AbstractSubTypeEditor (one for each of your subtypes) :
public class SupplyEditor extends Composite implements Editor<Supply> { public class ElecSupplyEditor implements Editor<ElecSupply> { 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<Supply, ElecSupply, ElecSupplyEditor> elecSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, ElecSupply, SupplyEditor.ElecSupplyEditor>(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<GasSupply> { public final TextBox mpr = new TextBox(); } @Ignore final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor(); @Path("") final AbstractSubTypeEditor<Supply, GasSupply, GasSupplyEditor> gasSupplyEditorWrapper = new AbstractSubTypeEditor<Supply, GasSupply, SupplyEditor.GasSupplyEditor>(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); } }
This should show/hide your fields according to the subclass you are editing, and bind the properties to the good fields.
这篇关于如何为类型的子类实现 GWT 编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!