5

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();
    }
}
4

1 回答 1

2

以下几行应该可以完成这项工作:

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;
    }

}

但不幸的是,GWT.create()只能接受一个不能泛型的类文字。所以,我想你仍然必须在Driver某个地方创建你的接口。我添加了一个driver参数,initialize()以便您可以GWT.create()在外部调用并将结果传入。

如果您有一些特定于Vendoror的代码,Person则可以将它们抽象为抽象方法,例如

protected abstract void doSomethingWithEditedObject(O object); 

并在EditWorkflow.

如果您继续创建 的子类EditWorkflow,则可以将Driver接口放在它们各自的EditWorkflow类中

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...
    }
}
于 2012-10-19T16:58:51.857 回答