Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply java generic in gwt editor

I am using gwt editor framework to bind form in gwt. I was able to bind form successfully, but my problem is I need to write too many code. I think using generic will shorten my code but I couldn't do it.

The code for EditVendorWorkflow is almost same, the only difference is this two line

interface Driver extends  SimpleBeanEditorDriver<Vendor, VendorEditor>{}
private VendorEditor editor;

Example, If I write a PersonEditor the corresponding EditPersonWorkflow code will have

interface Driver extends  SimpleBeanEditorDriver<Person, PersonEditor>{}
private PersonEditor editor;

So basically there is a repetition.

Any Help is appreciable.

public class EditVendorWorkflow{
      interface Driver extends  SimpleBeanEditorDriver<Vendor, VendorEditor>{}

      Driver driver = GWT.create(Driver.class);

      private VendorEditor editor;

      void edit(Vendor p) {
        driver.initialize(editor);
        driver.edit(p);
      }

      void save() {
        Vendor edited = driver.flush();
        //doSomethingWithEditedVendor(edited);

      }

    public void initialize(VendorEditor editor) {
        this.editor = editor;
    }
}


public class VendorEditor extends Composite implements Editor<Vendor> {

    private static VendorEditorUiBinder uiBinder = GWT
            .create(VendorEditorUiBinder.class);
    @UiField Button save;
    @UiField TextBox address;
    @UiField TextBox contactName;
    @UiField ValueBoxEditorDecorator<String> contactMobileNo;
    @Path("department.name")
    @UiField TextBox deptName;
    interface VendorEditorUiBinder extends UiBinder<Widget, VendorEditor> {
    }

    private final EditVendorWorkflow vendorDriver;
    public VendorEditor(Vendor p) {
        initWidget(uiBinder.createAndBindUi(this));
        vendorDriver = GWT.create(EditVendorWorkflow.class);
        vendorDriver.initialize(this);
        vendorDriver.edit(p);
    }


    @UiHandler("save")
    void onSaveClick(ClickEvent event) {
        vendorDriver.save();
    }
}
like image 511
user510783 Avatar asked Nov 03 '22 14:11

user510783


1 Answers

Something along the following lines should do the job:

public class EditWorkflow<O, E extends Editor<O>> {

    SimpleBeanEditorDriver<O, E> driver; 

    private E editor;

    void edit(O p) {
        driver.initialize(editor);
        driver.edit(p);
    }

    void save() {
        O edited = driver.flush();
        //doSomethingWithEditedObject(edited);
    }

    public void initialize(E editor, SimpleBeanEditorDriver<O, E> driver) {
        this.editor = editor;
        this.driver = driver;
    }

}

But unfortunately, GWT.create() can only accept a class literal, which cannot be generic. So, I guess still you have to create your Driver interfaces somewhere. I added a driver parameter to initialize() so that you can call GWT.create() outside and pass the result in.

If you have some codes that specific to Vendor or Person, you can abstract them in abstract methods like

protected abstract void doSomethingWithEditedObject(O object); 

and implement them in subclasses of EditWorkflow.

If you go on to create subclasses of EditWorkflow, you can put Driver interfaces inside their respective EditWorkflow class

public class EditVendorWorkflow extends EditWorkflow<Vendor, VendorEditor> {

    interface Driver extends  SimpleBeanEditorDriver<Vendor, VendorEditor>{}

    public void initialize(VendorEditor editor) {
        super.initialize(editor, GWT.create(Driver.class));
    }

    @Override
    protected void doSomethingWithEditedObject(Vendor object) {
        // Code specific to class Vendor...
    }
}
like image 91
Saintali Avatar answered Nov 11 '22 14:11

Saintali