PlaceTokenizer and @Prefix

502 views
Skip to first unread message

pansen

unread,
Jul 11, 2011, 1:36:54 PM7/11/11
to Google Web Toolkit
Hey,

I try to implement some PlaceTokenizers, which indicate Token in a
defined scope.

E.g. I want some tokens ordered in "Photo", resulting to:

- "Photo:AlbumList"
- "Photo:Detail"
...

the GWT compiler is complaining about the usage of the same prefix
more than once. This completely confuses me, as a prefix doesn't makes
sense to me if its unique.

What part did I misunderstand?

Thx, Andi

@Prefix("Photo")
public static class Tokenizer implements
PlaceTokenizer<AlbumListPlace> {

/**
* from http://tbroyer.posterous.com/gwt-21-places-part-ii
*
* The returned value will them be prepended with the
PlaceTokenizer's
* @Prefix and a colon as a separator, and the resulting
String is used as the
* history token.
*/
@Override
public String getToken(AlbumListPlace place) {
return place.getUserId();
}

@Override
public AlbumListPlace getPlace(String token) {
return new AlbumListPlace(token);
}
}

Jens

unread,
Jul 11, 2011, 2:51:16 PM7/11/11
to google-we...@googlegroups.com
If you add "-gen <path/folder>" to your compiler args (or to dev mode args) you can see what GWT generates for you. That often helps to understand whats going on under the hood. If I remember correctly GWT generates a PlaceHistoryMapper for you that contains a long if - else if - else if .... - else block which checks all the prefixes you have defined in your tokenizers against the prefix in the url and then uses the corresponding tokenizer. If you have two tokenizer with the same prefix the code generator does not know which tokenizer to use for a given url.

In your case you would just have a single PhotoPlace (with its PhotoPlaceTokenizer) whose tokens would be AlbumList and Detail. In your ActivityMapper you could return different activities based on the token. Alternatively you can create an abstract PhotoPlace and let PhotoDetailPlace and PhotoAlbumListPlace extend from it. Then create a PhotoPlaceTokenizer that extends Tokenizer<PhotoPlace> and you are now in charge of both places in the same Tokenizer. That way you have moved some logic from ActivityMapper into the Tokenizer (basically the if that dispatches the token to return the correct activity will now return the correct place based on the token).

-- J.

pansen

unread,
Jul 11, 2011, 3:07:34 PM7/11/11
to Google Web Toolkit
Jens,

thanks a lot! I was also reading this:
http://groups.google.com/group/google-web-toolkit/browse_thread/thread/a58c31280fc8a537
and had some similar idea like yours; only using an interface instead
of the abstract ``PhotoPlace``.

But was I was wondering about while reading the thread above: we have
lots of ``PlaceTokenizer``, all without a Prefix annotation. How does
it come this works?

$ ack --type=java --ignore-dir=target --ignore-dir=test 'implements
PlaceTokenizer' . -B1
vz-gwt-main/src/main/java/net/vz/studivz/mobile/client/places/
StartPlace.java
17-
18: public static class Tokenizer implements
PlaceTokenizer<StartPlace> {

vz-gwt-widgets/src/main/java/net/vz/common/widgets/client/places/
AdExamplePlace.java
20-
21: public static class Tokenizer implements
PlaceTokenizer<AdExamplePlace> {

vz-gwt-widgets/src/main/java/net/vz/common/widgets/client/places/
BoardMessageCreatePlace.java
42-
43: public static class Tokenizer implements
PlaceTokenizer<BoardMessageCreatePlace> {

vz-gwt-widgets/src/main/java/net/vz/common/widgets/client/places/
BoardPlace.java
25-
26: public static class Tokenizer implements
PlaceTokenizer<BoardPlace> {
...

Cheers :) Andi

Jens

unread,
Jul 11, 2011, 3:27:41 PM7/11/11
to google-we...@googlegroups.com
Without a Prefix annotation the code generator will use the simple class name of corresponding place by default (and it knows the place because you parametrized the PlaceTokenizer interface with it). I guess you will see the same error if you try to provide two Tokenizers for the same place and both with no prefix annotation.

-- J.
Reply all
Reply to author
Forward
0 new messages