Question

My Site has on the left a GWT-Tree. In the center is a GWT-TabBar.

Both parts are implemented as Views/Activities/Places. I have two tokenizer: "m" for the tree and "t" for the tabs.

If I visit one place (goTo()) only this place will be used to generate the history token. But I would like to see this: <page>#m:sub/sub/sub;t:map

I actually thought that the hole idea of activities&places. I don't see the point to have multiple tokenizer, when only one tokenizer can provide a token at once.

Was it helpful?

Solution

You cannot display two different tokens #m: and #t: at the same time as you cannot be in two places at the same time.

So if both tabs and tree are displaying at the same time, then the state of both must be stored at once in the same place.

This is more or less what you need.

public class ExamplePlace extends Place {

    public String treePosition = "/";

    public int tabIndex = 0;

    public ExamplePlace() {
        super();
    }

    public ExamplePlace(String treePosition, int tabIndex) {
        this.treePosition = treePosition;
        this.tabIndex = tabIndex;
    }

    @Prefix("overview")
    public static class Tokenizer implements PlaceTokenizer<ExamplePlace> {


        /**
         * parse token to get state
         * 
         */
        @Override
        public ExamplePlace getPlace(String token) {
            String treePosition = "";
            int tabIndex = 0;
            String[] states = token.split(";");
            for (String state : states) {
                String[] mapping = state.split("=");
                if (mapping.length == 2) {
                    if ("t".equals(mapping[0])) {
                        treePosition = mapping[1];
                    }
                    if ("m".equals(mapping[0])) {
                        try {
                            tabIndex = Integer.valueOf(mapping[1]);
                        } catch (Throwable e) {
                        }
                    }
                }
            }
            return new ExamplePlace(treePosition, tabIndex);
        }


        /**
         * store state in token
         * 
         */
        @Override
        public String getToken(ExamplePlace place) {
            StringBuffer sb = new StringBuffer();
            if (place.getTreePosition()!=null) {
                sb.append("t").append("=").append(place.getTreePosition());
               sb.append(";");
            }
            sb.append("m=").append(place.getTabIndex());
            return sb.toString();
        }

    }

    public String getTreePosition() {
        return treePosition;
    }

    public void setTreePosition(String treePosition) {
        this.treePosition = treePosition;
    }

    public int getTabIndex() {
        return tabIndex;
    }

    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    }

}

This will give you URLs that look like ;

index.html#overview:t=/subtree/subtree/leaf;m=2

You might run in trouble with the forward slashes in the token, not sure. Change them to some other character if necessary;

The activity receives the incoming place and inject the state into the view;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top