GWT编辑和获取/设定值

时间:2011-08-08 14:00:25

标签: java gwt editor

我有以下编辑课,我很好奇它有什么问题。运行时,它会正确设置所选的右侧单选按钮。但是,在刷新顶级编辑器时,从不调用getValue,并且我的对象的属性永远不会更新。这是代码(提示 - 修改后的ValueListBox):

    public class ValueRadioList<T> extends FlowPanel implements
        HasConstrainedValue<T>, LeafValueEditor<T>, ValueChangeHandler<Boolean> {

    private final List<T> values = new ArrayList<T>();
    private final Map<Object, Integer> valueKeyToIndex =
            new HashMap<Object, Integer>();

    private final String name;
    private final Renderer<T> renderer;
    private final ProvidesKey<T> keyProvider;

    private T value;

    public ValueRadioList(Renderer<T> renderer) {
        this(renderer, new SimpleKeyProvider<T>());
    }

    public ValueRadioList(Renderer<T> renderer, ProvidesKey<T> keyProvider) {
        super();
        this.name = DOM.createUniqueId();
        this.keyProvider = keyProvider;
        this.renderer = renderer;
    }

    private void addValue(T value) {
        Object key = keyProvider.getKey(value);
        if (valueKeyToIndex.containsKey(key)) {
            throw new IllegalArgumentException("Duplicate value: " + value);
        }

        valueKeyToIndex.put(key, values.size());
        values.add(value);
        RadioButton radio = new RadioButton(name, renderer.render(value));
        radio.addValueChangeHandler(this);
        add(radio);
        assert values.size() == getWidgetCount();
    }

    @Override public HandlerRegistration addValueChangeHandler(
            ValueChangeHandler<T> handler) {
        return addHandler(handler, ValueChangeEvent.getType());
    }

    @Override public T getValue() {
        return value;
    }

    @Override public void onValueChange(ValueChangeEvent<Boolean> event) {
        int selectedIndex = -1;
        for (int i = 0, l = getWidgetCount(); i < l; i++) {
            if (((RadioButton) getWidget(i)).getValue()) {
                selectedIndex = i;
                break;
            }
        }

        if (selectedIndex < 0) {
            return; // Not sure why this happens during addValue
        }
        T newValue = values.get(selectedIndex);
        setValue(newValue, true);
    }

    @Override public void setAcceptableValues(Collection<T> newValues) {
        values.clear();
        valueKeyToIndex.clear();
        clear();

        for (T nextNewValue : newValues) {
            addValue(nextNewValue);
        }

        updateRadioList();
    }

    @Override public void setValue(T value) {
        setValue(value, false);
    }

    @Override public void setValue(T value, boolean fireEvents) {
        if (value == this.value
                || (this.value != null && this.value.equals(value))) {
            return;
        }

        T before = this.value;
        this.value = value;
        updateRadioList();

        if (fireEvents) {
            ValueChangeEvent.fireIfNotEqual(this, before, value);
        }
    }

    private void updateRadioList() {
        Object key = keyProvider.getKey(value);
        Integer index = valueKeyToIndex.get(key);
        if (index == null) {
            addValue(value);
        }

        index = valueKeyToIndex.get(key);
        ((RadioButton) getWidget(index)).setValue(true);
    }
}

1 个答案:

答案 0 :(得分:1)

解决了,我的POJO错过了那个领域的制定者。