javagwtgwt-places

How to implement GWT PlaceHistoryMapper with custom tokens?


I'd like my small GWT app to have all of the following "bookmarkable" Places:

http://www.mywebapp.com                 -->     "home page"
http://www.mywebapp.com/login           -->     login screen
http://www.mywebapp.com/main            -->     main menu, after logged in
http://www.mywebapp.com/start           -->     start of a transactional process
http://www.mywebapp.com/complete        -->     end of transactional process (receipt)

So I went ahead and created 5 Place subclasses, all of which take the following form:

public class LoginPlace extends Place {
    // Intentionally left void because I'm not sure
    // what to implement here...
}

And have corresponding tokenizers:

public class LoginPlaceTokenizer extends PlaceTokenizer<LoginPlace> {
    @Override
    public LoginPlace getPlace(String token) {
        // ???
    }

    @Override
    public String getToken(LoginPlace place) {
        // ???
    }
}

I'm trying to implement a PlaceHistoryMapper for my app:

@WithTokenizers({
    HomePlaceTokenizer.class,
    LoginPlaceTokenizer.class,
    MainMenuPlaceTokenizer.class
    // etc.
})
public class MyWebAppPlaceHistoryMapper implements PlaceHistoryMapper {
    @Override
    public Place getPlace(String token) {
        // ???
    }

    @Override
    public String getToken(Place place) {
        // ???
    }
}

The companion getPlace/getToken methods in the PlaceTokenizer<T> subclasses and in MyWebAppPlaceHistoryMapper seem to be doing the same thing. Are they? If so, do I just use the same code inside both of them? If they are not the same, how are they different, and how should I be implementing them?

Keep in mind the URL tokens that I want as the bookmarkable places in the app - I don't want to use GWT's default someDisplay:SomePlace tokens. Thanks in advance!


Solution

  • Either you use an interface annotated with @WithTokenizers and let GWT generate the implementation from a GWT.create(MyWebAppPlaceHistoryMapper.class), or you implement the PlaceHistoryMapper interface "by hand" and you don't need PlaceTokenizers.

    With GWT.create(), GWT will implement the getPlace and getToken methods to dispatch to the appropriate PlaceTokenizer depending on the prefix of the token (using a if…else cascade with prefix.equals(...), based on the @Prefix annotations on the PlaceTokenizers) or the type of the Place (using a if…else cascade with instanceof, based on the generic type of the PlaceTokenizers).