Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package com.temp.client.foundation.widget.table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.temp.shared.utils.ComparatorSource;
import com.temp.shared.utils.StringUtils;
import com.temp.shared.LoadingProgressListener;
import com.temp.client.foundation.widget.table.CustomDataTable.SortDirection;
import com.temp.client.foundation.widget.table.CustomDataTable.DataTableUpdater;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionModel;

/**
 *
 * @author paule
 * To use CachedDataTable:
 * <pre>
 * 1) instantiate:
 *          table = new CachedDataTable&lt;RowType&gt;(...)
 *
 *  2) add Columns:
 *          table.addColumn(...)
 *
 *  3) set data of type List&lt;RowType&gt;
 *          table.setData(...)
 *
 *  And you're done!
 *</pre>
 * @param <R> the row type
 */
@SuppressWarnings("rawtypes")
public class CachedDataTable<R extends DataRow<F>,F> extends Composite implements LoadingProgressListener {
    public static final String DEFAULT_HEIGHT = "350px";
    public static final String DEFAULT_WIDTH = "100%";

    private static class SortFieldComparatorDirection {
        String sortByField;
        Comparator comparator;
        SortDirection sortDirection;
        SortFieldComparatorDirection(String _sortByField, Comparator _comparator, SortDirection _sortDirection) {
            sortByField = _sortByField;
            comparator = _comparator;
            sortDirection = _sortDirection;
        }

        String getSortByField() { return sortByField; }
        Comparator getComparator() { return comparator; }
        SortDirection getSortDirection() { return sortDirection; }

        void setSortDirection(SortDirection _sortDirection) { sortDirection = _sortDirection; }
    }

    // table
    CustomDataTable<R,F> dataTable;
    Map<String, Column<R, ?>> fieldColumns = new HashMap<String, Column<R, ?>>();
    Map<String, Comparator> fieldComparators = new HashMap<String, Comparator>();

    // data
    List<R> fullDataList = new ArrayList<R>();
    LinkedHashMap<String, List<R>> pageMarkerDataList = new LinkedHashMap<String, List<R>>();
    Map<String, String> nextPageMarkerMap = new HashMap<String, String>();
    LinkedList<SortFieldComparatorDirection> sortQueue = new LinkedList<SortFieldComparatorDirection>();

