Mvp4g multiple presenters – Part 2
May 11th, 2011 Javier Ochoa, Consultant (email the author)
Continuing from part 1, now let’s look at a way of using Mvp4g activation/deactivation of handlers with the setActivated method. Here we want to get rid of the IF condition on each and every event between our presenter groups. Here is a quick summary of steps to achieve this:
1. Have a presenter registry (maintain the instances references)
2. Write a Mvp4g Event Filter
3. Differentiate events to be filtered by event name.
Presenter Registry in PresenterHandler
First, we create an event handler in charge of instantiating our multiple presenters and keep a registry of the presenters organized per group, something like this:
@EventHandler
public class PresenterHandler
extends BaseEventHandler<MultiPresentersEventBus> {
// Map Registry <eventGroup,List<presenters>>
private Map<String,
List<AbstractGroupPresenter<?,
MultiPresentersEventBus>>> presenterMap;
public PresenterHandler() {
presenterMap = new HashMap<String,
List<AbstractGroupPresenter<?,
MultiPresentersEventBus>>>();
}
private void addWidget(
Class<? extends AbstractGroupPresenter<?,
MultiPresentersEventBus>> componentClass,
String group) {
AbstractGroupPresenter<?, MultiPresentersEventBus> presenter =
(AbstractGroupPresenter<?, MultiPresentersEventBus>) eventBus
.addHandler(componentClass, false);
presenter.setGroup(group);
presenter.bind();
// If is not in the registry - add this group
if (!presenterMap.containsKey(group)) {
presenterMap.put(group,
new ArrayList<AbstractGroupPresenter<?,
MultiPresentersEventBus>>());
}
presenterMap.get(group).add(presenter);
eventBus.displayWidget((Widget) presenter.getView());
}
/**
* Event to kick off the presenters instantiation
*/
public void onCreatePresenters() {
addWidget(SenderPresenter.class, "one");
addWidget(SenderPresenter.class, "two");
addWidget(SenderPresenter.class, "three");
addWidget(ReceiverPresenter.class, "one");
addWidget(ReceiverPresenter.class, "two");
addWidget(ReceiverPresenter.class, "three");
...
}
}
Event Filter
Next we need to write an event filter which will be triggered on each and every event fired in our Mvp4g event bus. For this we create MultiPresentersEventFilter class like this:
public class MultiPresentersEventFilter implements
EventFilter<MultiPresentersEventBus> {
/** GIN inject the presenter handler instance */
@Inject
private PresenterHandler presenterHandler;
public boolean filterEvent(String eventName,
Object[] params,
MultiPresentersEventBus eventBus) {
... // Event to be filtered
}
}
Notice how we are injecting the PresenterHandler in here (more about this injection shortly), and add this line to our event bus interface:
@Filters(filterClasses = MultiPresentersEventFilter.class)
@Events(startView = RootView.class)
public interface MultiPresentersEventBus extends EventBus {
Differentiate events to be filtered — ending with $
Now we are going to modify our event to be filtered: setSelectedItem(String group, String value). We are going to use a distinctive name for events to be filtered. In this case the new event would be:
void setSelectedItem$(String group, String value);
This way we can add the following code in the filterEvent method from MultiPresentersEventFilter. Here is where the presenter handler instance is required with its “activateGroup” method:
public boolean filterEvent(String eventName, Object[] params,
MultiPresentersEventBus eventBus) {
// Deactivate presenters registered for a group
// when the event name ends in "$"
if (eventName.endsWith("$")) {
presenterHandler.activateGroup((String) params[0]);
}
// Always return true to forward event
// to all "active" handlers
return true;
}
And the actiaveGroup method in the PresenterHandler looks like this:
/**
* Activate the presenter list for a particular event group
*/
public void activateGroup(String groupToActivate) {
for (String group : presenterMap.keySet()) {
for (AbstractGroupPresenter<?,
MultiPresentersEventBus> presenter
: presenterMap.get(group)) {
// When activating an empty group
// activate all presenters.
if (groupToActivate == null
|| groupToActivate.equals("")
|| groupToActivate.equals(group)) {
presenter.setActivated(true);
} else {
presenter.setActivated(false);
}
}
}
}
And the last change we need to do is in our ReceiverPresenter for the onSetSelectedItem method:
public void onSetSelectedItem$(String group,
String value) {
// No more IF statement at this level
view.setReceivedValue(value);
}
And that’s it. We are now registering and calling setActivated(true)/setActivated(false) for the presenters when the event is to be filtered on each triggered event. This way you can now code your events to be filtered with a distinctive name using “$” or any other special convention, passing the group as the first parameter of that event, and automatically your presenter would be called only when the event has the same group.
Here is the complete source code for your curiosity.
In the next post I will go a little ahead and show you how I got rid of the “$” in the event name, and use the GWT generator instead to get the same behavior. On top of that, I will explain a different way of activating/deactivating presenters without a separate registry.
Entry Filed under: Agile and Development
Pages
Categories
- Agile and Development
- Application Modernization
- Cloud Applications
- Process Integration
- Summa
- Technology + Healthcare
- Uncategorized
Most Recent Posts
- Summa Is Award Finalist at IBM’s Impact 2012
- Working with JqGrid and ASP.NET MVC - Setting up a base jqgrid parameters class
- Rebase a Slave Mercurial Repo to a Subversion Master
- The Social Enterprise Part 2 – How To Set Up Chatter In Less Than 30 Minutes
- Implement Clear Governance for BRMS
Feeds
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Apr | Jun » | |||||
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||

2 Comments Add your own
1. Mvp4g multiple presenters&hellip | May 11th, 2011 at 8:56 am
[...] the next post I will explain the details of doing some event filters to get rid of the IF statements on each [...]
2. Mvp4g multiple presenters&hellip | July 8th, 2011 at 8:34 pm
[...] 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 that are to [...]
Leave a Comment
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