GWT 2.5.1:动态所需现场指示器

GWT 2.5.1:动态所需现场指示器,gwt,uibinder,Gwt,Uibinder,显示动态必填字段指示器的更好方法是什么(在我的例子中,如果字段为空,则在字段旁边显示“*”;如果用户键入了什么,则隐藏它;如果用户清除了输入字段,则再次显示它)? 该指示器在下面的代码中称为requiredFieldHighlight MyValueBoxEditorDecorator.java public class MyValueBoxEditorDecorator<T> extends Composite implements HasEditorErrors<T>

显示动态必填字段指示器的更好方法是什么(在我的例子中,如果字段为空,则在字段旁边显示“*”;如果用户键入了什么,则隐藏它;如果用户清除了输入字段,则再次显示它)? 该指示器在下面的代码中称为requiredFieldHighlight

MyValueBoxEditorDecorator.java

public class MyValueBoxEditorDecorator<T> extends Composite implements HasEditorErrors<T>,
IsEditor<ValueBoxEditor<T>>
{
    interface Binder extends UiBinder<Widget, MyValueBoxEditorDecorator<?>>
    {
        Binder BINDER = GWT.create(Binder.class);
    }

    @UiField
    DivElement label;

    @UiField
    SimplePanel contents;

    @UiField
    DivElement requiredFieldHighlight;

    @UiField
    DivElement errorLabel;

    private ValueBoxEditor<T> editor;

    private ValueBoxBase<T> valueBox;

    /**
     * Constructs a ValueBoxEditorDecorator.
     */
    @UiConstructor
    public MyValueBoxEditorDecorator()
    {
        initWidget(Binder.BINDER.createAndBindUi(this));
    }

    public MyValueBoxEditorDecorator(int dummy)
    {
        this();

        valueBox = (ValueBoxBase<T>) new TextBoxTest(requiredFieldHighlight);
        this.editor = valueBox.asEditor();


        valueBox.addValueChangeHandler(new ValueChangeHandler<T>()
                {
            @Override
            public void onValueChange(ValueChangeEvent<T> event)
            {
                MyValueBoxEditorDecorator.this.onValueChange();
            }
                });

        contents.add(valueBox);

        MyValueBoxEditorDecorator.this.onValueChange();
    }

    private void onValueChange()
    {
        T value = editor.getValue();

        if (value == null)
        {
            requiredFieldHighlight.getStyle().setDisplay(Style.Display.INLINE_BLOCK);
            return;
        }
        else
        {
            requiredFieldHighlight.getStyle().setDisplay(Style.Display.NONE);
        }
    }

    public ValueBoxEditor<T> asEditor()
    {
        return editor;
    }


    public void setEditor(ValueBoxEditor<T> editor)
    {
        this.editor = editor;
    }

    @UiChild(limit = 1, tagname = "valuebox")
    public void setValueBox(ValueBoxBase<T> widget)
    {
        contents.add(widget);
        setEditor(widget.asEditor());
    }

    @Override
    public void showErrors(List<EditorError> errors)
    {
        // this manages the content of my errorLabel UiField
    }
}
我对这种方法有几个问题。首先,它创建了对我的另一个特定类(TextBoxTest)的依赖关系,其次,它实际上无法正常工作,因为当我使用GUI清除文本字段的内容时,GWT不会自动调用setText()!换句话说,为了使指示器正常工作,我需要在TextBoxTest类中重载setText+setValue,并且必须将ValueChangeHandler添加到MyValueBoxEditorDecorator对象中。为什么?(处理文本更改的正确事件/地点在哪里?)

20150629更新:当我的屏幕初始化时,实际上调用了setValue()。我的valueChangeHandler没有被触发,'但是,这是由于GWT内部(我认为是由于提供的setValue()没有使用fireEvents标志调用fireEvents重载并使用错误的fireEvent标志)

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
    xmlns:g='urn:import:com.google.gwt.user.client.ui'>

    <ui:style src="common.css" />

    <g:HTMLPanel width="100%">
        <div ui:field="label"  class="{style.label}"/>
        <g:SimplePanel ui:field="contents" stylePrimaryName="{style.contents}" />
        <div class="{style.errorLabel}" ui:field="errorLabel" />
        <div class="{style.errorLabel} {style.requiredFieldHighlight}" ui:field="requiredFieldHighlight">*</div>
    </g:HTMLPanel>
</ui:UiBinder>
public class TextBoxTest extends TextBox
{
    @Override
    public void setText(String text)
    {
        super.setText(text);
        updateRequiredFieldHighlight(text);
    }

    private final DivElement requiredFieldHighlight;

    public TextBoxTest(DivElement requiredFieldHighlight)
    {
        super();
        this.requiredFieldHighlight = requiredFieldHighlight;
    }

    private void updateRequiredFieldHighlight(String withValue)
    {
        if (withValue != null && !withValue.isEmpty())
        {
            requiredFieldHighlight.getStyle().setDisplay(Style.Display.NONE);
        }
        else
        {
            requiredFieldHighlight.getStyle().setDisplay(Style.Display.INLINE_BLOCK);
        }
    }


    @Override
    public void setValue(String value, boolean fireEvents)
    {
        super.setValue(value, fireEvents);
        updateRequiredFieldHighlight(value);
    }
}