I've started using timers combined with a state/transition matrix.
I've offered a simple example below.  The basic idea is to keep a
'state' variable with the current state in the program logic.  When
you initiate a asynchronous call, you move the state to "LOADING".
When the call is complete, you move the state to "RUNNING".  You can
easily chain requests (as in the example) by pushing the state from
"LOADING_WS1_DATA" to "DONE_LOADING_WS1_DATA" to "LOADING_WS2_DATA" to
"DONE_LOADING_WS2_DATA" finally back to "RUNNING".  Example follows:

package com.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GWTProcess implements EntryPoint
{
    private static final int ERROR = -2;
    private static final int INIT = -1;
    private static final int RUNNING = 0;
    private static final int BUILD_GUI = 100;
    private static final int START_LOADING_DATA = 200;
    private static final int LOADING_WS1_DATA = 300;
    private static final int DONE_LOADING_WS1_DATA = 301;
    private static final int LOADING_WS2_DATA = 400;
    private static final int DONE_LOADING_WS2_DATA = 401;
    private static final int DISPLAY_DATA = 500;

    int state = INIT;
    String result1 = "";
    String result2 = "";


    public void onModuleLoad()
    {
        Timer t = new Timer()
        {
            public void run()
            {
                if (state == INIT)
                {
                    initialize();
                    state = BUILD_GUI;
                }
                else if (state == BUILD_GUI)
                {
                    buildGui();
                    state = RUNNING;
                }
                else if (state == RUNNING)
                {
                    // do nothing special
                }
                else if (state == START_LOADING_DATA)
                {
                    fireWebServiceOne();
                    state = LOADING_WS1_DATA;
                }
                else if (state == LOADING_WS1_DATA)
                {
                    // wait patiently
                }
                else if (state == DONE_LOADING_WS1_DATA)
                {
                    state = LOADING_WS2_DATA;
                }
                else if (state == LOADING_WS2_DATA)
                {
                    fireWebServiceTwo();
                    state = LOADING_WS2_DATA;
                }
                else if (state == LOADING_WS2_DATA)
                {
                    // wait patiently
                }
                else if (state == DONE_LOADING_WS2_DATA)
                {
                    state = DISPLAY_DATA;
                }
                else if (state == DISPLAY_DATA)
                {
                    displayData();
                    state = RUNNING;
                } else if {state == ERROR) {
                    Window.alert("ERROR READING WEBSERVICE");
                    state = RUNNING;
                }
            }
        };

        // Schedule the timer to every 100 milliseconds.
        t.scheduleRepeating(100);
    }


    private void initialize()
    {
        // nothing to do
    }


    private void buildGui()
    {
        Button b = new Button("Click me", new ClickListener()
        {
            public void onClick(Widget sender)
            {
                Window.alert("start data load");
                state = START_LOADING_DATA;
            }
        });

        RootPanel.get().add(b);
    }

    private void displayData() {
        Window.alert(result1+":"+result2);
    }
    private void fireWebServiceOne()
    {
        WebServiceOneAsync webservice = GWT.create
(WebServiceOne.class);

        AsyncCallback<String> callback = new AsyncCallback<String>()
        {
            public void onFailure(Throwable caught)
            {
                state = ERROR;
            }

            public void onSuccess(String results)
            {
                result1 = results;
                state = DONE_LOADING_WS1_DATA;
            }
        };
        webservice.getData(callback);
    }
    private void fireWebServiceTwo()
    {
        WebServiceOneAsync webservice = GWT.create
(WebServiceOne.class);

        AsyncCallback<String> callback = new AsyncCallback<String>()
        {
            public void onFailure(Throwable caught)
            {
                state = ERROR;
            }

            public void onSuccess(String results)
            {
                result2 = results;
                state = DONE_LOADING_WS2_DATA;
            }
        };
        webservice.getData(callback);
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to