GWT的loading indicator(装载指示器)
本文参考:http://groups.google.com/group/Google-Web-Toolkit/browse_thread /thread/7ed0d179b89fa062/aaeb5391a760adef?lnk=gst&q=indicator+AsyncCallback&rnum=1#aaeb5391a760adef
大部分代码也是参考文中的。
在你的EntryPoint中装配好你的indicator
这样调用。
大部分代码也是参考文中的。
Java代码
- 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();
- }
- }
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代码
- 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();
- }
- }
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代码
- private void prepareLoadingIndicator(){
- LoadingStatus status = new LoadingStatus();
- AsyncCallbackWithStatus.setLoadingIndicator(status);
- }
private void prepareLoadingIndicator(){ LoadingStatus status = new LoadingStatus(); AsyncCallbackWithStatus.setLoadingIndicator(status); }
这样调用。
Java代码
- private void populateAndReadEnterprisesInfo(){
- GWTRPCServiceLocator.getUserService().getEnterprisesInfo(new AsyncCallbackWithStatus(){
- protected void onSuccessInternal(Object object) {
- EnterprisesInfoGTO gto = (EnterprisesInfoGTO) object;
- enterpriseName.setText(gto.getEnterprisesName());
- contactName.setText(gto.getContactName());
- contactTel.setText(gto.getContactTel());
- contactFax.setText(gto.getContactFax());
- contactAddress.setText(gto.getContactAddress());
- zipCode.setText(gto.getZipCode());
- email.setText(gto.getEmail());
- siteAddress.setText(gto.getSiteAddress());
- }
- });
- }