javagwtenumsgwt-editors

GWT Editor Framework - Show ENUM using ValueListBox in own editor


I have an Enum SupplierCode:

public enum SupplierCode
{       
    BG("British Gas"), CNG("Contract Natural Gas"), COR("Corona Energy");

    private String value;

    SupplierCode(String value)
    {
        if(value != "")
        {
            this.value = value;
        }
    }

    // ... toString() and fromString() omitted for brevity

    // for editor framework (?)
    public String getValue()
    {
        return value;
    }
    public void setValue(String value)
    {
        this.value = value;
    }
}

I display it in my editors using a ValueListBox:

@UiField(provided = true)
ValueListBox<SupplierCode> supplierCode = new ValueListBox<SupplierCode>(new AbstractRenderer<SupplierCode>()
{
    @Override
    public String render(SupplierCode object)
    {
        return object == null ? "" : object.toString();
    }
});

// in the constructor
public ContractEditor()
{
    initWidget(uiBinder.createAndBindUi(this));
    supplierCode.setAcceptableValues(Arrays.asList(SupplierCode.values()));
}

I have to edit this type a few times in my app so I wanted to make an editor for just this dropdown, called SupplierCodeEditor:

public class SupplierCodeEditor extends Composite implements Editor<SupplierCode>
{
    private static SupplierCodeEditorUiBinder uiBinder = GWT.create(SupplierCodeEditorUiBinder.class);

    interface SupplierCodeEditorUiBinder extends UiBinder<Widget, SupplierCodeEditor>
    {
    }

    @UiField(provided = true)
    ValueListBox<SupplierCode> value = new ValueListBox<SupplierCode>(new AbstractRenderer<SupplierCode>()
    {
        @Override
        public String render(SupplierCode object)
        {
            return object == null ? "" : object.toString();
        }
    });

    public SupplierCodeEditor()
    {
        initWidget(uiBinder.createAndBindUi(this));
        value.setAcceptableValues(Arrays.asList(SupplierCode.values()));
    }

}

However, when I use it, although it renders the list ok with the options, it doesn't select the actual value from the list. I thought having the getValue() and setValue() methods would work but seemingly not.

Does anyone know of a way to put this in one editor file? Then I won't have to repeat the code for the renderer and call setAcceptableValues() every place I want to use it.


Solution

  • Use LeafValueEditor<SupplierCode>:

    public class SupplierEditor extends Composite implements LeafValueEditor<SupplierCode> {
        interface SupplierEditorUiBinder extends UiBinder<Widget, SupplierEditor> {
        }
    
        private static SupplierEditorUiBinder uiBinder = GWT.create(SupplierEditorUiBinder.class);
    
        @UiField(provided = true)
        ValueListBox<SupplierCode> codes;
    
        public SupplierEditor() {
            codes = new ValueListBox<>(new AbstractRenderer<SupplierCode>() {
                @Override
                public String render(SupplierCode object) {
                    return object == null ? "" : object.toString();
                }
            });
    
            initWidget(uiBinder.createAndBindUi(this));
    
            codes.setAcceptableValues(Arrays.asList(SupplierCode.values()));
        }
    
        @Override
        public SupplierCode getValue() {
            return codes.getValue();
        }
    
        @Override
        public void setValue(SupplierCode value) {
            codes.setValue(value);
        }
    }
    

    This way, your widget will be easily pluggable in a Editor hierarchy. And you don't need the get/set methods in your SupplierCode enum.