1 Reply Latest reply on Feb 13, 2019 4:33 AM by Pierre Gilquin

    Errai : Generic DataBinding for enums

    Pierre Gilquin Newbie

      Hi,

       

      I give a try to errai. For now, I am interested only in client side (UI/DataBinding/Navigation).

       

      I had a generic component using Editor for enums rendering in radio button done with gwtbootstap3. I would like to adapt it in order to use it in the Errai DataBinding

      I started with a simplify example with a particular enum : "UnansweredYesNoUnknown"

      So I created a component based on ButtonGroup (gwtbootstrap3) implementing TakesValue<UnansweredYesNoUnknown>

      I added the corresponding @DataField in my page :

      @Inject @DataField @Bound public UnansweredYesNoUnknownEditor isNiceGuy;

       

      Almost everything goes right (the radiobutton group is rendered, the data is set correctly from the data bean, the model is updated in the UnansweredYesNoUnknownEditor).

      The only problem is that changes in GUI not come back to the bean when calling binder.getModel(), the method getValue() is not excuted.

       

      Did I miss something ?

       

      Thanks in avance for any help

      Pierre

       

      The UnansweredYesNoUnknownEditor code :

      public class UnansweredYesNoUnknownEditor extends ButtonGroup implements ClickHandler,TakesValue<UnansweredYesNoUnknown>{

       

          private Map<RadioButton, UnansweredYesNoUnknown> map;

         

          @Model

          private UnansweredYesNoUnknown model;

       

       

      // add RadioButton in ButtonGroup

      //  add click handler on each RadioButton in order to change the model

      public UnansweredYesNoUnknownEditor() {

          String name = "rb";

              map = new HashMap<RadioButton, UnansweredYesNoUnknown>();

              for (UnansweredYesNoUnknown e: UnansweredYesNoUnknown.values()){

                  RadioButton rb = new RadioButton(name.replaceAll(" ", "_"), e.getDisplayValue());

                  rb.setSize(ButtonSize.EXTRA_SMALL);

                  rb.setFormValue(e.getDisplayValue());

                  map.put(rb, e);

                  this.add(rb);

                  rb.addClickHandler(this);

             }

      }

       

       

          @Override

          public void setValue(UnansweredYesNoUnknown value) {

              if (value==null)

                  return;

              RadioButton rb = (RadioButton) super.getWidget(value.ordinal());

              rb.setValue(true);

              model = value;

       

       

          }

       

          @Override

          public UnansweredYesNoUnknown getValue() {

                return model;

          }

       

       

      // on click

      @Override

      public void onClick(ClickEvent event) {

      for (Entry<RadioButton, UnansweredYesNoUnknown> e: map.entrySet())

      {

           if (e.getKey().getValue())

           {

           model = e.getValue();

           }

      }

      }