Hi Group,

    This is Krishna. I need to implement the concept of
HistoryListener in my project. For that I tried to implement small
example.

    I have taken 3 pages each containing  Hyperlink say
HyperLink1,HyperLink2,HyperLink3. On click on each hyperlink It has to
redirect to other page. by using History Concept.

Problem :History is not maintaining properly if I click on Back or
Forward Button.

Actual Outcome:

        It has to redirect to the proper page by maintaining History
Concept.

For that I tried with the following Snippet.

Page1  Snippet :
------------------------


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Sample implements EntryPoint, HistoryListener
{
    VerticalPanel panel = new VerticalPanel();
        String oldToken = null;
        public void onModuleLoad() {
                // TODO Auto-generated method stub
                Hyperlink h = new Hyperlink();
                h.setText("HyperLink1");
                h.setTargetHistoryToken("HyperLink1");
                h.addClickListener(new ClickListener(){

                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                            panel.clear();
                            HyperLink1 p = new HyperLink1();
                            p.onModuleLoad();
                        }});
                panel.add(h);
                RootPanel.get().add(panel);
                History.addHistoryListener(this);
        }

        public void onHistoryChanged(String historyToken) {

                // TODO Auto-generated method stub
                // TODO Auto-generated method stub
                // If they are the same, no need to do anything
                if (oldToken != null && historyToken.equals(oldToken))
                        return;

                // Save the token for the next time round
                oldToken = historyToken;

                // Get the tab index
                if(historyToken.equals("HyperLink2")){
                        panel.clear();
                        HyperLink2 p = new HyperLink2();
                        p.onModuleLoad();
                }
                else
                if(historyToken.equals("HyperLink3")){
                        panel.clear();
                        Sample p = new Sample();
                        p.onModuleLoad();
                }
        }
}

Page2 Snippet :
----------------------



import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.Panel;

public class HyperLink1 extends Panel implements HistoryListener
{
    VerticalPanel panel = new VerticalPanel();
        String oldToken = null;
        public void onModuleLoad() {
                // TODO Auto-generated method stub
                Hyperlink h = new Hyperlink();
                h.setText("HyperLink2");
                h.setTargetHistoryToken("HyperLink2");
                h.addClickListener(new ClickListener(){

                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                                panel.clear();
                                HyperLink2 p = new HyperLink2();
                                p.onModuleLoad();
                        }});
                panel.add(h);
                RootPanel.get().add(panel);
                History.addHistoryListener(this);
        }

        public void onHistoryChanged(String historyToken) {

                // TODO Auto-generated method stub
                if (oldToken != null && historyToken.equals(oldToken))
                        return;

                // Save the token for the next time round
                oldToken = historyToken;

                // Get the tab index
                if(historyToken.equals("HyperLink2")){
                        panel.clear();
                        HyperLink2 p = new HyperLink2();
                        p.onModuleLoad();
                }
                else
                if(historyToken.equals("HyperLink3")){
                        panel.clear();
                        Sample p = new Sample();
                        p.onModuleLoad();
                }
        }
}


Page 3 Snippet :
------------------------


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class HyperLink2 implements EntryPoint, HistoryListener{
        VerticalPanel panel = new VerticalPanel();
        String oldToken = null;
        public void onModuleLoad() {
                // TODO Auto-generated method stub
                Hyperlink h = new Hyperlink();
                h.setText("HyperLink3");
                h.setTargetHistoryToken("HyperLink3");
                h.addClickListener(new ClickListener(){

                        public void onClick(Widget sender) {
                                // TODO Auto-generated method stub
                                panel.clear();
                                Sample m = new Sample();
                                m.onModuleLoad();
                        }});
                panel.add(h);
                RootPanel.get().add(panel);
                History.addHistoryListener(this);
        }

        public void onHistoryChanged(String historyToken) {

                // TODO Auto-generated method stub
                if (oldToken != null && historyToken.equals(oldToken))
                        return;

                // Save the token for the next time round
                oldToken = historyToken;

                // Get the tab index
                if(historyToken.equals("HyperLink1")){
                        panel.clear();
                        HyperLink1 p = new HyperLink1();
                        p.onModuleLoad();
                }
                else
                if(historyToken.equals("HyperLink3")){
                        panel.clear();
                        Sample p = new Sample();
                        p.onModuleLoad();
                }
        }
}

If you need further information Please let me know in advance.

Thanks in advance for your earliest reply.....


--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to