gwttokenbrowser-historygwt-2.4

Why does my History.newItem(someToken) not fire onValueChange()?


Even though it is correctly fired when I use History.fireCurrentHistoryState();

EDIT: All classes in the same package. Code updated -

TestHistory.java

public class TestHistory implements EntryPoint, ValueChangeHandler<String> {

    static boolean isLoggedIn = false;
    static final String PAGENAME = "mainscreen";
    public void onModuleLoad()
    {
        History.addValueChangeHandler(this);

        String startToken = History.getToken();
        System.out.println("onModuleLoad Called..... start token= -------"+startToken+"--------");
        if(!startToken.isEmpty())
            History.newItem(startToken);
        History.fireCurrentHistoryState(); //to execute onValueChange 1st time since 1st time history is not setup
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {

        String token = event.getValue();

        String args = "";
        int question = token.indexOf("?");
        if (question != -1) {
        args = token.substring(question + 1);
        token = token.substring(0, question);
        }

        if(!isLoggedIn)
        {
            if(token.isEmpty() || "login".equals(token))    //1st time opened the site normally
                new Login().display(false, RootPanel.get());
            else {
                new Login().display(true, RootPanel.get());
            }
        }
        else    //User has logged in
        {
            if(token.isEmpty() || "login".equals(token))
            {
                if(isLoggedIn)
                    Window.alert("Ur already logged in!!!");
                else
                    new Login().display(false, RootPanel.get());
            }
            else if("withdraw".equals(token))
                new Withdraw().display(RootPanel.get(), args);
            else if("deposit".equals(token))
                new Deposit().display(RootPanel.get(), args);
            else //token not clear
                Window.alert("Unrecognized token=" + token);
        }   

    }

}

Login.java

public class Login {
    static final String PAGENAME = "login";
    void display(final boolean hasTypedSomeToken, Panel myPanel) //Process login
    {
        System.out.println("login display called");
        Label displayLabel = new Label("This is the Login Page");
        Label enterName = new Label("Enter ur name");
        final TextBox txtName = new TextBox();
        Label enterPasswd = new Label("Enter ur Passwd");
        final TextBox txtPasswd = new TextBox();
        Button btnLogIn = new Button("Login", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

                /* Real app will check DB. Here we r jst chckng d txt fields hv value */
                if(txtName.getValue().length()>0 && txtPasswd.getValue().length()>0)
                {
                    TestHistory.isLoggedIn = true;
                    if(hasTypedSomeToken) {
                        //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                        History.newItem("login",false);
                        History.back();
                        System.out.println(History.getToken());
                    }
                    else{
                        myPanel.clear();
                                            Label displayLabel = new Label("Thank U for logging.);
                                            myPanel.add(displayLabel);
                                         }

                }   
            }
        });         
        myPanel.clear();
        myPanel.add(displayLabel);
        myPanel.add(enterName);
        myPanel.add(txtName);
        myPanel.add(enterPasswd);
        myPanel.add(txtPasswd);
        myPanel.add(btnLogIn);
    }
}

Deposit.java

public class Deposit {
    static final String PAGENAME = "deposit";
    void display(Panel myPanel, String param)
    {
        System.out.println("deposit display called");
        myPanel.clear();
        Label displayLabel = new Label("This is the Deposit Page & ur parameter = "+param+")");
        myPanel.add(displayLabel);
    }   
}

Withdraw.java

//similar to deposit.java


Solution

  • The problem was with the usage of History.newItem(). the problem was occuring when I was using the bookmarked url and calling History.newItem() with a new token. Since already a token was present for the same internal page and I was giving it a new token so there was some confusion and onValueChange() was not being called.

    Now Im clear that History.newItem() should be used when there is no token attached to the current view to mark the view with a token. Generally when a user opens a site normally (with no token), we should use history.newItem to mark the 1st view.

    Also worth noting is that History.fireCurrentHistoryState() just calls onValueChange with the current token. And by going through the GWT's Code I found that History.newItem() simply calls History.fireCurrentHistoryState()

    Actually if I replace

    if(!startToken.isEmpty())
                History.newItem(startToken);
            History.fireCurrentHistoryState();
    

    in my code with

       if(startToken.isEmpty())
                History.newItem("login");
            else
                History.fireCurrentHistoryState();
    

    & also the code

    if(hasTypedSomeToken) {
                            //History.back(); //send him to the URL(token) he bookmarked b4 loggin in
                            History.newItem("login",false);
                            History.back();
                            System.out.println(History.getToken());
                        }
    

    with

    if(hasTypedSomeToken) {
                        History.fireCurrentHistoryState();
                        System.out.println("getToken() in Login =   "+History.getToken());
                    }
    

    it works pretty well.