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

slu*_*rew 2 java gwt gwt-editors

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

帐户>网站>供应

帐户是一个实际的公司,一个网站是他们拥有的建筑物,一个供应是一个ElecSupply或一个GasSupply.供应从未实例化,理论上可能是一个抽象的阶级.

我正在使用Objectify进行持久化,并且有一个页面显示每个站点的供应列表,无论它们是否为ElecSupplyGasSupply.

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

@Entity
public class Supply implements Serializable
{
    @Id
    protected Long id;
    @Embedded
    protected List<BillingPeriod> billingPeriods = new ArrayList<BillingPeriod>();

    public Supply()
    {

    }
// ...
}
Run Code Online (Sandbox Code Playgroud)

子类:( 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()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)
@Subclass
public class GasSupply extends Supply implements Serializable
{
    private String mpr;

    public GasSupply()
    {

    }
// ...
}
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否有人对这种结构有任何经验?我曾试图为ElecSupplyand 创建单独的编辑器GasSupply,然后在编辑页面中显示或隐藏它们.

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

任何灯光都会被感激地收到.

Flo*_*yle 7

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

  • 首先创建一个名为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<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);
                    }
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 现在,您可以创建一个Supply for Editor,包含两个子编辑器和两个AbstractSubTypeEditor(每个子类型一个):

    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);
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)

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