Mvp4g multiple presenters – Part 1

April 27th, 2011 Javier Ochoa, Consultant  (email the author)

This entry is part 6 of 9 in the series Developing GWT Applications

Mvp4g is a great library to manage your large GWT application structure. Especially when using MVP where your Presenters talk to each other using events. Without Mvp4g you can follow plain vanilla GWT but you will need to create and manage a set of Handlers and Events to communicate those presenters. And on top of that, you’ll need to manage the registry and instantiation of those presenter instances.

In an application we recently built we used Mvp4g. We had this special requirement that the screen layout was to be built dynamically. That meant we would need to create presenters on the fly, but not only that, there might be multiple instances of a presenter at any given time – e.g. one screen would have two chart components with different supporting data. The presenter responsibility was the same for both, display a bar chart from the under laying data (a simple multi-column query).

I’ve used a simple concept of sender and receiver for events in groups. These are really simple implementations of one presenter sending events on list box value change and another presenter receiving that event. Look at the sample demo:

Quick running demo with this idea implemented

Quick running demo with this idea implemented

This blog series is divided in three parts:

  • Part 1: define multiple=true and event groups.
  • Part 2: event filter and presenters deactivation using Mvp4g features.
  • Part 3: write a GWT generator to distinguish event types to be filtered from the rest.

Part 1: Define multiple=true and event groups

1. Multiple=true

The crucial part in the use of Mvp4g was the need for multiple instances of presenters, a feature that was introduced on version 1.2 (a.k.a multiple=true). The presenter needs to be defined like this:

@Presenter(view = SenderView.class, multiple = true)
public class SenderPresenter extends
        AbstractGroupPresenter<ISenderView, MultiPresentersEventBus> {

When you mark a presenter with multiple=true, you need to manage the creation of the instances with something like:

SenderPresenter presenter = (SenderPresenter)
              eventBus.addHandler(SenderPresenter.class);

This line creates an instance and attaches it to the event bus and from here this instance is ready to listen to events.

2. Event groups

Back to our requirement, if I have multiple instances of the same presenter and would like to control how they communicate with each other I would need to group them. In this example I have this event to communicate the selected value:

@Event(handlers=ReceiverPresenter.class)
void setSelectedValue(String value);

Now what will happen if on my screen I have three ReceiverPresenter instances, and throw a setSelectedValue event? Such as:

eventBus.setSelectedValue("Texas");

All three presenters will react to that event. One simple way of controlling which presenter should listen to this event is making each of those presenters part of a group and have the group as a parameter in the event. Here are the main changes:

a. Our presenter has a group member (with setter/getter):

private String group;

b. When creating the presenter we register the group:

ReceiverPresenter presenterA = (ReceiverPresenter)
                    event.addHandler(ReceiverPresenter.class);
presenterA.setGroup("A");
ReceiverPresenter presenterB = (ReceiverPresenter )
                    event.addHandler(ReceiverPresenter .class);
presenterB.setGroup("B");
ReceiverPresenter presenterC = (ReceiverPresenter )
                    event.addHandler(ReceiverPresenter.class);
presenterC.setGroup("C");

c. Add the event group to the event:

@Event(handlers=ReceiverPresenter.class)
void setSelectedValue(String group, String value);

d. And lastly implement the event handling in the presenter checking the event group:

public void onSetSelectedValue(String toGroup, String value){
    if (this.group.equals(toGroup)){
        //do something...
    }
}

You can take a look at the source code for this example here.

In the next post I will explain the details of doing some event filters to get rid of the IF statements on each event to be filtered. That gives you a little bit more freedom when receiving events in your presenter. All you need to worry about is handling the event not filtering out events.

Entry Filed under: Agile and Development

2 Comments Add your own

  • 1. Mvp4g multiple presenters&hellip  |  May 11th, 2011 at 8:54 am

    [...] from part 1, now let’s look at a way of using Mvp4g activation/deactivation of handlers with the [...]

  • 2. Mvp4g multiple presenters&hellip  |  June 29th, 2011 at 11:58 am

    [...] is the third in the Mvp4g Multiple Presenters series (part 1 and part 2). In this post I will demonstrate how to use the GWT generator to differentiate events [...]

Leave a Comment

Required

Required, hidden


six + 8 =

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

© 2010-2012 Summa All Rights Reserved -- Copyright notice by Blog Copyright