    public CachedDataTable() {
        this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, null, false, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public CachedDataTable(String title) {
        this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, null, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public CachedDataTable(String title, ClickHandler downloadCSVButtonClickHandler) {
        this(CustomDataTable.DEFAULT_RESULTS_PER_PAGE, true, title, true, null, null, downloadCSVButtonClickHandler, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public CachedDataTable(int pageSize, boolean supportPaging, String width, String height) {
        this( pageSize, supportPaging, null, false, null, null, null, width, height);
    }

    public CachedDataTable(String title, int pageSize, boolean supportPaging, String width, String height) {
        this( pageSize, supportPaging, title, true, null, null, null, width, height);
    }

    public CachedDataTable(String title, int pageSize, boolean supportPaging, boolean includeRowNumber, String width, String height) {
        this( pageSize, supportPaging, title, true, null, null, null, includeRowNumber, width, height);
    }

    public CachedDataTable(int pageSize, boolean supportPaging, String title, boolean includeTitleBar,  Widget topWidget,
            Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, String width, String height) {
        this(pageSize, supportPaging, title, includeTitleBar,  topWidget,
            bottomWidget,  downloadCSVButtonClickHandler, true, width, height);
    }

    private CachedDataTable(int pageSize, boolean supportPaging, String title, boolean includeTitleBar,  Widget topWidget,
            Widget bottomWidget, final ClickHandler downloadCSVButtonClickHandler, boolean includeRowNumber, String width, String height) {

        boolean includeDownloadCSVButton = (downloadCSVButtonClickHandler != null);
        dataTable = new CustomDataTable<R,F>(
                pageSize, supportPaging, title,
                new DataTableUpdater() {
                    public void updateDataTable() {
                        String pageMarker = dataTable.getPageMarker();
                        List<R> dataList = new ArrayList<R>();
                        for (List<R> pagedDataList : pageMarkerDataList.values()) {
                            dataList.addAll(pagedDataList);
                        }
                        sort(dataList);
                        refreshData(dataList);
                        List<R> dataListPage = pageMarkerDataList.get(pageMarker);
                        String nextPageMarker = nextPageMarkerMap.get(pageMarker);
                        dataTable.setData(dataListPage, nextPageMarker);
                    }
                    public void onDataTableDownloadCSVButtonClick(ClickEvent event) {
                        downloadCSVButtonClickHandler.onClick(event);
                    }
                },
                topWidget, bottomWidget,
                includeTitleBar, includeDownloadCSVButton, includeRowNumber,
                width, height);
        initWidget(dataTable);
    }

    public void setTitle(String title) {
        dataTable.setTitle(title);
    }

    public void setSelectionModel(SelectionModel<R> selectionModel) {
        dataTable.setSelectionModel(selectionModel);
    }

    public SelectionModel<? super R> getSelectionModel() {
        return dataTable.getSelectionModel();
    }

    @Override
    public void loadingReset() {
        dataTable.loadingReset();
    }

    @Override
    public void loadingStarted() {
        dataTable.loadingStarted();
    }

    @Override
    public void loadingCompleted(boolean successfully) {
        dataTable.loadingCompleted(successfully);
    }

    /**
     * Adds a column
     *
     * @param fields
     *            - the column / "field" definitions
     */
    public <FIELD extends DataRowField> void addColumns(FIELD... fields) {
        if (fields != null) {
            for (FIELD field : fields) {
                addColumn(field);
            }
        }
    }

    /**
     * Adds a column
     *
     * @param field
     *            - the column or "field" definition
     */
    public <FIELD extends DataRowField> void addColumn(FIELD field) {
        if (show(field)) {
            addComparator( dataTable.addColumn(field), field, getComparator( field ) );
        }
    }

    /**
     * Adds a column
     *
     * @param column
     *            - the column
     * @param field
     *            - the column definition "field"
     */
    public <FIELD extends DataRowField> void addColumn(Column<R, ?> column, FIELD field) {
        if (show(field)) {
            addComparator(dataTable.addColumn(column, field), field, getComparator(field));
        }
    }

    /**
     * Adds a column
     *
     * @param field
     *            - the column or "field" definition
     * @param comparator - the comparator
     */
    public <FIELD extends DataRowField> void addColumn(FIELD field, Comparator comparator) {
        if (show(field)) {
            addComparator(dataTable.addColumn(field), field, comparator);
        }
    }

    /**
     * Adds a column
     *
     * @param column
     *            - the column
     * @param field
     *            - the column definition "field"
     * @param comparator - the comparator
     */
    public <FIELD extends DataRowField> void addColumn(Column<R, ?> column, FIELD field, Comparator comparator) {
        if (show(field)) {
            addComparator(dataTable.addColumn(column, field), field, comparator);
        }
    }

    private <FIELD extends DataRowField> boolean show(FIELD field) {
        if (field == null) {
            return false;
        }
        return (field instanceof ColumnHidable) ? !((ColumnHidable)field).hideColumn() : true;
    }

    /**
     * Adds a column with a comparator.
     * @param column - the column
     * @param columnHeader - the column header that appears at the top of the table
     * @param fieldName - must be unique among columns (used as key for columns for sorting)
     * @param comparator - the comparator
     */
    public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator) {
        dataTable.addColumn(column, columnHeader, fieldName);
        addComparator(column, fieldName, comparator);
    }

    /**
     * Adds a column
     * @param column - the column
     * @param columnHeader - the column header to be displayed
     */
    public void addColumn(Column<R, ?> column, String columnHeader, String fieldName) {
        dataTable.addColumn(column, columnHeader, fieldName);
    }

    public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, int width, Unit widthUnit) {
        dataTable.addColumn(column, columnHeader, fieldName, width, widthUnit);
    }

    public void addColumn(Column<R, ?> column, String columnHeader, String fieldName, Comparator comparator, int width, Unit widthUnit) {
        dataTable.addColumn(column, columnHeader, fieldName, width, widthUnit);
        addComparator(column, fieldName, comparator);
    }

    private <FIELD extends DataRowField> Comparator<?> getComparator(FIELD field) {
        if (field instanceof Comparator) {
            return (Comparator)field;
        }
        if (field instanceof ComparatorSource) {
            return ((ComparatorSource)field).getComparator();
        }
        return null;
    }

    private <FIELD extends DataRowField> void addComparator(Column<R, ?> column, FIELD field, Comparator comparator) {
        addComparator(column, field.name(), comparator);
    }

    private void addComparator(Column<R, ?> column, String fieldName, Comparator comparator) {
        if (comparator != null) {
            fieldComparators.put(fieldName, comparator);
            fieldColumns.put(fieldName, column);
        }
    }

    private void addToSortQueue(String sortByField, SortDirection sortDirection) {
        if (StringUtils.isBlank(sortByField)) {
            return;
        }
        Comparator comparator = fieldComparators.get(sortByField);
        if (comparator == null) {
            return;
        }
        SortFieldComparatorDirection sortParams = !sortQueue.isEmpty() ? sortQueue.getLast() : null;
        if (sortParams != null && sortParams.getSortByField().equals(sortByField))  {
            sortParams.setSortDirection(sortDirection);
        } else {
            if (sortQueue.size() == fieldComparators.size() && !sortQueue.isEmpty()) {
                // remove the head of the list;
                sortQueue.remove();
            }
            sortQueue.add(new SortFieldComparatorDirection(sortByField, comparator, sortDirection));
        }

    }

    /**
     * Sort the dataList
     * @param dataList - the unsorted data
     */
    @SuppressWarnings("unchecked")
    public void sort(List<R> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }

        String sortByField = dataTable.getSortByField();
        SortDirection sortDirection = dataTable.getSortDirection();
        addToSortQueue(sortByField, sortDirection);

        List<R> sortedDataList = new ArrayList<R>(dataList);
        for (SortFieldComparatorDirection sortParams : sortQueue) {
            final Comparator comparator = sortParams.getComparator();
            final Column<R, ?> column = fieldColumns.get(sortParams.getSortByField());
            //Window.alert("sort by " + sortParams.getSortByField() + ", " + sortParams.getSortDirection().name());
            Collections.sort(sortedDataList, new Comparator<R>(){
                public int compare(R data1, R data2) {
                    Object key1 = column.getValue(data1);
                    Object key2 = column.getValue(data2);
                    //Window.alert("" + key1 + " (" + comparator.compare(key1, key2) + ") " + key2);
                    return comparator.compare(key1, key2);
                }
            });
            dataList.clear();
            dataList.addAll(sortedDataList);

            if (sortParams.getSortDirection() == SortDirection.DESC) {
                Collections.reverse(dataList);
            }
        }
    }

    private void refreshData(List<R> dataList) {
        int numResultsPerPage = dataTable.getResultsPerPage();
        // break up the data list into page chunks
        pageMarkerDataList.clear();
        nextPageMarkerMap.clear();

        int pagedDataCounter = 0;
        String pageMarker = null;
        int pageMarkerCounter = 0;
        int dataCounter = 0;
        for (R data : dataList) {
            List<R> pagedDataList = pageMarkerDataList.get(pageMarker);
            if (pagedDataList == null) {
                pagedDataList = new ArrayList<R>(numResultsPerPage);
                pageMarkerDataList.put(pageMarker, pagedDataList);
            }
            pagedDataList.add(data);

            pagedDataCounter++;
            dataCounter++;
            if (pagedDataCounter == numResultsPerPage) {
                if (dataCounter < dataList.size()) {
                    String nextPageMarker = "" + (++pageMarkerCounter);
                    nextPageMarkerMap.put(pageMarker, nextPageMarker);
                    pageMarker = nextPageMarker;
                    pagedDataCounter = 0;
                } else {
                    nextPageMarkerMap.put(pageMarker, null);
                }
            }
        }
    }

    public void setData(List<R> dataList) {
    	List<R> newList = (dataList != null && !dataList.isEmpty()) ? new ArrayList<R>(dataList) : new ArrayList<R>();
        clear();
        fullDataList = newList;
        refreshData(fullDataList);
        dataTable.setData(pageMarkerDataList.get(null), nextPageMarkerMap.get(null));
    }

    public List<R> getData() {
        return fullDataList;
    }

    public void clear() {
        dataTable.reset();
        fullDataList.clear();
        sortQueue.clear();
        pageMarkerDataList.clear();
        nextPageMarkerMap.clear();
    }

    public void redraw(){
        dataTable.redraw();
    }

    public void addRow(R row) {
        fullDataList.add(row);
        setData(fullDataList);
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/table/CachedDataTable.java

Diff revisions: vs.
Revision Author Commited Message
626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000