01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.data;
09:
10: import com.google.gwt.user.client.rpc.AsyncCallback;
11:
12: /**
13: * <code>DataProxy</code> implementation that retrieves data using GWT RPC.
14: */
15: public abstract class RPCProxy implements DataProxy, AsyncCallback {
16:
17: protected DataReader reader;
18: protected DataCallback callback;
19: protected LoadConfig config;
20:
21: public void load(DataReader reader, LoadConfig config,
22: DataCallback cb) {
23: this .reader = reader;
24: this .config = config;
25: callback = cb;
26: loadInternal(this );
27: }
28:
29: public void onFailure(Throwable caught) {
30: LoadResult result = new LoadResult();
31: result.success = false;
32: callback.setResult(result);
33: }
34:
35: public void onSuccess(Object data) {
36: if (reader != null) {
37: LoadResult result = reader.read(data);
38: callback.setResult(result);
39: } else if (data instanceof LoadResult) {
40: callback.setResult((LoadResult) data);
41: } else if (data instanceof DataList) {
42: callback.setResult(new LoadResult((DataList) data));
43: } else if (data instanceof Model) {
44: callback.setResult(new LoadResult((Model) data));
45: } else {
46: LoadResult result = new LoadResult();
47: result.success = false;
48: callback.setResult(result);
49: }
50: }
51:
52: /**
53: * Subclasses should make RPC call using the load configuration.
54: *
55: * @param callback the callback to be used when making the rpc call.
56: */
57: protected abstract void loadInternal(AsyncCallback callback);
58: }
|