-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataTable.java
403 lines (338 loc) · 13 KB
/
DataTable.java
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
package org.zkoss.google.charts.data;
import org.zkoss.json.JSONArray;
import org.zkoss.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* <p>
* Wrapper of the <a href= "https://developers.google.com/chart/interactive/docs/reference#DataTable"
* >google.visualization.DataTable</a> JavaScript class provided in Google Charts.
* </p>
*
* @author Sean Connolly
*/
public class DataTable extends JSONObject {
private static final String COLUMNS = "cols";
private static final String ROWS = "rows";
// TODO base off of GWT implementation:
// http://gwt-google-apis.googlecode.com/svn/javadoc/visualization/1.1/com/google/gwt/visualization/client/AbstractDataTable.html
// TODO implement like ZK Grid
private List<Column> columns = new ArrayList<Column>();
private List<Row> rows = new ArrayList<Row>();
@SuppressWarnings("unchecked")
public DataTable() {
put(COLUMNS, columns);
put(ROWS, rows);
}
/**
* @return the number of columns in the table
*/
public int getNumberOfColumns() {
return columns.size();
}
/**
* @return the number of rows in the table
*/
public int getNumberOfRows() {
return rows.size();
}
/**
* Returns the value of the cell at the given row and column indexes.
*
* @param rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by
* the {@link #getNumberOfRows()} method.
* @param columnIndex should be a number greater than or equal to zero, and less than the number of columns as
* returned by the {@link #getNumberOfColumns()} method.
* @return The type of the returned value depends on the column type (see {@link #getColumnType(int)}):
* <ul>
* <li>If the column type is {@code 'string'}, the value is a {@code String}.</li>
* <li>If the column type is {@code 'number'}, the value is an {@code Integer}.</li>
* <li>If the column type is {@code 'boolean'}, the value is a {@code Boolean}.</li>
* <li>If the column type is {@code 'date'} or {@code 'datetime'}, the value is a {@code Date} object.</li>
* <li>If the column type is {@code 'timeofday'}, the value is an array of four numbers: [hour, minute,
* second, milliseconds].</li>
* <li>If the column value is a {@code null} value, an exception is thrown.</li>
* </ul>
*/
public Object getValue(int rowIndex, int columnIndex) {
return getRow(rowIndex).getCell(columnIndex).getValue();
}
/**
* Sets the value of a cell. In addition to overwriting any existing cell value, this method will also clear out any
* formatted value and properties for the cell.
*
* @param rowIndex should be a number greater than or equal to zero, and less than the number of rows as returned by
* the {@link #getNumberOfRows()} method.
* @param columnIndex should be a number greater than or equal to zero, and less than the number of columns as
* returned by the {@link #getNumberOfColumns()} method.
* @param value is the value assigned to the specified cell. The type of the returned value depends on the column
* type (see {@link #getColumnType(int)}):
* <ul>
* <li>If the column type is {@code 'string'}, the value is a {@code String}.</li>
* <li>If the column type is {@code 'number'}, the value is an {@code Integer}.</li>
* <li>If the column type is {@code 'boolean'}, the value is a {@code Boolean}.</li>
* <li>If the column type is {@code 'date'} or {@code 'datetime'}, the value is a {@code Date} object.</li>
* <li>If the column type is {@code 'timeofday'}, the value is an array of four numbers: [hour, minute,
* second, milliseconds].</li>
* <li>If the column value is a {@code null} value, an exception is thrown.</li>
* </ul>
*/
public void setValue(int rowIndex, int columnIndex, Object value) {
getRow(rowIndex).getCell(columnIndex).setValue(value);
}
public Column addStringColumn(String label) {
return addColumn(ColumnType.STRING, label);
}
public Column addNumberColumn(String label) {
return addColumn(ColumnType.NUMBER, label);
}
public Column addBooleanColumn(String label) {
return addColumn(ColumnType.BOOLEAN, label);
}
public Column addDateColumn(String label) {
return addColumn(ColumnType.DATE, label);
}
public Column addDateTimeColumn(String label) {
return addColumn(ColumnType.DATETIME, label);
}
public Column addTimeColumn(String label) {
return addColumn(ColumnType.TIMEOFDAY, label);
}
public Column addColumn(ColumnType type, String label) {
Column column = new Column(type, label);
columns.add(column);
for (Row row : rows) {
row.setCapacity(columns.size());
}
return column;
}
public Column addStringColumn(String label, String id) {
return addColumn(ColumnType.STRING, label, id);
}
public Column addNumberColumn(String label, String id) {
return addColumn(ColumnType.NUMBER, label, id);
}
public Column addBooleanColumn(String label, String id) {
return addColumn(ColumnType.BOOLEAN, label, id);
}
public Column addDateColumn(String label, String id) {
return addColumn(ColumnType.DATE, label, id);
}
public Column addDateTimeColumn(String label, String id) {
return addColumn(ColumnType.DATETIME, label, id);
}
public Column addTimeColumn(String label, String id) {
return addColumn(ColumnType.TIMEOFDAY, label, id);
}
public Column addColumn(ColumnType type, String label, String id) {
Column column = new Column(type, label, id);
columns.add(column);
for (Row row : rows) {
row.setCapacity(columns.size());
}
return column;
}
public Column addColumn(ColumnType type, ColumnRole role) {
Column column = new Column(type, role);
columns.add(column);
for (Row row : rows) {
row.setCapacity(columns.size());
}
return column;
}
public ColumnType getColumnType(int index) {
return columns.get(index).getType();
}
public String getColumnLabel(int index) {
return columns.get(index).getLabel();
}
public String getColumnId(int index) {
return columns.get(index).getId();
}
public Row addRow() {
Row row = new Row(getNumberOfColumns());
rows.add(row);
return row;
}
public Row addRow(int index) {
Row row = new Row(getNumberOfColumns());
rows.add(index, row);
return row;
}
public Row addRow(Object... values) {
if (values.length != getNumberOfColumns()) {
throw new IllegalArgumentException("Cannot add a row of " + values.length + " cells to a DataTable of "
+ columns.size() + " columns.");
}
Row row = new Row(getNumberOfColumns());
for (int i = 0; i < values.length; i++) {
Object value = values[i];
if (value instanceof FormattedValue) {
row.setValue(i, (FormattedValue) value);
} else {
row.setValue(i, value);
}
}
rows.add(row);
return row;
}
private Row getRow(int rowIndex) {
if (rowIndex < 0 || rowIndex >= getNumberOfRows()) {
throw new IndexOutOfBoundsException("rowIndex: " + rowIndex);
}
return rows.get(rowIndex);
}
/**
* <p>
* A column of the {@link DataTable} contents.
* </p>
* <p>
* Serializes to an object of the JSON <a href=
* "https://developers.google.com/chart/interactive/docs/reference#dataparam" >{@code cols}</a> property.
* </p>
*/
public static final class Column extends JSONObject {
private static final String TYPE = "type";
private static final String LABEL = "label";
private static final String ID = "id";
private static final String PATTERN = "pattern";
private static final String ROLE = "role";
private static final String PROPERTIES = "p";
/**
* @param type The data type of the column.
* @param label A label for the column.
*/
@SuppressWarnings("unchecked")
public Column(ColumnType type, String label) {
put(TYPE, type.toString());
put(LABEL, label);
}
/**
* @param type The data type of the column.
* @param label A label for the column.
* @param id An ID for the column.
*/
@SuppressWarnings("unchecked")
public Column(ColumnType type, String label, String id) {
put(TYPE, type.toString());
put(LABEL, label);
put(ID, id);
}
/**
* @param type The data type of the column.
* @param label A label for the column.
* @param id An ID for the column.
* @param pattern A number (or date) format string specifying how to display the column value.
*/
@SuppressWarnings("unchecked")
public Column(ColumnType type, String label, String id, String pattern) {
put(TYPE, type.toString());
put(LABEL, label);
put(ID, id);
put(PATTERN, pattern);
}
/**
* @param type The data type of the column.
* @param role The role of the column.
*/
public Column(ColumnType type, ColumnRole role) {
put(TYPE, type.toString());
put(ROLE, role.toString());
}
public ColumnType getType() {
Object type = get(TYPE);
return type != null ? ColumnType.fromString(type.toString()) : null;
}
public String getLabel() {
Object label = get(LABEL);
return label != null ? label.toString() : null;
}
public String getId() {
Object id = get(ID);
return id != null ? id.toString() : null;
}
}
/**
* <p>
* A row of the {@link DataTable} contents.
* </p>
* <p>
* Serializes to an element of the JSON <a href=
* "https://developers.google.com/chart/interactive/docs/reference#rowsproperty" >{@code rows}</a> property.
* </p>
*/
private static final class Row extends JSONObject {
private static final String CELLS = "c";
private static final String PROPERTIES = "p";
private final Cells cells = new Cells();
@SuppressWarnings("unchecked")
Row(int capacity) {
put(CELLS, cells);
cells.setCapacity(capacity);
}
@SuppressWarnings("unchecked")
void setValue(int colIndex, Object value) {
getCell(colIndex).setValue(value);
}
void setValue(int colIndex, FormattedValue value) {
setValue(colIndex, value.getValue(), value.getFormat());
}
@SuppressWarnings("unchecked")
void setValue(int colIndex, Object value, String format) {
getCell(colIndex).setValue(value).setFormat(format);
}
Cell getCell(int columnIndex) {
if (columnIndex < 0 || columnIndex >= cells.size()) {
throw new IndexOutOfBoundsException("columnIndex: " + columnIndex);
}
return (Cell) cells.get(columnIndex);
}
void setCapacity(int capacity) {
cells.setCapacity(capacity);
}
}
private static final class Cells extends JSONArray {
void setCapacity(int capacity) {
// TODO efficiency
while (size() < capacity) {
add(new Cell(null));
}
}
}
/**
* <p>
* A cell of the {@link DataTable} contents.
* </p>
* <p>
* Serializes to an element of the JSON <a href=
* "https://developers.google.com/chart/interactive/docs/reference#cell_object" >{@code cell}</a> object.
* </p>
*/
private static final class Cell extends JSONObject {
private static final String VALUE = "v";
private static final String FORMAT = "f";
private static final String PROPERTIES = "p";
Cell(Object value) {
setValue(value);
}
Cell(Object value, String format) {
setValue(value);
setFormat(format);
}
Object getValue() {
return get(VALUE);
}
Cell setValue(Object value) {
if (value instanceof Date) {
value = ((Date) value).getTime();
}
put(VALUE, value);
return this;
}
Cell setFormat(String format) {
put(FORMAT, format);
return this;
}
}
}