【问题标题】:GWT Editor Framework - Show ENUM using ValueListBox in own editorGWT Editor Framework - 在自己的编辑器中使用 ValueListBox 显示 ENUM
【发布时间】:2014-03-25 13:11:56
【问题描述】:

我有一个枚举 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()。

【问题讨论】:

    标签: java gwt enums gwt-editors


    【解决方案1】:

    使用LeafValueEditor&lt;SupplierCode&gt;:

    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);
        }
    }
    

    这样,您的小部件将很容易插入Editor 层次结构中。 而且您不需要 SupplierCode 枚举中的 get/set 方法。

    【讨论】:

    • 如果不需要自定义列表框的默认布局,可以直接initWidget(this.codes);跳过UiBinder。
    【解决方案2】:

    您必须:

    • 对您的孩子使用@Editor.Path("") ValueListBox

    • 让你的SupplierCodeEditor 实现LeafValueEditor&lt;SupplierCode&gt;,将getValuesetValue 委托给ValueListBox

    • 让你的SupplierCodeEditor实现IsEditor&lt;LeafValueEditor&lt;SupplierCode>,从你自己的asEditor()返回ValueListBoxasEditor()

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

    【讨论】:

    • 设置@Path("") 不起作用,但实现LeafValueEditor&lt;SupplierCode&gt; 就成功了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多