Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've already been in this case, and I've implemented the following solution :</p> <ul> <li><p>First create an generic utilitary class named AbstractSubTypeEditor which will activate a specific editor when you edit one of your subclass object :</p> <pre><code>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&lt;T, C extends T, E extends Editor&lt;C&gt;&gt; implements CompositeEditor&lt;T, C, E&gt;, LeafValueEditor&lt;T&gt; { private EditorChain&lt;C, E&gt; 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&lt;T&gt; delegate) { } public void setEditorChain(EditorChain&lt;C, E&gt; chain) { this.chain = chain; } public void setValue(T value, boolean instanceOf) { if (currentValue != null &amp;&amp; value == null) { chain.detach(subEditor); } currentValue = value; if (value != null &amp;&amp; instanceOf) { chain.attach((C)value, subEditor); } } } </code></pre></li> <li><p>Now you can create an Editor for Supply, containing two sub-editors and two AbstractSubTypeEditor (one for each of your subtypes) :</p> <pre><code>public class SupplyEditor extends Composite implements Editor&lt;Supply&gt; { public class ElecSupplyEditor implements Editor&lt;ElecSupply&gt; { 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&lt;Supply, ElecSupply, ElecSupplyEditor&gt; elecSupplyEditorWrapper = new AbstractSubTypeEditor&lt;Supply, ElecSupply, SupplyEditor.ElecSupplyEditor&gt;(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&lt;GasSupply&gt; { public final TextBox mpr = new TextBox(); } @Ignore final GasSupplyEditor gasSupplyEditor = new GasSupplyEditor(); @Path("") final AbstractSubTypeEditor&lt;Supply, GasSupply, GasSupplyEditor&gt; gasSupplyEditorWrapper = new AbstractSubTypeEditor&lt;Supply, GasSupply, SupplyEditor.GasSupplyEditor&gt;(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); } } </code></pre></li> </ul> <p>This should show/hide your fields according to the subclass you are editing, and bind the properties to the good fields.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload