javagwtgwt-platformgwt-activities

How to properly configure my GWT Activity and ActivityMapper?


I have the following classes:

// The "backing bean" for DashboardDisplay.ui.xml (which is omitted for brevity).
public class DashboardDisplay extends Composite implements AcceptsOneWidget {
    private EventBus eventBus;
    private PlaceController placeController;
    private IsWidget content = null;

    public DashboardDisplay(EventBus eventBus, PlaceController placeController) {
        super();

        setEventBus(eventBus);
        setPlaceController(placeController);
    }

    // Getters and setters for all properties
    // ...

    @Override
    public void setWidget(IsWidget widget) {
        setContent(widget);
    }
}

public class DashboardActivity extends AbstractActivity {
    private AcceptsOneWidget container;
    private ActivityManager manager;
    private DashboardDisplay display;

    public DashboardActivity(AcceptsOneWidget container, ActivityManager manager,
        DashboardDisplay display) {
        super();

        setContainer(container);
        setManager(manager);
        setDisplay(display);

        // I *believe* this ensures that the 'panel' passed in to the
        // start method is always our 'container'. Yes?
        manager.setDisplay(container);
    }

    // Getters and setters for all properties
    // ...

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        display.setEventBus(eventBus);
        panel.setWidget(display);
    }
}

public class DashboardActivityMapper implements ActivityMapper {
    private DashboardActivity activity = null;

    // Getters and setter for activity.

    @Override
    public Activity getActivity(Place place) {
        // Leaving arguments null because I'm confused about the object graph
        // (read below).
        return new DashboardActivity(null, null, null);
    }
}

So I'm starting to have some semblance of an understanding of the Activities framework, but still so new to it that when I try to venture away from the Hello, GWT examples, I run into obstacles with the API almost instantly.

In my app's main GWT module, MyWebAppModule, I'm trying to instantiate and configure these 3 objects. But because I'm still unsure of how to use their API, I'm running into implementation problems:

public class MyWebAppModule implements EntryPoint {
    @Override
    public void onModuleLoad() {
        // I'll worry about DI and getting GIN to work after
        // I figure this basic stuff out.
        EventBus eventBus = new SimpleEventBus();
        PlaceController placeController = new PlaceController(eventBus);
        MyWebAppPlaceHistoryMapper placeMapper = new MyWebAppPlaceHistoryMapper();
        HomePlace homePlace = new HomePlace();

        // WHERE THE PROBLEM STARTS
        DashboardActivityMapper activityMapper = new DashboardActivityMapper();
        ActivityManager manager = new ActivityManager(activityMapper, eventBus);

        // Really not sure how to inject/configure this properly.
        DashboardActivity dashboardActivity = new DashboardActivity(null, null, null);
        // WHERE THE PROBLEM ENDS

        PlaceHistoryHandler historian = new PlaceHistoryHandler(placeMapper);
        historian.register(placeController, eventBus, homePlace);
        historian.handleCurrentHistory();
    }
}
  1. Clearly, I haven't implemented DashboardActivityMapper and DashboardActivity correctly, so it's causing problems when I try to instantiate and wire their instances inside MyWebAppModule#onModuleLoad. Could someone help me implement this correctly? Once I get a working example down, I'm sure I'll be able to connect the rest of the "dots" in the Activities framework.
  2. Unless I'm missing something, the ActivityManager manager above doesn't seem to get added to any other class. Is this correct? Will GWT ensure that it stays alive in memory so that it is always listening on the eventBus I pass it for PlaceChangeEvents? Or do I need to add/register it to anything?

Thanks in advance!


Solution

    1. Your DashboardActivityMapper is responsible of creating and returning a DashboardActivity when a given place (e.g. the HomePlace) is reached. Is this the information you're missing?
      See https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces#ActivityMapper

    2. As long as the EventBus has a handle to the ActivityManager (so it can dispatch events to it). However, ActivityManager registers itself on the EventBus in its setDisplay method, when called with a non-null value (and de-registers itself when a null is passed to setDisplay), and you don't call setDisplay (which incidentally mean that the activities wouldn't even be displayed)