开发者

How to respond to URLs with GWT's built-in MVP-framework?

I'm building a very simple calendar app开发者_运维技巧 to get familiar with the MVP-framework introduced with the 2.1 version of GWT.

What I want to achieve is being able to switch between a list of scheduled appointments and a list of the avialable time.

I have created the a CalendarPlace, CalendarActivity, CalendarView and CalendarViewImpl. I know that to navigate to a different place i would call PlaceController.goTo(Place), so in my calendar app I would call:

clientFactory.getPlaceController.goTo(new CalendarPlace("freeTime");

The URL would be index.html#CalendarPlace:freeTime for the list of free time or

clientFactory.getPlaceController.goTo(new CalendarPlace("appointments");

for the list of scheduled appointments. The URL would be index.html#CalendarPlace:appointments

But the question is where do I respond to the different tokens? I guess the CalendarPlace would be the right place, but how would I do that?

Here is my source code(I took most of the boilerplate from the tutorial here:

CalendarPlace:

public class CalendarPlace extends Place {
    private String calendarName;
    public CalendarPlace(String token) {
        this.calendarName = token;
    }
    public String getCalendarName() {
        return calendarName;
    }
    public static class Tokenizer implements PlaceTokenizer<CalendarPlace> {
        @Override
        public CalendarPlace getPlace(String token) {
            return new CalendarPlace(token);
        }
        @Override
        public String getToken(CalendarPlace place) {
            return place.getCalendarName();
        }

    }
}

CalendarActivity:

public class CalendarActivity extends AbstractActivity
    implements
        CalendarView.Presenter {
    private ClientFactory clientFactory;
    private String name;
    public CalendarActivity(CalendarPlace place, ClientFactory clientFactory) {
        this.name = place.getCalendarName();
        this.clientFactory = clientFactory;
    }
    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }
    @Override
    public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
        CalendarView calendarView = clientFactory.getCalendarView();
        calendarView.setName(name);
        calendarView.setPresenter(this);
        containerWidget.setWidget(calendarView.asWidget());
    }
}

CalendarViewImpl:

public class CalendarViewImpl extends Composite implements CalendarView {
    private VerticalPanel content;
    private String name;
    private Presenter presenter;
    private OptionBox optionBox;
    public CalendarViewImpl() {
        //optionBox is used for navigation
        //optionBox is where I call PlaceController.goTo() from
        optionBox=new OptionBox();
        RootPanel.get("bluebar").add(optionBox);
        content=new VerticalPanel();
        this.initWidget(content);
    }
    @Override
    public void setPresenter(Presenter listener) {
        this.presenter=listener;
    }
    @Override
    public void setName(String calendarName) {
        this.name = calendarName;
    }

    public void displayFreeTime() {
        //called from somewhere to display the free time
    }
    public void getAppointments() {
        //called from somewhere to display the appointments
    }
}


In your CalendarActivity constructor you have access to the place, and therefore the token. Tuck it aside, and then in your start() method you can use it. Activities are meant to be lightweight objects, created for each new navigation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