2011-02-13 83 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