001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.data;
009:
010: import java.util.List;
011:
012: import net.mygwt.ui.client.Style;
013: import net.mygwt.ui.client.util.Observable;
014:
015: /**
016: * <code>DataLoaders</code> are used to retrieve and convert raw data. Data is
017: * retrieved using a <code>DataProxy</code> and optionally read and parsed
018: * using a <code>DataReader</code>.
019: *
020: * <dl>
021: * <dt><b>Events:</b></dt>
022: *
023: * <dd><b>BeforeLoad</b> : (loader, config)<br>
024: * <div>Fires before a load operation. Listeners can set the <code>doit</code>
025: * field to <code>false</code> to cancel the action.</div>
026: * <ul>
027: * <li>loader : this</li>
028: * <li>config : the load config</li>
029: * </ul>
030: * </dd>
031: *
032: * <dd><b>Load</b> : (loader, config, result)<br>
033: * <div>Fires after the button is selected.</div>
034: * <ul>
035: * <li>loader : this</li>
036: * <li>config : the load config</li>
037: * <li>result : the load result</li>
038: * </ul>
039: * </dd>
040: *
041: * <dd><b>LoadException</b> : (loader, config, result)<br>
042: * <div>Fires after the button is selected.</div>
043: * <ul>
044: * <li>loader : this</li>
045: * <li>config : the load config</li>
046: * <li>result : the load result</li>
047: * </ul>
048: * </dd>
049: * </dl>
050: *
051: * @see DataProxy
052: * @see DataReader
053: */
054: public class DataLoader extends Observable implements Loader {
055:
056: /**
057: * totalLength specifies the total number of records which may not equal the
058: * local elements when using paging.
059: */
060: public int totalLength;
061: private boolean remoteSort;
062:
063: private String sortField;
064: private int sortDir = Style.NONE;
065:
066: private Object data;
067: private LoadConfig lastConfig;
068: private DataProxy proxy;
069: private DataReader reader;
070:
071: /**
072: * Creates a new loader instance with the given proxy. A reader is not
073: * specified and will not be passed to the proxy at load time.
074: *
075: * @param proxy the data proxy
076: */
077: public DataLoader(DataProxy proxy) {
078: this .proxy = proxy;
079: }
080:
081: /**
082: * Creates a new loader with the given proxy and reader.
083: *
084: * @param proxy the data proxy
085: * @param reader the data reader
086: */
087: public DataLoader(DataProxy proxy, DataReader reader) {
088: this .proxy = proxy;
089: this .reader = reader;
090: }
091:
092: /**
093: * Returns the last data from the last load.
094: *
095: * @return the remote data
096: */
097: public Object getData() {
098: return data;
099: }
100:
101: /**
102: * Returns the elements from the last load as a list.
103: *
104: * @deprecated Use {@link #getData()}.
105: * @return the elements
106: */
107: public List getElements() {
108: if (data instanceof List) {
109: return (List) data;
110: }
111: return null;
112: }
113:
114: public LoadConfig getLastConfig() {
115: return lastConfig;
116: }
117:
118: public boolean getRemoteSort() {
119: return remoteSort;
120: }
121:
122: public int getSortDir() {
123: return sortDir;
124: }
125:
126: public String getSortField() {
127: return sortField;
128: }
129:
130: public int getTotalLength() {
131: return totalLength;
132: }
133:
134: /**
135: * Loads the data.
136: */
137: public void load() {
138: load(new LoadConfig());
139: }
140:
141: /**
142: * Loads the data using the specified start and limit parameters.
143: *
144: * @param start the start mark
145: * @param limit the limit mark
146: */
147: public void load(int start, int limit) {
148: LoadConfig config = new LoadConfig();
149: config.start = start;
150: config.limit = limit;
151: load(config);
152: }
153:
154: public void load(LoadConfig config) {
155: LoadEvent evt = new LoadEvent(this , config, null);
156: if (fireEvent(BeforeLoad, evt)) {
157: lastConfig = config;
158: lastConfig.sortField = sortField;
159: lastConfig.sortDir = sortDir;
160: proxy.load(reader, config, new DataCallback() {
161: public void setResult(LoadResult result) {
162: onReceivedResult(result);
163: }
164: });
165: }
166:
167: }
168:
169: /**
170: * Reloads using the last load config.
171: */
172: public void reload() {
173: if (lastConfig != null) {
174: load(lastConfig);
175: }
176: }
177:
178: /**
179: * Sets if sorting is handled by the server. Defult value is
180: * <code>false</code>.
181: *
182: * @param remoteSort <code>true</code> to enable server side sorting
183: */
184: public void setRemoteSort(boolean remoteSort) {
185: this .remoteSort = remoteSort;
186: }
187:
188: public void setSortDir(int sortDir) {
189: this .sortDir = sortDir;
190: }
191:
192: public void setSortField(String sortField) {
193: this .sortField = sortField;
194: }
195:
196: public void sort(String sortField, int sortDir) {
197: this .sortField = sortField;
198: this .sortDir = sortDir;
199: load(lastConfig);
200: }
201:
202: protected void onReceivedResult(LoadResult result) {
203: LoadEvent evt = new LoadEvent(this, lastConfig, result);
204: evt.result = result;
205: data = result.data;
206: if (result.success) {
207: totalLength = result.totalLength;
208: fireEvent(Load, evt);
209: } else {
210: fireEvent(LoadException, evt);
211: }
212: }
213: }
|