javagwtgwt-editors

How to implement GWT editors for subclasses of a type?


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.


Solution

  • I've already been in this case, and I've implemented the following solution :

    This should show/hide your fields according to the subclass you are editing, and bind the properties to the good fields.