简体   繁体   English

GWT,Enum,RadioButton和Editors框架

[英]GWT, Enum, RadioButton and Editors framework

Here is the problem: I have a bean and this bean have a enum property: 这是问题:我有一个bean,这个bean有一个枚举属性:

enum E {
    ONE, TWO, THREE;
}

class A implements Serializable {
    public E foo;
}

I'd like to use GWT Editor framework to let user edit this bean 我想使用GWT Editor框架让用户编辑这个bean

public class P extends FlowPanel implements Editor<A> {
    // ... UiBinder code here ...
    @UiField RadioButton one, two, three;
    // ...
}

I've got an error: 我有一个错误:

[ERROR] [gwtmodule] - Could not find a getter for path one in proxy type com.company.A [错误] [gwtmodule] - 在代理类型com.company.A中找不到路径1的getter

[ERROR] [gwtmodule] - Could not find a getter for path two in proxy type com.company.A [错误] [gwtmodule] - 无法在代理类型com.company.A中找到路径二的getter

[ERROR] [gwtmodule] - Could not find a getter for path three in proxy type com.company.A [错误] [gwtmodule] - 在代理类型com.company.A中找不到路径3的getter

Is there a way to make this work in GWT 2.2? 有没有办法在GWT 2.2中完成这项工作?

public class EnumEditor extends FlowPanel implements LeafValueEditor<E> {

    private Map<RadioButton, E> map;

    @UiConstructor
    public EnumEditor(String groupName) {
        map = new HashMap<RadioButton, E>();
        for (E e: E.class.getEnumConstants()){
            RadioButton rb = new RadioButton(groupName, e.name());
            map.put(rb, e);
            super.add(rb);
        }
    }

    @Override
    public void setValue(E value) {
        if (value==null)
            return;
        RadioButton rb = (RadioButton) super.getWidget(value.ordinal());
        rb.setValue(true);
    }

    @Override
    public E getValue() {
        for (Entry<RadioButton, E> e: map.entrySet()) {
            if (e.getKey().getValue())
                return e.getValue();
        }
        return null;
    }
}

The problem isn't with the enum. 问题不在于枚举。 The compiler is looking for the bean-like getter methods that correspond to the uiFields one, two and three. 编译器正在寻找与uiFields 1,2和3对应的类似bean的getter方法。 RadioButtons map to boolean properties as they implement the IsEditor<LeafValueEditor<java.lang.Boolean>> interface. RadioButtons在实现IsEditor<LeafValueEditor<java.lang.Boolean>>接口时映射到布尔属性。

This should make your example code work, but it's obviously not a very flexible solution: 这应该使您的示例代码工作,但它显然不是一个非常灵活的解决方案:

class A implements Serializable {
    public E foo;
    public Boolean getOne() {return foo==E.ONE;}
    public Boolean getTwo() {return foo==E.TWO;}
    public Boolean getThree() {return foo==E.THREE;}
}

To map a group of radiobuttons to a single enum property (and its corresponding getter/setter) you'd have to implement your own editor wrapping the radiobutton group, and returning a value of type E. It'd need to implement an interface like IsEditor<LeafValueEditor<E>> . 要将一组radiobuttons映射到单个枚举属性(及其相应的getter / setter),您必须实现包含radiobutton组的自己的编辑器,并返回类型E的值。它需要实现类似的接口IsEditor<LeafValueEditor<E>>

There's a related discussion on the GWT group 有关GWT小组相关讨论

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

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