Base presenter and view classes for gwt-presenter
Posted by David Chandler on October 10, 2009
When your presenters extend gwt-presenter’s WidgetPresenter, you are required to implement four methods that are often unused. In order to avoid boilerplate in all my presenters, as well as to do other things that are common to all my presenters, I’ve created an abstract base class that extends WidgetPresenter.
public abstract class MyBasePresenter<D extends MyBasePresenter.Display> extends WidgetPresenter<D> { public interface Display extends WidgetDisplay { } public MyBasePresenter(final D display, final EventBus eventBus) { super(display, eventBus); } @Override protected void onBind() { // TODO Auto-generated method stub } @Override protected void onPlaceRequest(PlaceRequest request) { // TODO Auto-generated method stub } @Override protected void onUnbind() { // TODO Auto-generated method stub } @Override public void refreshDisplay() { // TODO Auto-generated method stub } }
Now instead of extending WidgetPresenter directly, I can extend MyBasePresenter:
public class ManageListsPresenter extends MyBasePresenter<ManageListsPresenter.Display> { public interface Display extends MyBasePresenter.Display { ... } @Inject public ManageListsPresenter(final Display display, final EventBus eventBus) { super(display, eventBus); bind(); } ... }
In similar fashion, I define a corresponding BaseView that eliminates the need for the startProcessing() and stopProcessing() methods required by gwt-presenter’s WidgetDisplay interface. Besides eliminating boilerplate, I use the base view constructor to inject references to my applications Constants and Images singletons (see this previous post):
public abstract class MyBaseView implements MyBasePresenter.Display { protected final RoaConstants constants; protected final RoaImages images; protected BaseView(final RoaConstants constants, final RoaImages images) { this.constants = constants; this.images = images; } @Override public void startProcessing() { // TODO Auto-generated method stub } @Override public void stopProcessing() { // TODO Auto-generated method stub } }
Now all views that extend MyBaseView will have less boilerplate and will automatically have access to the constants and images interfaces.
public class ManageListsView extends MyBaseView implements ManageListsPresenter.Display { @Inject public ManageListsView(final RoaConstants constants, final RoaImages images) { super(constants, images); ... } ... }
Darren said
Nice post. What about reavealDisplay() in MyBasePresnter?
David Chandler said
revealDisplay() is implemented as follows in gwt-presenter’s BasicPresenter class from which WidgetPresenter is derived, so it’s not necessary to implement it elsewhere, though you certainly could.
public void revealDisplay() { eventBus.fireEvent( new PresenterRevealedEvent( this ) ); }
realgt said
i think theres a disconnect here between the published 1.0.0 jar for gwt-presenter and the trunk code. the jar doesn’t have the PresenterRevealedEvent class nor the revealDisplay() implementation in BasicPresenter
David Chandler said
You’re correct. This was written against 1.1.0-SNAPSHOT built from trunk on Sep 23, 2009.
I understand there are also changes on the gwt-presenter “replace” branch that, once released, will make this post seriously out-of-sync 😦
Honza said
Great article, I have a question about MyBaseView. If all my views will be derived from MyBaseView, there is no way (or there is?) to extend any standard widget, like if I have something like:
public class LeftMenuTree extends Tree implements LeftMenuTreePresenter.Display {
…
}
The view is then my own component, which can be reused elsewhere…
David Chandler said
Rather than extending a widget directly, your view can maintain a reference to a custom widget. This way, your view may contain multiple widgets. Hope that helps…
How to reduce startup time with gwt-presenter « TurboManage said
[…] I’ve created a new method onFirstRequest() in my base presenter. It gets called on the first invocation of onPlaceRequest(), which simply increments a counter in a […]
Jay said
I’m trying to implement a base class for my view and presenter, but I’m having problems with integrating with uiBuilder because it requires that I extend from a composite with initWidget( widget ) in view. Has anyone been able to use base class with the uiBuilder?
Also, thanks so much for this info~~