Search code examples
javagwtgxt

How to derive BaseModel or BeanModel from BeanModelMarker


I followed the instructions:

  1. create the bean. This bean is for an enum transferred from server to client using RestyGWT.

    public enum Mode{
      MODIFY,
      EDIT,
      DELETE,
      CREATE
    }
    
  2. Define the marker on the bean.

    @BEAN(Mode.class)
    static public class ModelMarker implements BeanModelMarker { }
    
  3. (Paraphrasing a statement from GXT blog) Now use your BeanModelMarker on any data component.

    So, I created a combo box.

    static private ComboBox<ModelMarker> propertyTypeComboBox =
      new ComboBox<ModelMarker>();
    

    Ooops, the ComboBox does not accept BeanModelMarker as a Model type. And it certainly is wrong, because it has not been GWT created yet.

And so what do I do with a GWT created instance?

static ModelMarker beanModel =
  GWT.create(ModelMarker.class);

I am unable to find any literature that tells me explicitly what to do with a BeanModelMarker after it is defined. How do I use it?

I would like to know what I need to do to define a Bean or Base Model, so that I could use the enum in my data-driven combobox.

Am I asking the question in the right way about BeanModelMarker? Is it relevant to my attempt at creating an enum-driven combo box?


Solution

  • Couple of thoughts:

    Here is a discussion on the GXT forums about wrapping enum types in ComboBoxes. http://www.sencha.com/forum/showthread.php?67317-Enum-based-ComboBox. Several approaches are used - in that thread, I went with the approach of making an EnumWrapper, and some static convenience methods to create List<EnumWrapper<MyEnum>> collections to give to the combo box ListStore. One of the main reasons that I went for that as opposed to a BeanModel approach was that I needed my enums to be i18n-capable, and didn't want that i18n logic in the enum itself.

    More pertinent to your question, the correct way to translate a java object to a BeanModel is to use the BeanModelFactory instance provided from BeanModelLookup.getFactory(Mode.class). ModelMarker is just an interface, and can't implement ModelData, so your ComboBox<ModelMarker> declaration doesn't really make sense. Remember that using this approach means your Mode enum needs to expose getters so the BeanModel generation code can work its reflection-ish magic (like in http://www.sencha.com/forum/showthread.php?67317-Enum-based-ComboBox&p=332996&viewfull=1#post332996).

    ComboBox<BeanModel> modeCombo = new ComboBox<BeanModel>();
    ListStore<BeanModel> store = new ListStore<BeanModel>();
    
    // this next line might need ModelMarker.class instead, its been a while
    BeanModelFactory modeFactory = BeanModelLookup.get().getFactory(Mode.class);
    
    
    // either add items one at a time
    Mode mode = Mode.EDIT;
    store.add(modeFactory.createModel(mode));
    
    // or add a collection
    store.add(modeFactory.createModel(Arrays.asList(Mode.values()));