简体   繁体   English

GWT编辑器框架-在自己的编辑器中使用ValueListBox显示ENUM

[英]GWT Editor Framework - Show ENUM using ValueListBox in own editor

I have an Enum SupplierCode : 我有一个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 : 我使用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 : 我必须在应用程序中多次编辑此类型,所以我想为此下拉列表创建一个名为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. 我以为拥有getValue()和setValue()方法会起作用,但似乎无效。

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. 然后,我将不必重复渲染器的代码,并在我想使用它的每个位置调用setAcceptableValues()。

Use LeafValueEditor<SupplierCode> : 使用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. 这样,您的窗口小部件将很容易插入到Editor层次结构中。 And you don't need the get/set methods in your SupplierCode enum. 而且您不需要在SupplierCode枚举中使用get / set方法。

You have to either: 您必须:

  • use @Editor.Path("") on your child ValueListBox 在孩子的ValueListBox上使用@Editor.Path("")

  • make your SupplierCodeEditor implement LeafValueEditor<SupplierCode> , with delegating getValue and setValue to the ValueListBox 使您的SupplierCodeEditor实现LeafValueEditor<SupplierCode> ,并将getValuesetValue委托给ValueListBox

  • make your SupplierCodeEditor implement IsEditor<LeafValueEditor<SupplierCode >, returning the ValueListBox 's asEditor() from your own asEditor() . 让你的SupplierCodeEditor实施IsEditor<LeafValueEditor<SupplierCode >,返回ValueListBoxasEditor()从自己的asEditor()

BTW, you absolutely don't need the getValue and setValue on your enum values. 顺便说一句,您绝对不需要枚举值上getValuesetValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM