TurboManage

David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler


    Web app developer since 1994 and former Developer Advocate with Google now residing in Colorado. Besides tech, I enjoy landscape photography and share my work at ColoradoPhoto.gallery.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 241 other followers

  • Sleepless Nights…

    October 2009
    S M T W T F S
    « Sep   Nov »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Blog Stats

    • 857,971 hits

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);
        ...
	}
    ...
}
Advertisements

8 Responses to “Base presenter and view classes for gwt-presenter”

  1. Darren said

    Nice post. What about reavealDisplay() in MyBasePresnter?

    • 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

      • 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 😦

  2. 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…

  3. […] 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 […]

  4. 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~~

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

 
%d bloggers like this: