|
@@ -1,48 +1,36 @@ |
1 |
1 |
|
package com.temp.client.foundation.widget.table; |
2 |
2 |
|
|
3 |
|
- |
import java.util.ArrayList; |
4 |
|
- |
import java.util.Collections; |
5 |
|
- |
import java.util.Comparator; |
6 |
|
- |
import java.util.HashMap; |
7 |
|
- |
import java.util.LinkedHashMap; |
8 |
|
- |
import java.util.LinkedList; |
9 |
|
- |
import java.util.List; |
10 |
|
- |
import java.util.Map; |
11 |
|
- |
|
12 |
|
- |
import com.temp.shared.utils.ComparatorSource; |
13 |
|
- |
import com.temp.shared.utils.StringUtils; |
14 |
|
- |
import com.temp.shared.LoadingProgressListener; |
15 |
|
- |
import com.temp.client.foundation.widget.table.CustomDataTable.SortDirection; |
16 |
|
- |
import com.temp.client.foundation.widget.table.CustomDataTable.DataTableUpdater; |
17 |
|
- |
|
18 |
|
- |
import com.google.gwt.dom.client.Style.Unit; |
19 |
|
- |
import com.google.gwt.event.dom.client.ClickEvent; |
20 |
|
- |
import com.google.gwt.event.dom.client.ClickHandler; |
21 |
|
- |
import com.google.gwt.user.cellview.client.Column; |
22 |
|
- |
import com.google.gwt.user.client.ui.Composite; |
23 |
|
- |
import com.google.gwt.user.client.ui.Widget; |
24 |
|
- |
import com.google.gwt.view.client.SelectionModel; |
|
3 |
+ |
import com.google.gwt.dom.client.Style.*; |
|
4 |
+ |
import com.google.gwt.event.dom.client.*; |
|
5 |
+ |
import com.google.gwt.user.cellview.client.*; |
|
6 |
+ |
import com.google.gwt.user.client.ui.*; |
|
7 |
+ |
import com.google.gwt.view.client.*; |
|
8 |
+ |
import com.temp.client.foundation.widget.table.CustomDataTable.*; |
|
9 |
+ |
import com.temp.shared.*; |
|
10 |
+ |
import com.temp.shared.utils.*; |
|
11 |
+ |
|
|
12 |
+ |
import java.util.*; |
25 |
13 |
|
|
26 |
14 |
|
/** |
|
15 |
+ |
* @param <R> the row type |
27 |
16 |
|
* |
28 |
17 |
|
* @author paule |
29 |
|
- |
* To use CachedDataTable: |
30 |
|
- |
* <pre> |
31 |
|
- |
* 1) instantiate: |
32 |
|
- |
* table = new CachedDataTable<RowType>(...) |
|
18 |
+ |
* To use CachedDataTable: |
|
19 |
+ |
* <pre> |
|
20 |
+ |
* 1) instantiate: |
|
21 |
+ |
* table = new CachedDataTable<RowType>(...) |
33 |
22 |
|
* |
34 |
|
- |
* 2) add Columns: |
35 |
|
- |
* table.addColumn(...) |
|
23 |
+ |
* 2) add Columns: |
|
24 |
+ |
* table.addColumn(...) |
36 |
25 |
|
* |
37 |
|
- |
* 3) set data of type List<RowType> |
38 |
|
- |
* table.setData(...) |
|
26 |
+ |
* 3) set data of type List<RowType> |
|
27 |
+ |
* table.setData(...) |
39 |
28 |
|
* |
40 |
|
- |
* And you're done! |
41 |
|
- |
*</pre> |
42 |
|
- |
* @param <R> the row type |
|
29 |
+ |
* And you're done! |
|
30 |
+ |
* </pre> |
43 |
31 |
|
*/ |
44 |
32 |
|
@SuppressWarnings("rawtypes") |
45 |
|
- |
public class CachedDataTable<R extends DataRow<F>,F> extends Composite implements LoadingProgressListener { |
|
33 |
+ |
public class CachedDataTable<R extends DataRow<F>, F> extends Composite implements LoadingProgressListener { |
46 |
34 |
|
public static final String DEFAULT_HEIGHT = "350px"; |
47 |
35 |
|
public static final String DEFAULT_WIDTH = "100%"; |
48 |
36 |
|
|
|
@@ -50,21 +38,32 @@ |
50 |
38 |
|
String sortByField; |
51 |
39 |
|
Comparator comparator; |
52 |
40 |
|
SortDirection sortDirection; |
53 |
|
- |
SortFieldComparatorDirection(String _sortByField, Comparator _comparator, SortDirection _sortDirection) { |
|
41 |
+ |
|
|
42 |
+ |
SortFieldComparatorDirection( String _sortByField, Comparator _comparator, SortDirection _sortDirection ) { |
54 |
43 |
|
sortByField = _sortByField; |
55 |
44 |
|
comparator = _comparator; |
56 |
45 |
|
sortDirection = _sortDirection; |
57 |
46 |
|
} |
58 |
47 |
|
|
59 |
|
- |
String getSortByField() { return sortByField; } |
60 |
|
- |
Comparator getComparator() { return comparator; } |
61 |
|
- |
SortDirection getSortDirection() { return sortDirection; } |
|
48 |
+ |
String getSortByField() { |
|
49 |
+ |
return sortByField; |
|
50 |
+ |
} |
|
51 |
+ |
|
|
52 |
+ |
Comparator getComparator() { |
|
53 |
+ |
return comparator; |
|
54 |
+ |
} |
62 |
55 |
|
|
63 |
|
- |
void setSortDirection(SortDirection _sortDirection) { sortDirection = _sortDirection; } |
|
56 |
+ |
SortDirection getSortDirection() { |
|
57 |
+ |
return sortDirection; |
|
58 |
+ |
} |
|
59 |
+ |
|
|
60 |
+ |
void setSortDirection( SortDirection _sortDirection ) { |
|
61 |
+ |
sortDirection = _sortDirection; |
|
62 |
+ |
} |
64 |
63 |
|
} |
65 |
64 |
|
|
66 |
65 |
|
// table |
67 |
|
- |
CustomDataTable<R,F> dataTable; |
|
66 |
+ |
CustomDataTable<R, F> dataTable; |
68 |
67 |
|
Map<String, Column<R, ?>> fieldColumns = new HashMap<String, Column<R, ?>>(); |
69 |
68 |
|
Map<String, Comparator> fieldComparators = new HashMap<String, Comparator>(); |
70 |
69 |
|
|
|
@@ -75,70 +74,71 @@ |
75 |
74 |
|
LinkedList<SortFieldComparatorDirection> sortQueue = new LinkedList<SortFieldComparatorDirection>(); |
76 |
75 |
|
|
77 |
76 |
|
public CachedDataTable() { |
78 |
|
- |
this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, null, false, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT); |
|
77 |
+ |
this( CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, null, false, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT ); |
79 |
78 |
|
} |
80 |
79 |
|
|
81 |
|
- |
public CachedDataTable(String title) { |
82 |
|
- |
this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT); |
|
80 |
+ |
public CachedDataTable( String title ) { |
|
81 |
+ |
this( CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT ); |
83 |
82 |
|
} |
84 |
83 |
|
|
85 |
|
- |
public CachedDataTable(String title, ClickHandler downloadCSVButtonClickHandler) { |
86 |
|
- |
this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, downloadCSVButtonClickHandler, DEFAULT_WIDTH, DEFAULT_HEIGHT); |
|
84 |
+ |
public CachedDataTable( String title, ClickHandler downloadCSVButtonClickHandler ) { |
|
85 |
+ |
this( CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, downloadCSVButtonClickHandler, DEFAULT_WIDTH, DEFAULT_HEIGHT ); |
87 |
86 |
|
} |
88 |
87 |
|
|
89 |
|
- |
public CachedDataTable(int pageSize, boolean supportPaging, String width, String height) { |
90 |
|
- |
this( pageSize, supportPaging, null, false, null, null, null, width, height); |
|
88 |
+ |
public CachedDataTable( int pageSize, boolean supportPaging, String width, String height ) { |
|
89 |
+ |
this( pageSize, supportPaging, null, false, null, null, null, width, height ); |
91 |
90 |
|
} |
92 |
91 |
|
|
93 |
|
- |
public CachedDataTable(String title, int pageSize, boolean supportPaging, String width, String height) { |
94 |
|
- |
this( pageSize, supportPaging, title, true, null, null, null, width, height); |
|
92 |
+ |
public CachedDataTable( String title, int pageSize, boolean supportPaging, String width, String height ) { |
|
93 |
+ |
this( pageSize, supportPaging, title, true, null, null, null, width, height ); |
95 |
94 |
|
} |
96 |
95 |
|
|
97 |
|
- |
public CachedDataTable(String title, int pageSize, boolean supportPaging, boolean includeRowNumber, String width, String height) { |
98 |
|
- |
this( pageSize, supportPaging, title, true, null, null, null, includeRowNumber, width, height); |
|
96 |
+ |
public CachedDataTable( String title, int pageSize, boolean supportPaging, boolean includeRowNumber, String width, String height ) { |
|
97 |
+ |
this( pageSize, supportPaging, title, true, null, null, null, includeRowNumber, width, height ); |
99 |
98 |
|
} |
100 |
99 |
|
|
101 |
|
- |
public CachedDataTable(int pageSize, boolean supportPaging, String title, boolean includeTitleBar, Widget topWidget, |
102 |
|
- |
Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, String width, String height) { |
103 |
|
- |
this(pageSize, supportPaging, title, includeTitleBar, topWidget, |
104 |
|
- |
bottomWidget, downloadCSVButtonClickHandler, true, width, height); |
|
100 |
+ |
public CachedDataTable( int pageSize, boolean supportPaging, String title, boolean includeTitleBar, Widget topWidget, |
|
101 |
+ |
Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, String width, String height ) { |
|
102 |
+ |
this( pageSize, supportPaging, title, includeTitleBar, topWidget, |
|
103 |
+ |
bottomWidget, downloadCSVButtonClickHandler, true, width, height ); |
105 |
104 |
|
} |
106 |
105 |
|
|
107 |
|
- |
private CachedDataTable(int pageSize, boolean supportPaging, String title, boolean includeTitleBar, Widget topWidget, |
108 |
|
- |
Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, boolean includeRowNumber, String width, String height) { |
|
106 |
+ |
private CachedDataTable( int pageSize, boolean supportPaging, String title, boolean includeTitleBar, Widget topWidget, |
|
107 |
+ |
Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, boolean includeRowNumber, String width, String height ) { |
109 |
108 |
|
|
110 |
109 |
|
boolean includeDownloadCSVButton = (downloadCSVButtonClickHandler != null); |
111 |
|
- |
dataTable = new CustomDataTable<R,F>( |
|
110 |
+ |
dataTable = new CustomDataTable<R, F>( |
112 |
111 |
|
pageSize, supportPaging, title, |
113 |
112 |
|
new DataTableUpdater() { |
114 |
113 |
|
public void updateDataTable() { |
115 |
114 |
|
String pageMarker = dataTable.getPageMarker(); |
116 |
115 |
|
List<R> dataList = new ArrayList<R>(); |
117 |
|
- |
for (List<R> pagedDataList : pageMarkerDataList.values()) { |
118 |
|
- |
dataList.addAll(pagedDataList); |
|
116 |
+ |
for ( List<R> pagedDataList : pageMarkerDataList.values() ) { |
|
117 |
+ |
dataList.addAll( pagedDataList ); |
119 |
118 |
|
} |
120 |
|
- |
sort(dataList); |
121 |
|
- |
refreshData(dataList); |
122 |
|
- |
List<R> dataListPage = pageMarkerDataList.get(pageMarker); |
123 |
|
- |
String nextPageMarker = nextPageMarkerMap.get(pageMarker); |
124 |
|
- |
dataTable.setData(dataListPage, nextPageMarker); |
|
119 |
+ |
sort( dataList ); |
|
120 |
+ |
refreshData( dataList ); |
|
121 |
+ |
List<R> dataListPage = pageMarkerDataList.get( pageMarker ); |
|
122 |
+ |
String nextPageMarker = nextPageMarkerMap.get( pageMarker ); |
|
123 |
+ |
dataTable.setData( dataListPage, nextPageMarker ); |
125 |
124 |
|
} |
126 |
|
- |
public void onDataTableDownloadCSVButtonClick(ClickEvent event) { |
127 |
|
- |
downloadCSVButtonClickHandler.onClick(event); |
|
125 |
+ |
|
|
126 |
+ |
public void onDataTableDownloadCSVButtonClick( ClickEvent event ) { |
|
127 |
+ |
downloadCSVButtonClickHandler.onClick( event ); |
128 |
128 |
|
} |
129 |
129 |
|
}, |
130 |
130 |
|
topWidget, bottomWidget, |
131 |
131 |
|
includeTitleBar, includeDownloadCSVButton, includeRowNumber, |
132 |
|
- |
width, height); |
133 |
|
- |
initWidget(dataTable); |
|
132 |
+ |
width, height ); |
|
133 |
+ |
initWidget( dataTable ); |
134 |
134 |
|
} |
135 |
135 |
|
|
136 |
|
- |
public void setTitle(String title) { |
137 |
|
- |
dataTable.setTitle(title); |
|
136 |
+ |
public void setTitle( String title ) { |
|
137 |
+ |
dataTable.setTitle( title ); |
138 |
138 |
|
} |
139 |
139 |
|
|
140 |
|
- |
public void setSelectionModel(SelectionModel<R> selectionModel) { |
141 |
|
- |
dataTable.setSelectionModel(selectionModel); |
|
140 |
+ |
public void setSelectionModel( SelectionModel<R> selectionModel ) { |
|
141 |
+ |
dataTable.setSelectionModel( selectionModel ); |
142 |
142 |
|
} |
143 |
143 |
|
|
144 |
144 |
|
public SelectionModel<? super R> getSelectionModel() { |
|
@@ -156,20 +156,19 @@ |
156 |
156 |
|
} |
157 |
157 |
|
|
158 |
158 |
|
@Override |
159 |
|
- |
public void loadingCompleted(boolean successfully) { |
160 |
|
- |
dataTable.loadingCompleted(successfully); |
|
159 |
+ |
public void loadingCompleted( boolean successfully ) { |
|
160 |
+ |
dataTable.loadingCompleted( successfully ); |
161 |
161 |
|
} |
162 |
162 |
|
|
163 |
163 |
|
/** |
164 |
164 |
|
* Adds a column |
165 |
165 |
|
* |
166 |
|
- |
* @param fields |
167 |
|
- |
* - the column / "field" definitions |
|
166 |
+ |
* @param fields - the column / "field" definitions |
168 |
167 |
|
*/ |
169 |
|
- |
public <FIELD extends DataRowField> void addColumns(FIELD... fields) { |
170 |
|
- |
if (fields != null) { |
171 |
|
- |
for (FIELD field : fields) { |
172 |
|
- |
addColumn(field); |
|
168 |
+ |
public <FIELD extends DataRowField> void addColumns( FIELD... fields ) { |
|
169 |
+ |
if ( fields != null ) { |
|
170 |
+ |
for ( FIELD field : fields ) { |
|
171 |
+ |
addColumn( field ); |
173 |
172 |
|
} |
174 |
173 |
|
} |
175 |
174 |
|
} |
|
@@ -177,173 +176,169 @@ |
177 |
176 |
|
/** |
178 |
177 |
|
* Adds a column |
179 |
178 |
|
* |
180 |
|
- |
* @param field |
181 |
|
- |
* - the column or "field" definition |
|
179 |
+ |
* @param field - the column or "field" definition |
182 |
180 |
|
*/ |
183 |
|
- |
public <FIELD extends DataRowField> void addColumn(FIELD field) { |
184 |
|
- |
if (show(field)) { |
185 |
|
- |
addComparator( dataTable.addColumn(field), field, getComparator( field ) ); |
|
181 |
+ |
public <FIELD extends DataRowField> void addColumn( FIELD field ) { |
|
182 |
+ |
if ( show( field ) ) { |
|
183 |
+ |
addComparator( dataTable.addColumn( field ), field, getComparator( field ) ); |
186 |
184 |
|
} |
187 |
185 |
|
} |
188 |
186 |
|
|
189 |
187 |
|
/** |
190 |
188 |
|
* Adds a column |
191 |
189 |
|
* |
192 |
|
- |
* @param column |
193 |
|
- |
* - the column |
194 |
|
- |
* @param field |
195 |
|
- |
* - the column definition "field" |
|
190 |
+ |
* @param column - the column |
|
191 |
+ |
* @param field - the column definition "field" |
196 |
192 |
|
*/ |
197 |
|
- |
public <FIELD extends DataRowField> void addColumn(Column<R, ?> column, FIELD field) { |
198 |
|
- |
if (show(field)) { |
199 |
|
- |
addComparator(dataTable.addColumn(column, field), field, getComparator(field)); |
|
193 |
+ |
public <FIELD extends DataRowField> void addColumn( Column<R, ?> column, FIELD field ) { |
|
194 |
+ |
if ( show( field ) ) { |
|
195 |
+ |
addComparator( dataTable.addColumn( column, field ), field, getComparator( field ) ); |
200 |
196 |
|
} |
201 |
197 |
|
} |
202 |
198 |
|
|
203 |
199 |
|
/** |
204 |
200 |
|
* Adds a column |
205 |
201 |
|
* |
206 |
|
- |
* @param field |
207 |
|
- |
* - the column or "field" definition |
|
202 |
+ |
* @param field - the column or "field" definition |
208 |
203 |
|
* @param comparator - the comparator |
209 |
204 |
|
*/ |
210 |
|
- |
public <FIELD extends DataRowField> void addColumn(FIELD field, Comparator comparator) { |
211 |
|
- |
if (show(field)) { |
212 |
|
- |
addComparator(dataTable.addColumn(field), field, comparator); |
|
205 |
+ |
public <FIELD extends DataRowField> void addColumn( FIELD field, Comparator comparator ) { |
|
206 |
+ |
if ( show( field ) ) { |
|
207 |
+ |
addComparator( dataTable.addColumn( field ), field, comparator ); |
213 |
208 |
|
} |
214 |
209 |
|
} |
215 |
210 |
|
|
216 |
211 |
|
/** |
217 |
212 |
|
* Adds a column |
218 |
213 |
|
* |
219 |
|
- |
* @param column |
220 |
|
- |
* - the column |
221 |
|
- |
* @param field |
222 |
|
- |
* - the column definition "field" |
|
214 |
+ |
* @param column - the column |
|
215 |
+ |
* @param field - the column definition "field" |
223 |
216 |
|
* @param comparator - the comparator |
224 |
217 |
|
*/ |
225 |
|
- |
public <FIELD extends DataRowField> void addColumn(Column<R, ?> column, FIELD field, Comparator comparator) { |
226 |
|
- |
if (show(field)) { |
227 |
|
- |
addComparator(dataTable.addColumn(column, field), field, comparator); |
|
218 |
+ |
public <FIELD extends DataRowField> void addColumn( Column<R, ?> column, FIELD field, Comparator comparator ) { |
|
219 |
+ |
if ( show( field ) ) { |
|
220 |
+ |
addComparator( dataTable.addColumn( column, field ), field, comparator ); |
228 |
221 |
|
} |
229 |
222 |
|
} |
230 |
223 |
|
|
231 |
|
- |
private <FIELD extends DataRowField> boolean show(FIELD field) { |
232 |
|
- |
if (field == null) { |
|
224 |
+ |
private <FIELD extends DataRowField> boolean show( FIELD field ) { |
|
225 |
+ |
if ( field == null ) { |
233 |
226 |
|
return false; |
234 |
227 |
|
} |
235 |
|
- |
return (field instanceof ColumnHidable) ? !((ColumnHidable)field).hideColumn() : true; |
|
228 |
+ |
return (field instanceof ColumnHidable) ? !((ColumnHidable) field).hideColumn() : true; |
236 |
229 |
|
} |
237 |
230 |
|
|
238 |
231 |
|
/** |
239 |
232 |
|
* Adds a column with a comparator. |
240 |
|
- |
* @param column - the column |
|
233 |
+ |
* |
|
234 |
+ |
* @param column - the column |
241 |
235 |
|
* @param columnHeader - the column header that appears at the top of the table |
242 |
|
- |
* @param fieldName - must be unique among columns (used as key for columns for sorting) |
243 |
|
- |
* @param comparator - the comparator |
|
236 |
+ |
* @param fieldName - must be unique among columns (used as key for columns for sorting) |
|
237 |
+ |
* @param comparator - the comparator |
244 |
238 |
|
*/ |
245 |
|
- |
public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator) { |
246 |
|
- |
dataTable.addColumn(column, columnHeader, fieldName); |
247 |
|
- |
addComparator(column, fieldName, comparator); |
|
239 |
+ |
public void addColumn( Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator ) { |
|
240 |
+ |
dataTable.addColumn( column, columnHeader, fieldName ); |
|
241 |
+ |
addComparator( column, fieldName, comparator ); |
248 |
242 |
|
} |
249 |
243 |
|
|
250 |
244 |
|
/** |
251 |
245 |
|
* Adds a column |
252 |
|
- |
* @param column - the column |
|
246 |
+ |
* |
|
247 |
+ |
* @param column - the column |
253 |
248 |
|
* @param columnHeader - the column header to be displayed |
254 |
249 |
|
*/ |
255 |
|
- |
public void addColumn(Column<R, ?> column, String columnHeader, String fieldName) { |
256 |
|
- |
dataTable.addColumn(column, columnHeader, fieldName); |
|
250 |
+ |
public void addColumn( Column<R, ?> column, String columnHeader, String fieldName ) { |
|
251 |
+ |
dataTable.addColumn( column, columnHeader, fieldName ); |
257 |
252 |
|
} |
258 |
253 |
|
|
259 |
|
- |
public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, int width, Unit widthUnit) { |
260 |
|
- |
dataTable.addColumn(column, columnHeader, fieldName, width, widthUnit); |
|
254 |
+ |
public void addColumn( Column<R, ?> column, String columnHeader, String fieldName, int width, Unit widthUnit ) { |
|
255 |
+ |
dataTable.addColumn( column, columnHeader, fieldName, width, widthUnit ); |
261 |
256 |
|
} |
262 |
257 |
|
|
263 |
|
- |
public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator, int width, Unit widthUnit) { |
264 |
|
- |
dataTable.addColumn(column, columnHeader, fieldName, width, widthUnit); |
265 |
|
- |
addComparator(column, fieldName, comparator); |
|
258 |
+ |
public void addColumn( Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator, int width, Unit widthUnit ) { |
|
259 |
+ |
dataTable.addColumn( column, columnHeader, fieldName, width, widthUnit ); |
|
260 |
+ |
addComparator( column, fieldName, comparator ); |
266 |
261 |
|
} |
267 |
262 |
|
|
268 |
|
- |
private <FIELD extends DataRowField> Comparator<?> getComparator(FIELD field) { |
269 |
|
- |
if (field instanceof Comparator) { |
270 |
|
- |
return (Comparator)field; |
|
263 |
+ |
private <FIELD extends DataRowField> Comparator<?> getComparator( FIELD field ) { |
|
264 |
+ |
if ( field instanceof Comparator ) { |
|
265 |
+ |
return (Comparator) field; |
271 |
266 |
|
} |
272 |
|
- |
if (field instanceof ComparatorSource) { |
273 |
|
- |
return ((ComparatorSource)field).getComparator(); |
|
267 |
+ |
if ( field instanceof ComparatorSource ) { |
|
268 |
+ |
return ((ComparatorSource) field).getComparator(); |
274 |
269 |
|
} |
275 |
270 |
|
return null; |
276 |
271 |
|
} |
277 |
272 |
|
|
278 |
|
- |
private <FIELD extends DataRowField> void addComparator(Column<R, ?> column, FIELD field, Comparator comparator) { |
279 |
|
- |
addComparator(column, field.name(), comparator); |
|
273 |
+ |
private <FIELD extends DataRowField> void addComparator( Column<R, ?> column, FIELD field, Comparator comparator ) { |
|
274 |
+ |
addComparator( column, field.name(), comparator ); |
280 |
275 |
|
} |
281 |
276 |
|
|
282 |
|
- |
private void addComparator(Column<R, ?> column, String fieldName, Comparator comparator) { |
283 |
|
- |
if (comparator != null) { |
284 |
|
- |
fieldComparators.put(fieldName, comparator); |
285 |
|
- |
fieldColumns.put(fieldName, column); |
|
277 |
+ |
private void addComparator( Column<R, ?> column, String fieldName, Comparator comparator ) { |
|
278 |
+ |
if ( comparator != null ) { |
|
279 |
+ |
fieldComparators.put( fieldName, comparator ); |
|
280 |
+ |
fieldColumns.put( fieldName, column ); |
286 |
281 |
|
} |
287 |
282 |
|
} |
288 |
283 |
|
|
289 |
|
- |
private void addToSortQueue(String sortByField, SortDirection sortDirection) { |
290 |
|
- |
if (StringUtils.isBlank(sortByField)) { |
|
284 |
+ |
private void addToSortQueue( String sortByField, SortDirection sortDirection ) { |
|
285 |
+ |
if ( StringUtils.isBlank( sortByField ) ) { |
291 |
286 |
|
return; |
292 |
287 |
|
} |
293 |
|
- |
Comparator comparator = fieldComparators.get(sortByField); |
294 |
|
- |
if (comparator == null) { |
|
288 |
+ |
Comparator comparator = fieldComparators.get( sortByField ); |
|
289 |
+ |
if ( comparator == null ) { |
295 |
290 |
|
return; |
296 |
291 |
|
} |
297 |
292 |
|
SortFieldComparatorDirection sortParams = !sortQueue.isEmpty() ? sortQueue.getLast() : null; |
298 |
|
- |
if (sortParams != null && sortParams.getSortByField().equals(sortByField)) { |
299 |
|
- |
sortParams.setSortDirection(sortDirection); |
|
293 |
+ |
if ( sortParams != null && sortParams.getSortByField().equals( sortByField ) ) { |
|
294 |
+ |
sortParams.setSortDirection( sortDirection ); |
300 |
295 |
|
} else { |
301 |
|
- |
if (sortQueue.size() == fieldComparators.size() && !sortQueue.isEmpty()) { |
|
296 |
+ |
if ( sortQueue.size() == fieldComparators.size() && !sortQueue.isEmpty() ) { |
302 |
297 |
|
// remove the head of the list; |
303 |
298 |
|
sortQueue.remove(); |
304 |
299 |
|
} |
305 |
|
- |
sortQueue.add(new SortFieldComparatorDirection(sortByField, comparator, sortDirection)); |
|
300 |
+ |
sortQueue.add( new SortFieldComparatorDirection( sortByField, comparator, sortDirection ) ); |
306 |
301 |
|
} |
307 |
|
- |
|
308 |
302 |
|
} |
309 |
303 |
|
|
310 |
304 |
|
/** |
311 |
305 |
|
* Sort the dataList |
|
306 |
+ |
* |
312 |
307 |
|
* @param dataList - the unsorted data |
313 |
308 |
|
*/ |
314 |
309 |
|
@SuppressWarnings("unchecked") |
315 |
|
- |
public void sort(List<R> dataList) { |
316 |
|
- |
if (dataList == null || dataList.isEmpty()) { |
|
310 |
+ |
public void sort( List<R> dataList ) { |
|
311 |
+ |
if ( dataList == null || dataList.isEmpty() ) { |
317 |
312 |
|
return; |
318 |
313 |
|
} |
319 |
314 |
|
|
320 |
315 |
|
String sortByField = dataTable.getSortByField(); |
321 |
316 |
|
SortDirection sortDirection = dataTable.getSortDirection(); |
322 |
|
- |
addToSortQueue(sortByField, sortDirection); |
|
317 |
+ |
addToSortQueue( sortByField, sortDirection ); |
323 |
318 |
|
|
324 |
|
- |
List<R> sortedDataList = new ArrayList<R>(dataList); |
325 |
|
- |
for (SortFieldComparatorDirection sortParams : sortQueue) { |
|
319 |
+ |
List<R> sortedDataList = new ArrayList<R>( dataList ); |
|
320 |
+ |
for ( SortFieldComparatorDirection sortParams : sortQueue ) { |
326 |
321 |
|
final Comparator comparator = sortParams.getComparator(); |
327 |
|
- |
final Column<R, ?> column = fieldColumns.get(sortParams.getSortByField()); |
|
322 |
+ |
final Column<R, ?> column = fieldColumns.get( sortParams.getSortByField() ); |
328 |
323 |
|
//Window.alert("sort by " + sortParams.getSortByField() + ", " + sortParams.getSortDirection().name()); |
329 |
|
- |
Collections.sort(sortedDataList, new Comparator<R>(){ |
330 |
|
- |
public int compare(R data1, R data2) { |
331 |
|
- |
Object key1 = column.getValue(data1); |
332 |
|
- |
Object key2 = column.getValue(data2); |
|
324 |
+ |
Collections.sort( sortedDataList, new Comparator<R>() { |
|
325 |
+ |
public int compare( R data1, R data2 ) { |
|
326 |
+ |
Object key1 = column.getValue( data1 ); |
|
327 |
+ |
Object key2 = column.getValue( data2 ); |
333 |
328 |
|
//Window.alert("" + key1 + " (" + comparator.compare(key1, key2) + ") " + key2); |
334 |
|
- |
return comparator.compare(key1, key2); |
|
329 |
+ |
return comparator.compare( key1, key2 ); |
335 |
330 |
|
} |
336 |
|
- |
}); |
|
331 |
+ |
} ); |
337 |
332 |
|
dataList.clear(); |
338 |
|
- |
dataList.addAll(sortedDataList); |
|
333 |
+ |
dataList.addAll( sortedDataList ); |
339 |
334 |
|
|
340 |
|
- |
if (sortParams.getSortDirection() == SortDirection.DESC) { |
341 |
|
- |
Collections.reverse(dataList); |
|
335 |
+ |
if ( sortParams.getSortDirection() == SortDirection.DESC ) { |
|
336 |
+ |
Collections.reverse( dataList ); |
342 |
337 |
|
} |
343 |
338 |
|
} |
344 |
339 |
|
} |
345 |
340 |
|
|
346 |
|
- |
private void refreshData(List<R> dataList) { |
|
341 |
+ |
private void refreshData( List<R> dataList ) { |
347 |
342 |
|
int numResultsPerPage = dataTable.getResultsPerPage(); |
348 |
343 |
|
// break up the data list into page chunks |
349 |
344 |
|
pageMarkerDataList.clear(); |
|
@@ -353,35 +348,35 @@ |
353 |
348 |
|
String pageMarker = null; |
354 |
349 |
|
int pageMarkerCounter = 0; |
355 |
350 |
|
int dataCounter = 0; |
356 |
|
- |
for (R data : dataList) { |
357 |
|
- |
List<R> pagedDataList = pageMarkerDataList.get(pageMarker); |
358 |
|
- |
if (pagedDataList == null) { |
359 |
|
- |
pagedDataList = new ArrayList<R>(numResultsPerPage); |
360 |
|
- |
pageMarkerDataList.put(pageMarker, pagedDataList); |
|
351 |
+ |
for ( R data : dataList ) { |
|
352 |
+ |
List<R> pagedDataList = pageMarkerDataList.get( pageMarker ); |
|
353 |
+ |
if ( pagedDataList == null ) { |
|
354 |
+ |
pagedDataList = new ArrayList<R>( numResultsPerPage ); |
|
355 |
+ |
pageMarkerDataList.put( pageMarker, pagedDataList ); |
361 |
356 |
|
} |
362 |
|
- |
pagedDataList.add(data); |
|
357 |
+ |
pagedDataList.add( data ); |
363 |
358 |
|
|
364 |
359 |
|
pagedDataCounter++; |
365 |
360 |
|
dataCounter++; |
366 |
|
- |
if (pagedDataCounter == numResultsPerPage) { |
367 |
|
- |
if (dataCounter < dataList.size()) { |
|
361 |
+ |
if ( pagedDataCounter == numResultsPerPage ) { |
|
362 |
+ |
if ( dataCounter < dataList.size() ) { |
368 |
363 |
|
String nextPageMarker = "" + (++pageMarkerCounter); |
369 |
|
- |
nextPageMarkerMap.put(pageMarker, nextPageMarker); |
|
364 |
+ |
nextPageMarkerMap.put( pageMarker, nextPageMarker ); |
370 |
365 |
|
pageMarker = nextPageMarker; |
371 |
366 |
|
pagedDataCounter = 0; |
372 |
367 |
|
} else { |
373 |
|
- |
nextPageMarkerMap.put(pageMarker, null); |
|
368 |
+ |
nextPageMarkerMap.put( pageMarker, null ); |
374 |
369 |
|
} |
375 |
370 |
|
} |
376 |
371 |
|
} |
377 |
372 |
|
} |
378 |
373 |
|
|
379 |
|
- |
public void setData(List<R> dataList) { |
380 |
|
- |
List<R> newList = (dataList != null && !dataList.isEmpty()) ? new ArrayList<R>(dataList) : new ArrayList<R>(); |
|
374 |
+ |
public void setData( List<R> dataList ) { |
|
375 |
+ |
List<R> newList = (dataList != null && !dataList.isEmpty()) ? new ArrayList<R>( dataList ) : new ArrayList<R>(); |
381 |
376 |
|
clear(); |
382 |
377 |
|
fullDataList = newList; |
383 |
|
- |
refreshData(fullDataList); |
384 |
|
- |
dataTable.setData(pageMarkerDataList.get(null), nextPageMarkerMap.get(null)); |
|
378 |
+ |
refreshData( fullDataList ); |
|
379 |
+ |
dataTable.setData( pageMarkerDataList.get( null ), nextPageMarkerMap.get( null ) ); |
385 |
380 |
|
} |
386 |
381 |
|
|
387 |
382 |
|
public List<R> getData() { |
|
@@ -396,12 +391,12 @@ |
396 |
391 |
|
nextPageMarkerMap.clear(); |
397 |
392 |
|
} |
398 |
393 |
|
|
399 |
|
- |
public void redraw(){ |
|
394 |
+ |
public void redraw() { |
400 |
395 |
|
dataTable.redraw(); |
401 |
396 |
|
} |
402 |
397 |
|
|
403 |
|
- |
public void addRow(R row) { |
404 |
|
- |
fullDataList.add(row); |
405 |
|
- |
setData(fullDataList); |
|
398 |
+ |
public void addRow( R row ) { |
|
399 |
+ |
fullDataList.add( row ); |
|
400 |
+ |
setData( fullDataList ); |
406 |
401 |
|
} |
407 |
402 |
|
} |