Question

I am trying to create a GWT Editor for a bean Object, which has two fields of another Object type, therefore I want sub-editors for those fields. The problem is that I am quite new to the editors framework, and I don't really get the idea of all those Editor interfaces out there. Please advise of the best solution. My ideas were the following: using drivers of sub-editors to pass data to main editor driver; or use some sort of CompositeEditor (but I didn't know what to do with overridden methods...). Here's a sample code:

public class A {

    String s1, s2;
    Integer i;
    Double d;
    B ba, bb;

}

public class B {

    BigDecimal a, b;
}

//sub-editor
public class BEditor extends Composite implements Editor<B> {

    interface BDriver extends BeanEditorDriver<B, BEditor> {
    }

    //implements HasEditorErrors interface
    ValueBoxEditorDecorator<BigDecimal> a, b;
}

//main editor
public class AEditor extends Composite implements Editor<A> {

    interface ADriver extends BeanEditorDriver<A, AEditor> {
    }

    private ADriver driver = GWT.create(ADriver.class);

    ValueBoxEditorDecorator<String> s1, s2;
    ValueBoxEditorDecorator<Integer> i;
    ValueBoxEditorDecorator<Double> d;
    BEditor ba, bb;

    public AEditor() {
        driver.initialize(this);
        driver.edit(new A());
    }

    //called when the editor form is submitted
    public void onSubmit() {

        driver.clearErrors();
        A a = driver.flush();
        // A has both B fields = null
        // AEditor successfully displays validation errors, 
        // but BEditors do not display errors, nor does 
        // ADriver get any errors from BEditor
    }  
}

}

Was it helpful?

Solution

When you create the VehiculeDTO, also create B subclasses :

A a = new A();
a.setBa(new B());
a.setBb(new B());
driver.edit(a);

OTHER TIPS

Here are some guidelines from my experiences using the Editor Framework, both personally, and also in industry. I have tried my best to make them relevant to your example.

  • Identify your "top-level" editor. In your case, it would be AEditor - in most other cases, it would be a view. Have the designated widget implement the Editor interface, with type param = your backing object (which you have done correctly).
  • Ensure your backing object A includes getters and setters and the fields are private. You have left them with default access which I don't think is a good idea.
  • Ensure your top level widget contains a sub-editor for each of the fields in A. They should share the same name as the corresponding field in A, or be annotated with @Path to indicate which field they relate to.
  • Your sub-editors should never have their own driver interface. They should either implement LeafValueEditor, ValueAwareEditor etc or an adapter interface such as IsEditor
  • In the constructor for your top level editor (here, AEditor), you need to initialise the driver and backing object:
  • ADriver driver =  GWT.create(ADriver.class);
    public AEditor {
        driver.initialize(this);
        driver.edit(new A());
    }
    

  • When you save, you should be calling driver.flush() to move the data from the top level editor into the backing object. Conversely, when you load, you should be calling driver.edit() with the backing object you wish to load
  • I have put up some Gists to demonstrate LeafValueEditor and IsEditor, in case you need help changing your sub-editor:

    LeafValueEditor

    IsEditor

    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top