【问题标题】:GWT, Enum, RadioButton and Editors frameworkGWT、Enum、RadioButton 和 Editors 框架
【发布时间】:2011-06-26 11:25:00
【问题描述】:

问题是:我有一个 bean,而这个 bean 有一个枚举属性:

enum E {
    ONE, TWO, THREE;
}

class A implements Serializable {
    public E foo;
}

我想用GWT Editor framework 让用户编辑这个bean

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

我有一个错误:

[错误] [gwtmodule] - 找不到 代理类型中路径一的吸气剂 com.company.A

[错误] [gwtmodule] - 找不到 代理类型中路径二的吸气剂 com.company.A

[错误] [gwtmodule] - 找不到 代理类型中路径三的吸气剂 com.company.A

有没有办法在 GWT 2.2 中完成这项工作?

【问题讨论】:

    标签: java gwt gwt2


    【解决方案1】:
    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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      问题不在于枚举。编译器正在寻找对应于 uiFields 1、2 和 3 的类 bean 的 getter 方法。 RadioButton 在实现IsEditor&lt;LeafValueEditor&lt;java.lang.Boolean&gt;&gt; 接口时映射到布尔属性。

      这应该使您的示例代码工作,但它显然不是一个非常灵活的解决方案:

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

      要将一组单选按钮映射到单个枚举属性(及其对应的 getter/setter),您必须实现自己的编辑器来包装单选按钮组,并返回 E 类型的值。它需要实现像IsEditor&lt;LeafValueEditor&lt;E&gt;&gt; 这样的接口。

      有一个related discussion on the GWT group

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多