GWT的loading indicator(装载指示器)

GWT的loading indicator(装载指示器)
本文参考:http://groups.google.com/group/Google-Web-Toolkit/browse_thread /thread/7ed0d179b89fa062/aaeb5391a760adef?lnk=gst&q=indicator+AsyncCallback&rnum=1#aaeb5391a760adef

大部分代码也是参考文中的。

Java代码
  1. package com.javaeye.dengyin2000.truck.client.server;  
  2.   
  3. import com.google.gwt.user.client.Window;  
  4. import com.google.gwt.user.client.rpc.AsyncCallback;  
  5.   
  6. public abstract class AsyncCallbackWithStatus implements AsyncCallback {  
  7.     private static LoadingIndicator loadingIndicator;  
  8.   
  9.     public static final void setLoadingIndicator(LoadingIndicator indicator) {  
  10.         loadingIndicator = indicator;  
  11.     }  
  12.   
  13.     public AsyncCallbackWithStatus() {  
  14.         setLoading(true);  
  15.     }  
  16.   
  17.     public final void onSuccess(Object object) {  
  18.         setLoading(false);  
  19.         onSuccessInternal(object);  
  20.     }  
  21.   
  22.     public final void onFailure(Throwable throwable) {  
  23.         setLoading(false);  
  24.         onFailureInternal(throwable);  
  25.     }  
  26.   
  27.     protected abstract void onSuccessInternal(Object object);  
  28.   
  29.     protected void onFailureInternal(Throwable throwable){  
  30.         Window.alert("服务器没有响应,请稍后再试!");  
  31.     }  
  32.   
  33.     private void setLoading(boolean isLoading) {  
  34.         if (null != loadingIndicator) {  
  35.             if (isLoading) {  
  36.                 loadingIndicator.show();  
  37.             } else {  
  38.                 loadingIndicator.hide();  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     interface LoadingIndicator {  
  44.         void show();  
  45.   
  46.         void hide();  
  47.     }  
  48.   
  49. }  
package com.javaeye.dengyin2000.truck.client.server;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
public abstract class AsyncCallbackWithStatus implements AsyncCallback {
private static LoadingIndicator loadingIndicator;
public static final void setLoadingIndicator(LoadingIndicator indicator) {
loadingIndicator = indicator;
}
public AsyncCallbackWithStatus() {
setLoading(true);
}
public final void onSuccess(Object object) {
setLoading(false);
onSuccessInternal(object);
}
public final void onFailure(Throwable throwable) {
setLoading(false);
onFailureInternal(throwable);
}
protected abstract void onSuccessInternal(Object object);
protected void onFailureInternal(Throwable throwable){
Window.alert("服务器没有响应,请稍后再试!");
}
private void setLoading(boolean isLoading) {
if (null != loadingIndicator) {
if (isLoading) {
loadingIndicator.show();
} else {
loadingIndicator.hide();
}
}
}
interface LoadingIndicator {
void show();
void hide();
}
}


Java代码
  1. package com.javaeye.dengyin2000.truck.client.server;  
  2.   
  3. import com.google.gwt.user.client.Window;  
  4. import com.google.gwt.user.client.WindowResizeListener;  
  5. import com.google.gwt.user.client.ui.HTML;  
  6. import com.google.gwt.user.client.ui.PopupPanel;  
  7. import com.javaeye.dengyin2000.truck.client.server.AsyncCallbackWithStatus.LoadingIndicator;  
  8.   
  9. public class LoadingStatus implements LoadingIndicator, WindowResizeListener {  
  10.     private PopupPanel popup;  
  11.   
  12.     public LoadingStatus() {  
  13.         popup = new PopupPanel();  
  14.         popup.add(new HTML("<div style='background-color: red;'> Loading, please wait...</div>"));  
  15.         placeIndicator();  
  16.         Window.addWindowResizeListener(this);  
  17.         // set the style name for greater control over the  
  18.     }  
  19.   
  20.     public void show() {  
  21.         popup.show();   
  22.     }  
  23.   
  24.     public void hide() {  
  25.         popup.hide();  
  26.     }  
  27.   
  28.     private void placeIndicator(){  
  29.         popup.setPopupPosition(Window.getClientWidth() - 120, 10);  
  30.     }  
  31.       
  32.     public void onWindowResized(int width, int height) {  
  33.          placeIndicator();  
  34.     }   
  35.   
  36. }  
package com.javaeye.dengyin2000.truck.client.server;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.javaeye.dengyin2000.truck.client.server.AsyncCallbackWithStatus.LoadingIndicator;
public class LoadingStatus implements LoadingIndicator, WindowResizeListener {
private PopupPanel popup;
public LoadingStatus() {
popup = new PopupPanel();
popup.add(new HTML("<div style='background-color: red;'> Loading, please wait...</div>"));
placeIndicator();
Window.addWindowResizeListener(this);
// set the style name for greater control over the
}
public void show() {
popup.show();
}
public void hide() {
popup.hide();
}
private void placeIndicator(){
popup.setPopupPosition(Window.getClientWidth() - 120, 10);
}
public void onWindowResized(int width, int height) {
placeIndicator();
}
}


在你的EntryPoint中装配好你的indicator

Java代码
  1. private void prepareLoadingIndicator(){  
  2.     LoadingStatus status = new LoadingStatus();  
  3.     AsyncCallbackWithStatus.setLoadingIndicator(status);  
  4. }  
	private void prepareLoadingIndicator(){
LoadingStatus status = new LoadingStatus();
AsyncCallbackWithStatus.setLoadingIndicator(status);
}


这样调用。

Java代码
  1. private void populateAndReadEnterprisesInfo(){  
  2.     GWTRPCServiceLocator.getUserService().getEnterprisesInfo(new AsyncCallbackWithStatus(){  
  3.   
  4.         protected void onSuccessInternal(Object object) {  
  5.             EnterprisesInfoGTO gto = (EnterprisesInfoGTO) object;  
  6.             enterpriseName.setText(gto.getEnterprisesName());  
  7.             contactName.setText(gto.getContactName());  
  8.             contactTel.setText(gto.getContactTel());  
  9.             contactFax.setText(gto.getContactFax());  
  10.             contactAddress.setText(gto.getContactAddress());  
  11.             zipCode.setText(gto.getZipCode());  
  12.             email.setText(gto.getEmail());  
  13.             siteAddress.setText(gto.getSiteAddress());  
  14.         }  
  15.           
  16.     });  

你可能感兴趣的:(GWT的loading indicator(装载指示器))