GWT Loading....

JimmyblyLee 2011-09-06

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.PopupPanel;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;


/**
 * 
 * @author ldsjdy
 *
 */
public class Loading extends PopupPanel
{
  private static Loading me;

  private List workingObjects = new ArrayList();
  private boolean shown;

  private Loading()
  {
//    super(false, true);
    
    Img img = new Img("loading_smartgwt.gif",30,30);
    Grid grid = new Grid(1,2);	
    grid.setWidget(0, 0, img);
    
    Label label = new Label("Loading........");
    label.setHeight(30);
    grid.setWidget(0, 1, label);
//    grid.setWidget(0, 1, label);
    setWidget(grid);
  }

  public static Loading get()
  {
    if (me == null)
    {
      me = new Loading();
    }

    return me;
  }

  public AsyncCallback startWorking(AsyncCallback callback)
  {
    AsyncCallbackWrapper wrapper = new AsyncCallbackWrapper(callback);
    this.workingObjects.add(wrapper);

    if (!this.shown)
    {
      this.shown = true;
      this.center();
    }

    return wrapper;
  }

  private void endWorking(AsyncCallbackWrapper wrapper)
  {
    this.workingObjects.remove(wrapper);

    if (this.shown && (this.workingObjects.size() <= 0))
    {
      this.shown = false;
      this.setModal(false);
      this.hide();
    }
  }

  private class AsyncCallbackWrapper implements AsyncCallback
  {
    private AsyncCallback callback;

    private AsyncCallbackWrapper(AsyncCallback callback)
    {
      this.callback = callback;
    }

    public void onFailure(Throwable t)
    {
      Loading.get().endWorking(this);
      this.callback.onFailure(t);
    }

    public void onSuccess(Object obj)
    {
      this.callback.onSuccess(obj);
      Loading.get().endWorking(this);
    }
  }
}

相关推荐

nicepainkiller / 0评论 2018-04-28