2011-02-13 55 views
2

这里有一个问题:我有一个bean和这个bean有一个枚举属性:GWT,枚举,单选按钮和编辑器框架

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

我得到了一个错误:

[ERROR] [gwtmodule] - Could not find a getter for path one in proxy type com.company.A

[ERROR] [gwtmodule] - Could not find a getter for path two in proxy type com.company.A

[ERROR] [gwtmodule] - Could not find a getter for path three in proxy type com.company.A

有没有一种方法,使在GWT 2.2这项工作?

回答

8
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

感谢张贴此代码安东尼奥。 – Stevko 2011-03-15 16:56:57

1

问题不在于enum。编译器正在寻找与uiFields 1,2和3相对应的bean类getter方法。 RadioButtons在实现IsEditor<LeafValueEditor<java.lang.Boolean>>接口时映射到布尔属性。

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

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<LeafValueEditor<E>>这样的接口。

有一个related discussion on the GWT group