001: /*******************************************************************************
002: * Copyright (c) 2007 MyGWT.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *******************************************************************************/package net.mygwt.ui.client.viewer;
008:
009: import net.mygwt.ui.client.Style;
010: import net.mygwt.ui.client.data.DataCallback;
011: import net.mygwt.ui.client.data.LoadConfig;
012: import net.mygwt.ui.client.data.LoadEvent;
013: import net.mygwt.ui.client.data.LoadResult;
014: import net.mygwt.ui.client.data.Loader;
015: import net.mygwt.ui.client.util.Observable;
016:
017: /**
018: * A <code>IStructuredContentProvider</code> implementation that supports the
019: * remote loading of data and paging.
020: */
021: public abstract class RemoteContentProvider extends Observable
022: implements Loader, IStructuredContentProvider {
023:
024: /**
025: * The owning viewer.
026: */
027: protected Viewer viewer;
028:
029: /**
030: * The last load config.
031: */
032: protected LoadConfig lastConfig;
033:
034: private String sortField;
035: private int sortDir = Style.NONE;
036: private boolean remoteSort;
037: private Object data;
038: private int totalLength;
039:
040: /**
041: * Returns the last data from the last load.
042: *
043: * @return the current remote data
044: */
045: public Object getData() {
046: return data;
047: }
048:
049: /**
050: * Subclasses must implement and return the remote data based on the given
051: * load config. The viewer's setInput method will be called, passing the data
052: * being returned from the callback.
053: *
054: * @param config the load config
055: * @param callback the callback
056: */
057: public abstract void getData(LoadConfig config,
058: DataCallback callback);
059:
060: /* (non-Javadoc)
061: * @see net.mygwt.ui.client.data.Loader#getLastConfig()
062: */
063: public LoadConfig getLastConfig() {
064: return lastConfig;
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see net.mygwt.ui.client.data.Loader#getRemoteSort()
071: */
072: public boolean getRemoteSort() {
073: return remoteSort;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see net.mygwt.ui.client.data.Loader#getSortDir()
080: */
081: public int getSortDir() {
082: return sortDir;
083: }
084:
085: /**
086: * Sets the sort field.
087: *
088: * @return the sort field
089: */
090: public String getSortField() {
091: return sortField;
092: }
093:
094: /**
095: * Returns the total number of records which may not equal the local elements
096: * when using paging.
097: *
098: * @return the total length
099: */
100: public int getTotalLength() {
101: return totalLength;
102: }
103:
104: public void inputChanged(Viewer viewer, Object oldInput,
105: Object newInput) {
106: this .viewer = viewer;
107: }
108:
109: /**
110: * Loads the data.
111: */
112: public void load() {
113: load(new LoadConfig());
114: }
115:
116: /**
117: * Loads the data using the specified start and limit parameters.
118: *
119: * @param start the start mark
120: * @param limit the limit mark
121: */
122: public void load(int start, int limit) {
123: LoadConfig config = new LoadConfig();
124: config.start = start;
125: config.limit = limit;
126: load(config);
127: }
128:
129: /**
130: * Loads the data using the specified configuation.
131: *
132: * @param config the configuration
133: */
134: public void load(LoadConfig config) {
135: LoadEvent evt = new LoadEvent(this , config, null);
136: if (fireEvent(BeforeLoad, evt)) {
137: lastConfig = config;
138: lastConfig.sortField = sortField;
139: lastConfig.sortDir = sortDir;
140: getData(lastConfig, new DataCallback() {
141: public void setResult(LoadResult result) {
142: onReceiveResults(result);
143: }
144: });
145: }
146: }
147:
148: /**
149: * Reloads using the last load config.
150: */
151: public void reload() {
152: if (lastConfig != null) {
153: load(lastConfig);
154: }
155: }
156:
157: /**
158: * Sets whether sorting should be done server side.
159: *
160: * @param remoteSort <code>true</code> to enable remote sorting
161: */
162: public void setRemoteSort(boolean remoteSort) {
163: this .remoteSort = remoteSort;
164: }
165:
166: /**
167: * Sets the sort direction.
168: *
169: * @param sortDir the sort direction
170: */
171: public void setSortDir(int sortDir) {
172: this .sortDir = sortDir;
173: }
174:
175: /**
176: * Sets the current sort field.
177: *
178: * @param sortField the sort field
179: */
180: public void setSortField(String sortField) {
181: this .sortField = sortField;
182: }
183:
184: /**
185: * Sorts the elements.
186: *
187: * @param sortField the sort field
188: * @param sortDir the sort direction
189: */
190: public void sort(String sortField, int sortDir) {
191: this .sortField = sortField;
192: this .sortDir = sortDir;
193: load(lastConfig);
194: }
195:
196: protected void onReceiveResults(LoadResult result) {
197: LoadEvent evt = new LoadEvent(this, lastConfig, result);
198: data = result.getData();
199: if (result.success) {
200: totalLength = result.totalLength;
201: fireEvent(Load, evt);
202: } else {
203: fireEvent(LoadException, evt);
204: }
205: }
206:
207: }
|