2

我有一个枚举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;
    }
}

我使用以下命令在编辑器中显示它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()));
}

我必须在我的应用程序中编辑这种类型几次,所以我想为这个下拉列表创建一个编辑器,称为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()));
    }

}

但是,当我使用它时,虽然它可以使用选项呈现列表,但它不会从列表中选择实际值。我认为拥有 getValue() 和 setValue() 方法会起作用,但似乎不行。

有谁知道将其放入一个编辑器文件的方法?然后我不必重复渲染器的代码并在每个我想使用它的地方调用 setAcceptableValues() 。

4

2 回答 2

2

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.

于 2014-03-25T13:30:21.887 回答
1

您必须:

  • @Editor.Path("")在你的孩子身上ValueListBox

  • 使您的SupplierCodeEditor实施,与LeafValueEditor<SupplierCode>委派getValuesetValueValueListBox

  • 制作你的SupplierCodeEditor工具IsEditor<LeafValueEditor<SupplierCode>,从你自己ValueListBox的.asEditor()asEditor()

顺便说一句,你绝对不需要你的枚举值的getValueand setValue

于 2014-03-25T13:32:11.153 回答