Search code examples
javagwtgwt-rpcgwt2gwt-mvp

How to handle Datagrid column's events from Presenter?


I am using GWT MVP and I would like to handle events in columns of DataGrid from Presenter. From GWT DataGrid ShowCase ...

    // First name.
Column<ContactInfo, String> firstNameColumn =
    new Column<ContactInfo, String>(new EditTextCell()) {
      @Override
      public String getValue(ContactInfo object) {
        return object.getFirstName();
      }
    };
firstNameColumn.setSortable(true);
sortHandler.setComparator(firstNameColumn, new Comparator<ContactInfo>() {
  @Override
  public int compare(ContactInfo o1, ContactInfo o2) {
    return o1.getFirstName().compareTo(o2.getFirstName());
  }
});
dataGrid.addColumn(firstNameColumn, constants.cwDataGridColumnFirstName());
firstNameColumn.setFieldUpdater(new FieldUpdater<ContactInfo, String>() {
  @Override
  public void update(int index, ContactInfo object, String value) {
    // Here I would like to call RPC methods
  }
});
dataGrid.setColumnWidth(firstNameColumn, 20, Unit.PCT);

Above example , this code is writing in View side and it just really example. As mvp pattern , I just need to call rpc request to update database. This can't be done in View side but I have no idea how to set FieldUpdater from my presenter. Please help me how can I figure it out ? Thanks greatly .


Solution

  • There are two ways to do this (MVP I or MVP II way):

    MVP 1 style:

    http://www.gwtproject.org/articles/mvp-architecture.html

    Create a setter on your View (i.e. setFieldUpdater(FieldUpdater<ContactInfo, String>)) that takes a FieldUpdater and adds it to the column. From the Presenter you can call (provided that view is the reference to your View).

    Presenter:

    view.setFieldUpdater(new FieldUpdater<ContactInfo, String>() {
      @Override
      public void update(int index, ContactInfo object, String value) {
        // Make the RPC call
      }
    });
    

    MVP2 style:

    http://www.gwtproject.org/articles/mvp-architecture-2.html

    If you have a reference from your View back to your Presenter. Then you can call a function on your Presenter.

    View:

    firstNameColumn.setFieldUpdater(new FieldUpdater<ContactInfo, String>() {
      @Override
      public void update(int index, ContactInfo object, String value) {
        getPresenter().onUpdate(info,value);
      }
    });