Search code examples
javagwtgxt

How to implement the ModelData interface in ext gwt?


I am trying to use an EditorGrid in a project that I am working on. I am using EditorGrid grid = new EditorGrid (...)

ClassGrade is an object that contains the name, grade (as a Grade object), and credit hours for a class.

However, ClassGrade must implement ModelData. When I tried to implement the interface, there are a few methods that I am not sure how to correctly implement.

@Override
public <X> X get(String property) {
    if (property.equals("name"))
        return this.getClassName();
    if (property.equals("hours"))
        return this.getHours();
    if (property.equals("grade"))
        return this.getGrade();
    return null;
}

@Override
public Map<String, Object> getProperties() {
    Map<String, Object> propMap = new HashMap<String, Object>();
    propMap.put("grade", this.getGrade());
    propMap.put("hours", this.getHours());
    propMap.put("name", this.getClassName());
    return propMap;
}

@Override
public Collection<String> getPropertyNames() {
    ArrayList<String> props = new ArrayList<String>();
    props.add("grade");
    props.add("hours");
    props.add("name");
    return props;
}

@Override
public <X> X remove(String property) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public <X> X set(String property, X value) {
    // TODO Auto-generated method stub
    return null;
}

I dont know what to do for the get, remove, and set methods, because I do not know what the X means. I tried using

   @Override
public <X> X get(String property) {
    if (property.equals("name"))
        return this.getClassName();
    if (property.equals("hours"))
        return this.getHours();
    if (property.equals("grade"))
        return this.getGrade();
    return null;
}

but that didn't work because the return was not of type X. What am I doing wrong and how do I get this to work?


Solution

  • I also use grids etc. and use BeanModel class for stores which wants modelData. I implement as below.

    public class dto implements IsSerializable, BeanModelTag  {
    
    ....//attributes and setter getters.
    
    }
    

    and

    convert this object to model as below;

    public static <E> BeanModel toModel(E e) {
        BeanModelFactory factory = BeanModelLookup.get().getFactory(e.getClass());
        return factory.createModel(e);
    }
    

    I hope it is useful for you.