Thursday, July 10, 2014

View in GWT using the UiBinder technique.

This is an implementation of building a View in GWT using the UiBinder technique. In part 1 you can find the basic setup for this project. 

PART 2 

SearchPresenter (Class)
  
package helloworld.client.view;
import helloworld.client.presenter.SearchPresenter;
import com.google.gwt.user.client.ui.Widget;
public interface SearchView {
 public interface Presenter { }
 void setPresenter(SearchPresenter presenter);
 Widget asWidget();
}

SearchView (Interface)

package helloworld.client.presenter;
import helloworld.client.view.SearchView;
import com.google.gwt.user.client.ui.HasWidgets;
public class SearchPresenter implements Presenter, SearchView.Presenter {

    private final SearchView view;

    /** Constructor **/
    public SearchPresenter(SearchView view) {
        this.view = view;
        bind();
    }

    public final void bind() {
        view.setPresenter(this);
    }

    /**************
     * SET VIEW *
     **************/

    @Override
    public final void go(final HasWidgets container) {
        container.clear();
        container.add(view.asWidget());
    }

*The "owner" class is instantiated inside the function onModuleLoad().

SearchViewImpl ("Owner" Class)
  
package helloworld.client.view;

import *HINT:Hit {ctrl+shft+O} if you use Eclipse*

public class SearchViewImpl extends Composite implements SearchView {

 @UiField TextBox searchBox;
 @UiField Button buttonSubmit;
    
 private static SearchViewUiBinder uiBinder = GWT.create(SearchViewUiBinder.class);

 @UiTemplate("SearchView.ui.xml")
 interface SearchViewUiBinder extends UiBinder { }

 public SearchViewImpl() {
  initWidget(uiBinder.createAndBindUi(this)); 
 }
 
 /**************************
  * Connect with Presenter *
  **************************/

 private SearchPresenter presenter;

 public void setPresenter(SearchPresenter presenter) {
  this.presenter = presenter;
 }
  
}

We can access fields by using ui:field="name_Of_Field_to_manipulate_with_Java". Below is the xml file used by UiBinder in order to render the page of the web application.
  
 

 
 

No comments:

Post a Comment