1
2
3
4
5
6
7
8
9
10
11
12 package org.displaytag.model;
13
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17
18 import javax.servlet.jsp.PageContext;
19
20 import org.apache.commons.lang.builder.ToStringBuilder;
21 import org.apache.commons.lang.builder.ToStringStyle;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.displaytag.decorator.TableDecorator;
25 import org.displaytag.properties.MediaTypeEnum;
26 import org.displaytag.properties.TableProperties;
27
28
29
30
31
32
33
34 public class TableModel
35 {
36
37
38
39
40 private static Log log = LogFactory.getLog(TableModel.class);
41
42
43
44
45 private List headerCellList;
46
47
48
49
50 private List rowListFull;
51
52
53
54
55 private List rowListPage;
56
57
58
59
60 private String sortedColumnName;
61
62
63
64
65 private boolean sortOrderAscending = true;
66
67
68
69
70 private boolean sortFullTable = true;
71
72
73
74
75 private int sortedColumn = -1;
76
77
78
79
80 private TableDecorator tableDecorator;
81
82
83
84
85 private String id;
86
87
88
89
90 private TableProperties properties;
91
92
93
94
95 private int pageOffset;
96
97
98
99
100 private String encoding;
101
102
103
104
105 private boolean localSort = true;
106
107
108
109
110 private String caption;
111
112
113
114
115 private String footer;
116
117
118
119
120 private PageContext pageContext;
121
122
123
124
125 private MediaTypeEnum media;
126
127
128
129
130
131
132 public TableModel(TableProperties tableProperties, String charEncoding, PageContext pageContext)
133 {
134 this.rowListFull = new ArrayList(20);
135 this.headerCellList = new ArrayList(20);
136 this.properties = tableProperties;
137 this.encoding = charEncoding;
138 this.pageContext = pageContext;
139 }
140
141
142
143
144
145 protected PageContext getPageContext()
146 {
147 return this.pageContext;
148 }
149
150
151
152
153
154 public MediaTypeEnum getMedia()
155 {
156 return this.media;
157 }
158
159
160
161
162
163 public void setMedia(MediaTypeEnum media)
164 {
165 this.media = media;
166 }
167
168
169
170
171
172 public void setLocalSort(boolean localSort)
173 {
174 this.localSort = localSort;
175 }
176
177
178
179
180 public boolean isLocalSort()
181 {
182 return localSort;
183 }
184
185
186
187
188
189 public void setPageOffset(int offset)
190 {
191 this.pageOffset = offset;
192 }
193
194
195
196
197
198 public void setId(String tableId)
199 {
200 this.id = tableId;
201 }
202
203
204
205
206
207 public String getId()
208 {
209 return this.id;
210 }
211
212
213
214
215
216 public List getRowListFull()
217 {
218 return this.rowListFull;
219 }
220
221
222
223
224
225 public List getRowListPage()
226 {
227 return this.rowListPage;
228 }
229
230
231
232
233
234 public void addRow(Row row)
235 {
236 row.setParentTable(this);
237
238 if (log.isDebugEnabled())
239 {
240 log.debug("[" + this.id + "] adding row " + row);
241 }
242 this.rowListFull.add(row);
243 }
244
245
246
247
248
249 public void setSortedColumnName(String sortedColumnName)
250 {
251 this.sortedColumnName = sortedColumnName;
252 }
253
254
255
256
257
258
259 public void setSortFullTable(boolean sortFull)
260 {
261 this.sortFullTable = sortFull;
262 }
263
264
265
266
267
268 public boolean isSortFullTable()
269 {
270 return this.sortFullTable;
271 }
272
273
274
275
276
277 public boolean isSortOrderAscending()
278 {
279 return this.sortOrderAscending;
280
281 }
282
283
284
285
286
287 public void setSortOrderAscending(boolean isSortOrderAscending)
288 {
289 this.sortOrderAscending = isSortOrderAscending;
290 }
291
292
293
294
295 public void setRowListPage(List rowList)
296 {
297 this.rowListPage = rowList;
298 }
299
300
301
302
303
304 public TableDecorator getTableDecorator()
305 {
306 return this.tableDecorator;
307 }
308
309
310
311
312
313 public void setTableDecorator(TableDecorator decorator)
314 {
315 this.tableDecorator = decorator;
316 }
317
318
319
320
321
322 public boolean isSorted()
323 {
324 return this.sortedColumn != -1;
325 }
326
327
328
329
330
331 public HeaderCell getSortedColumnHeader()
332 {
333 if (this.sortedColumn < 0 || (this.sortedColumn > (this.headerCellList.size() - 1)))
334 {
335 return null;
336 }
337 return (HeaderCell) this.headerCellList.get(this.sortedColumn);
338 }
339
340
341
342
343
344 public int getNumberOfColumns()
345 {
346 return this.headerCellList.size();
347 }
348
349
350
351
352
353 public boolean isEmpty()
354 {
355 return this.headerCellList.size() == 0;
356 }
357
358
359
360
361
362 public int getSortedColumnNumber()
363 {
364 return this.sortedColumn;
365 }
366
367
368
369
370
371 public void setSortedColumnNumber(int sortIndex)
372 {
373 this.sortedColumn = sortIndex;
374 }
375
376
377
378
379
380 public void addColumnHeader(HeaderCell headerCell)
381 {
382 if (this.sortedColumnName == null)
383 {
384 if (this.sortedColumn == this.headerCellList.size())
385 {
386 headerCell.setAlreadySorted();
387 }
388 }
389 else
390 {
391
392 if (this.sortedColumnName.equals(headerCell.getSortName()))
393 {
394 headerCell.setAlreadySorted();
395 }
396 }
397 headerCell.setColumnNumber(this.headerCellList.size());
398
399 this.headerCellList.add(headerCell);
400 }
401
402
403
404
405
406 public List getHeaderCellList()
407 {
408 return this.headerCellList;
409 }
410
411
412
413
414
415
416
417
418 public RowIterator getRowIterator(boolean full)
419 {
420 RowIterator iterator = new RowIterator(
421 full ? this.rowListFull : this.rowListPage,
422 this.headerCellList,
423 this.tableDecorator,
424 this.pageOffset);
425
426 iterator.setId(this.id);
427 return iterator;
428 }
429
430
431
432
433
434 private void sortRowList(List list)
435 {
436 if (isSorted())
437 {
438 HeaderCell sortedHeaderCell = getSortedColumnHeader();
439
440 if (sortedHeaderCell != null)
441 {
442
443 if (sortedHeaderCell.getBeanPropertyName() != null
444 || (this.sortedColumn != -1 && this.sortedColumn < this.headerCellList.size()))
445 {
446
447 String sorted = (sortedHeaderCell.getSortProperty() != null)
448 ? sortedHeaderCell.getSortProperty()
449 : sortedHeaderCell.getBeanPropertyName();
450
451 Collections.sort(list, new RowSorter(
452 this.sortedColumn,
453 sorted,
454 getTableDecorator(),
455 this.sortOrderAscending,
456 sortedHeaderCell.getComparator()));
457 }
458 }
459
460 }
461
462 }
463
464
465
466
467 public void sortPageList()
468 {
469 if (log.isDebugEnabled())
470 {
471 log.debug("[" + this.id + "] sorting page list");
472 }
473 sortRowList(this.rowListPage);
474
475 }
476
477
478
479
480 public void sortFullList()
481 {
482 if (log.isDebugEnabled())
483 {
484 log.debug("[" + this.id + "] sorting full data");
485 }
486 sortRowList(this.rowListFull);
487 }
488
489
490
491
492
493 public TableProperties getProperties()
494 {
495 return this.properties;
496 }
497
498
499
500
501
502 public String getEncoding()
503 {
504 return encoding;
505 }
506
507
508
509
510
511 public String getCaption()
512 {
513 return this.caption;
514 }
515
516
517
518
519
520 public void setCaption(String caption)
521 {
522 this.caption = caption;
523 }
524
525
526
527
528
529 public String getFooter()
530 {
531 return this.footer;
532 }
533
534
535
536
537
538 public void setFooter(String footer)
539 {
540 this.footer = footer;
541 }
542
543
544
545
546 public String toString()
547 {
548 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
549 .append("rowListFull", this.rowListFull)
550 .append("rowListPage", this.rowListPage)
551 .append("properties", this.properties)
552 .append("empty", this.isEmpty())
553 .append("encoding", this.encoding)
554 .append("numberOfColumns", this.getNumberOfColumns())
555 .append("headerCellList", this.headerCellList)
556 .append("sortFullTable", this.sortFullTable)
557 .append("sortedColumnNumber", this.getSortedColumnNumber())
558 .append("sortOrderAscending", this.sortOrderAscending)
559 .append("sortedColumnHeader", this.getSortedColumnHeader())
560 .append("sorted", this.isSorted())
561 .append("tableDecorator", this.tableDecorator)
562 .append("caption", this.caption)
563 .append("footer", this.footer)
564 .append("media", this.media)
565 .toString();
566 }
567
568 }