Well, what I described and linked to at stackoverflow does exactly what coderinabstract explains for the gwtp mechanism.
Here's my code. Maybe it's clearer then.
Event:
import com.google.gwt.event.shared.
EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
/**
* Event fired before and after Data is fetched.
*/
public class ShowCallProgressEvent extends GwtEvent<ShowCallProgressEvent.ShowCallProgressEventHandler> {
public interface ShowCallProgressEventHandler extends EventHandler {
void onShowCallProgress(ShowCallProgressEvent event);
}
private static final Type<ShowCallProgressEventHandler> TYPE = new Type<ShowCallProgressEventHandler>();
public static void fire(HasHandlers source, boolean inProgress) {
if (TYPE != null) {
source.fireEvent(new ShowCallProgressEvent(inProgress));
}
}
public static Type<ShowCallProgressEventHandler> getType() {
return TYPE;
}
private boolean inProgress;
public ShowCallProgressEvent() {
}
public boolean getInProgress() {
return inProgress;
}
public void setInProgress(boolean inProgress) {
this.inProgress = inProgress;
}
public ShowCallProgressEvent(boolean inProgress) {
this.inProgress = inProgress;
}
@Override
public Type<ShowCallProgressEventHandler> getAssociatedType() {
return getType();
}
@Override
protected void dispatch(ShowCallProgressEventHandler handler) {
handler.onShowCallProgress(this);
}
}
CustomRequestTransport:
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.requestfactory.client.DefaultRequestTransport;
import com.google.gwt.user.client.Window;
import org.xxx.wpm.client.event.ShowCallProgressEvent;
/**
* Custom RequestTransport fireing data fetching indication events.
*/
public class WPMRequestTransport extends DefaultRequestTransport implements HasHandlers {
private final EventBus eventBus;
public WPMRequestTransport(EventBus eventBus) {
this.eventBus = eventBus;
}
@Override
protected RequestCallback createRequestCallback(TransportReceiver receiver) {
final RequestCallback superCallback = super.createRequestCallback(receiver);
ShowCallProgressEvent.fire(this, true); //true -> show loading
return new RequestCallback() {
public void onResponseReceived(Request request, Response response) {
ShowCallProgressEvent.fire(WPMRequestTransport.this, false); //false -> hide loading
superCallback.onResponseReceived(request, response);
}
public void onError(Request request, Throwable exception) {
superCallback.onError(request, exception);
ShowCallProgressEvent.fire(WPMRequestTransport.this, false);//false -> hide loading
}
};
}
@Override
public void fireEvent(GwtEvent<?> gwtEvent) {
eventBus.fireEvent(gwtEvent);
}
}
EntryPoint (WPM as the name of the app):
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtplatform.mvp.client.DelayedBindRegistry;
import org.xxx.shared.WPMRequestFactory;
import org.xxx.wpm.client.gin.WPMGinjector;
import org.xxx.wpm.client.util.WPMRequestTransport;
/**
* Entry point classes define <code>onModuleLoad()</code>
*/
public class WPM implements EntryPoint {
public final WPMGinjector ginjector = GWT.create(WPMGinjector.class);
public void onModuleLoad() {
DelayedBindRegistry.bind(ginjector);
EventBus eventBus = ginjector.getEventBus();
WPMRequestFactory requestFactory = ginjector.wpmRequestFactory();
WPMRequestTransport requestTransport = new WPMRequestTransport(eventBus);
requestFactory.initialize(eventBus, requestTransport);
............ etc
}
}
Everytime
you make a call with this RequestFactory the events before and after
the call are fired and you can react to them anywhere in your app and
show some loading indicator.