MVP Contacts

July 31, 2010 at 6:30 pm 1 comment

Google MVP Contacts example

 The HandlerManager class is used on client side to act as the event bus.

Entry point – The contacts class is defined as the  entry point class in the gwt.xml file. The following OnModuleLoad() method performs the following:
ContactsServiceAsync rpcService = GWT.create(ContactsService.class);
HandlerManager eventBus = new HandlerManager(null);
AppController appViewer = new AppController(rpcService, eventBus);
appViewer.go(RootPanel.get());

Host Page – This is the JSP page which invokes the GWT via the following javascript snippet:
Contacts.jsp:
src="contacts/contacts.nocache.js" type="text/javascript">

Event Handlers  – the event handler is an interface and is defined by extending an EventHandler as shown below:
public interface AddContactEventHandler extends EventHandler
   void onAddContact(AddContactEvent event);

Events – an Event extend a GwtEvent and makes use of  an EventHandler.  This one goes with the EventHandler thate we just defined:

public class AddContactEvent extends GwtEvent
public static Type TYPE = new Type();

@Override
public Type getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(AddContactEventHandler handler) {
handler.onAddContact(this);
}

App Controller – the app controller implements a presenter. In the presenter, we define which events we are registered to listen for and are sent on to handle in the view:
private void bind() {
History.addValueChangeHandler(this);
eventBus.addHandler(AddContactEvent.TYPE,
new AddContactEventHandler() {
public void onAddContact(AddContactEvent event) {
doAddNewContact();
}
});

By creating a new item an event gets fired

private void doAddNewContact()
{
History.newItem("add");
}

Then, the following is invoked:


public void onValueChange(ValueChangeEvent event) {
String token = event.getValue();

if (token != null) {
Presenter presenter = null;

if (token.equals(“list”)) {
presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
}
else if (token.equals(“add”)) {
presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
}
else if (token.equals(“edit”)) {
presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
}

if (presenter != null) {
presenter.go(container);
}
}
}
}

Presenter – a new ContactsView is passed into ContactsPresenter by the AppController.
this is followed by a presenter.go()


public abstract interface Presenter {
public abstract void go(final HasWidgets container);
}

The ContactsPresenter go method:

public void go(final HasWidgets container) {
bind();
container.clear();
container.add(display.asWidget());
fetchContactDetails();
}

ContactsPresenter implements Presenter

public interface Display {
HasClickHandlers getAddButton();
HasClickHandlers getDeleteButton();
HasClickHandlers getList();
void setData(List data);
int getClickedRow(ClickEvent event);
List getSelectedRows();
Widget asWidget();
}

In the bind method in the ContactsPresenter, we set up the add button to fire:

display.getAddButton().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
eventBus.fireEvent(new AddContactEvent());
}
});

View – the view implements the display class and is invoked via the presenter.

public class ContactsView extends Composite implements ContactsPresenter.Display

The View implements the Presenter display interface. For example one method in the display interface is
* HasClickHandlers getAddButton();

Which is implemented here:


public HasClickHandlers getAddButton() {
return addButton;
}

Advertisements

Entry filed under: Uncategorized.

App Engine Trials MVP Contacts example with a different flavor

1 Comment Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


@davidmbloom


%d bloggers like this: