gwtdesign-patternsgwt-editors

Is there a better pattern to show an OptionalFieldEditor only if the value is non-null?


I have created a large scale editor composed of many, many subeditors. Some of them need to handle optional substructures, and I wrapped them in a OptionalFieldEditor.

Problem is now, that I want to show those sub-structures only, if there is such a structure, and if not, I want to show a create button.

So far, no problem: If I start without that structure, I will simply hide the editor panel, and make it visible, as soon as the user clicks the create button.

Problem arises, if the editor has the structure filled already from the beginning. The OptionalFieldEditor will not allow me to get notified or read the value to adopt the display to the initial state. So the editor stays either invisible despite of values or the editor is visible, but no values are set.

To work around this issue, I implemented a derived OptionalField Editor, that will notify a callback if it's setValue() method is called.

But I thought, this should be a classical use case for the OptionalFieldEditor, so why do I need to invent this? Maybe I am just to stupid to understand the right way, thus I ask here. Is that a valid pattern, or is there a facility in the editor framework to make this simple?

Here is the code:

public class MyOptionalFieldEditor
        extends OptionalFieldEditor<HwrDefinition, HwrDefinitionEditorView>
{
    public interface ValueHandler {
        void receivedValue(HwrDefinition value);
    }

    ValueHandler handler;

    protected MyOptionalFieldEditor( HwrDefinitionEditorView subEditor, 
                                     ValueHandler handler ) 
    {
        super( subEditor );
        this.handler = handler;
    }

    @Override
    public void setValue(HwrDefinition value) {
        super.setValue( value );
        if( handler != null ) {
            handler.receivedValue(value);
        }
    }
}

Thanks,

Thomas


Solution

  • We tried various things with the OptionalFieldEditor and ended up with something similar to what you have, although we encapsulated the UI part and managing the editor framework into one reusable class. On top of this we had different variations - with add/delete button or an implicit one where filling in the fields would add/remove the object under the covers.

    I know what you mean about "should it do more" but I think it fits nicely with the GWT philosophy of providing basic build blocks rather trying to venture into less certain territory. OptionalFieldEditor seems very useful on its own.

    Maybe you could share your code somewhere to save somebody else having to invent it?