简体   繁体   中英

GWT, Enum, RadioButton and Editors framework

Here is the problem: I have a bean and this bean have a enum property:

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

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

[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

Is there a way to make this work in 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. RadioButtons map to boolean properties as they implement the IsEditor<LeafValueEditor<java.lang.Boolean>> interface.

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>> .

There's a related discussion on the GWT group

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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