user592704
user592704

Reputation: 3704

GWT - refresh button/history/previous UI

I am just wondering...

When I click refresh button my gwt app comes to its default UI state despite its UI was modified during client-server interactions (callbacks) etc... But sometimes it is really essential thing to "cache" UI if user clicks refresh by mistake or reopened web page which user still logged-in;

So my question is... Is there a way to restore gwt app UI (its before-refreshed state) in some standard way? Can History tokens help for this kind of issue?

edit

Concerning the history tokens I saw this example :

History.addValueChangeHandler(new ValueChangeHandler<String>() {
      public void onValueChange(ValueChangeEvent<String> event) {
        String historyToken = event.getValue();

        // Parse the history token
        try {
          if (historyToken.substring(0, 4).equals("page")) {
            String tabIndexToken = historyToken.substring(4, 5);
            int tabIndex = Integer.parseInt(tabIndexToken);
            // Select the specified tab panel
            tabPanel.selectTab(tabIndex);
          } else {
            tabPanel.selectTab(0);
          }

        } catch (IndexOutOfBoundsException e) {
          tabPanel.selectTab(0);
        }
      }
    });

... and I could notice it restores tabIndex from history; so will it help if tab panel won't be init-ed by module load (by default) but something this way:

//on button click...
    getSimplePanel().setWidget(new MyTabbedPane());

edit

To be more clear here is my test code which I am trying to figure out how to restore MainUI I mean its previous UI state as if refresh button wasn't clicked.

the EntryPoint...

public class Main implements EntryPoint {
    private SimplePanel simplePanel;
    public void onModuleLoad() {
        RootPanel rootPanel = RootPanel.get();

        FlowPanel flowPanel = new FlowPanel();
        rootPanel.add(flowPanel, 10, 10);
        flowPanel.setSize("410px", "280px");

        Button setWidgetButton = new Button("Click");
        setWidgetButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                getSimplePanel().setWidget(new MainUI());
            }
        });
        flowPanel.add(setWidgetButton);

        simplePanel = new SimplePanel();
        flowPanel.add(simplePanel);
    }
    protected SimplePanel getSimplePanel() {
        return simplePanel;
    }
}

...and composite;

public class MainUI extends Composite {
    private VerticalPanel verticalPanel;

    int index;
    public MainUI() {

        FlowPanel flowPanel = new FlowPanel();
        initWidget(flowPanel);

        Button button = new Button("+");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {             
                getVerticalPanel().add(new Label(""+(++index)+": "+Math.random()));
            }
        });
        flowPanel.add(button);

        DecoratorPanel decoratorPanel = new DecoratorPanel();
        flowPanel.add(decoratorPanel);

        verticalPanel = new VerticalPanel();
        decoratorPanel.setWidget(verticalPanel);

    }

    protected VerticalPanel getVerticalPanel() {
        return verticalPanel;
    }
}

...and, as a result, to have "cached" ui state without regenerating it again with extracting strings from history tokens and re-instantiate objects or what so ever...

for example if I have this UI (see image) I am interested to have totally the same one after refresh button is pressed...

enter image description here

but I am not pretty sure which way should I look for? I haven't seen any gwt snippet in this direction; So I really need your advice what way should I dig in?

Any useful comment is appreciated

Thanks

P.S. GWT 2.3

Upvotes: 0

Views: 1341

Answers (1)

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

I think you miss to store the state into the URLs-Hashtag.

You can use GWTP (as suggested in the comments)

In fact you need to read the Hashtag in your onModuleLoad and restore your state.

This may work with getHash():

String state = Window.Location.getHash();
myRestoreStateFromTokenMethod(state);

update

Here are some snippets to create a push store.

List<String> states = [...]

public void onClick(){ states.add("newState");changeHash(states); }


public void changeHash(){
    String hash = states.get(0) + ";"
    for(other states) hash += states.get(i);
    // use a UrlBuilder to set the Hash
}

Documentation for UrlBuilder

Or you can try this: https://github.com/jbarop/gwt-pushstate

Upvotes: 1

Related Questions