如何正确配置我的GET活动和活动映射器?

时间:2012-11-08 12:50:45

标签: java gwt gwt-platform gwt-activities

我有以下课程:

// 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);
    }
}

所以我开始对于活动框架有一些相似之处,但是当我试图冒险离开 Hello,GWT

在我的应用程序的主要GWT模块MyWebAppModule中,我正在尝试实例化和配置这3个对象。但是因为我仍然不确定如何使用他们的API,我遇到了实施问题:

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. 显然,我没有正确实现DashboardActivityMapperDashboardActivity,因此当我尝试在MyWebAppModule#onModuleLoad内实例化并连接其实例时,会导致问题。有人可以帮我正确实现吗?一旦我得到一个工作示例,我相信我将能够连接活动框架中的其余“点”。
  2. 除非我遗漏了某些内容,否则上面的ActivityManager manager似乎没有被添加到任何其他类中。它是否正确? GWT是否会确保它在内存中保持活跃状态​​,以便它始终在eventBus监听我为PlaceChangeEvent传递它?或者我需要添加/注册它吗?
  3. 提前致谢!

1 个答案:

答案 0 :(得分:0)

  1. 您的DashboardActivityMapper负责在到达指定地点(例如DashboardActivity)时创建并返回HomePlace。这是您缺少的信息吗? 请参阅https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces#ActivityMapper

  2. 只要EventBus具有ActivityManager的句柄(因此它可以向其发送事件)。但是,当使用非空值调用ActivityManager时,EventBus会在其setDisplay方法中注册null(并在setDisplay传递给{时自行取消注册{1}}),并且您不会调用setDisplay(这意味着甚至不会显示活动)