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 private String form;
131
132
133
134
135
136
137 public TableModel(TableProperties tableProperties, String charEncoding, PageContext pageContext)
138 {
139 this.rowListFull = new ArrayList(20);
140 this.headerCellList = new ArrayList(20);
141 this.properties = tableProperties;
142 this.encoding = charEncoding;
143 this.pageContext = pageContext;
144 }
145
146
147
148
149
150 protected PageContext getPageContext()
151 {
152 return this.pageContext;
153 }
154
155
156
157
158
159 public MediaTypeEnum getMedia()
160 {
161 return this.media;
162 }
163
164
165
166
167
168 public void setMedia(MediaTypeEnum media)
169 {
170 this.media = media;
171 }
172
173
174
175
176
177 public void setLocalSort(boolean localSort)
178 {
179 this.localSort = localSort;
180 }
181
182
183
184
185 public boolean isLocalSort()
186 {
187 return localSort;
188 }
189
190
191
192
193
194 public String getForm()
195 {
196 return this.form;
197 }
198
199
200
201
202
203 public void setForm(String form)
204 {
205 this.form = form;
206 }
207
208
209
210
211
212 public void setPageOffset(int offset)
213 {
214 this.pageOffset = offset;
215 }
216
217
218
219
220
221 public void setId(String tableId)
222 {
223 this.id = tableId;
224 }
225
226
227
228
229
230 public String getId()
231 {
232 return this.id;
233 }
234
235
236
237
238
239 public List getRowListFull()
240 {
241 return this.rowListFull;
242 }
243
244
245
246
247
248 public List getRowListPage()
249 {
250 return this.rowListPage;
251 }
252
253
254
255
256
257 public void addRow(Row row)
258 {
259 row.setParentTable(this);
260
261 if (log.isDebugEnabled())
262 {
263 log.debug("[" + this.id + "] adding row " + row);
264 }
265 this.rowListFull.add(row);
266 }
267
268
269
270
271
272 public void setSortedColumnName(String sortedColumnName)
273 {
274 this.sortedColumnName = sortedColumnName;
275 }
276
277
278
279
280
281
282 public void setSortFullTable(boolean sortFull)
283 {
284 this.sortFullTable = sortFull;
285 }
286
287
288
289
290
291 public boolean isSortFullTable()
292 {
293 return this.sortFullTable;
294 }
295
296
297
298
299
300 public boolean isSortOrderAscending()
301 {
302 return this.sortOrderAscending;
303
304 }
305
306
307
308
309
310 public void setSortOrderAscending(boolean isSortOrderAscending)
311 {
312 this.sortOrderAscending = isSortOrderAscending;
313 }
314
315
316
317
318 public void setRowListPage(List rowList)
319 {
320 this.rowListPage = rowList;
321 }
322
323
324
325
326
327 public TableDecorator getTableDecorator()
328 {
329 return this.tableDecorator;
330 }
331
332
333
334
335
336 public void setTableDecorator(TableDecorator decorator)
337 {
338 this.tableDecorator = decorator;
339 }
340
341
342
343
344
345 public boolean isSorted()
346 {
347 return this.sortedColumn != -1;
348 }
349
350
351
352
353
354 public HeaderCell getSortedColumnHeader()
355 {
356 if (this.sortedColumn < 0 || (this.sortedColumn > (this.headerCellList.size() - 1)))
357 {
358 return null;
359 }
360 return (HeaderCell) this.headerCellList.get(this.sortedColumn);
361 }
362
363
364
365
366
367 public int getNumberOfColumns()
368 {
369 return this.headerCellList.size();
370 }
371
372
373
374
375
376 public boolean isEmpty()
377 {
378 return this.headerCellList.size() == 0;
379 }
380
381
382
383
384
385 public int getSortedColumnNumber()
386 {
387 return this.sortedColumn;
388 }
389
390
391
392
393
394 public void setSortedColumnNumber(int sortIndex)
395 {
396 this.sortedColumn = sortIndex;
397 }
398
399
400
401
402
403 public void addColumnHeader(HeaderCell headerCell)
404 {
405 if (this.sortedColumnName == null)
406 {
407 if (this.sortedColumn == this.headerCellList.size())
408 {
409 headerCell.setAlreadySorted();
410 }
411 }
412 else
413 {
414
415 if (this.sortedColumnName.equals(headerCell.getSortName()))
416 {
417 headerCell.setAlreadySorted();
418 }
419 }
420 headerCell.setColumnNumber(this.headerCellList.size());
421
422 this.headerCellList.add(headerCell);
423 }
424
425
426
427
428
429 public List getHeaderCellList()
430 {
431 return this.headerCellList;
432 }
433
434
435
436
437
438
439
440
441 public RowIterator getRowIterator(boolean full)
442 {
443 RowIterator iterator = new RowIterator(
444 full ? this.rowListFull : this.rowListPage,
445 this.headerCellList,
446 this.tableDecorator,
447 this.pageOffset);
448
449 iterator.setId(this.id);
450 return iterator;
451 }
452
453
454
455
456
457 private void sortRowList(List list)
458 {
459 if (isSorted())
460 {
461 HeaderCell sortedHeaderCell = getSortedColumnHeader();
462
463 if (sortedHeaderCell != null)
464 {
465
466 if (sortedHeaderCell.getBeanPropertyName() != null
467 || (this.sortedColumn != -1 && this.sortedColumn < this.headerCellList.size()))
468 {
469
470 String sorted = (sortedHeaderCell.getSortProperty() != null)
471 ? sortedHeaderCell.getSortProperty()
472 : sortedHeaderCell.getBeanPropertyName();
473
474 Collections.sort(list, new RowSorter(
475 this.sortedColumn,
476 sorted,
477 getTableDecorator(),
478 this.sortOrderAscending,
479 sortedHeaderCell.getComparator()));
480 }
481 }
482
483 }
484
485 }
486
487
488
489
490 public void sortPageList()
491 {
492 if (log.isDebugEnabled())
493 {
494 log.debug("[" + this.id + "] sorting page list");
495 }
496 sortRowList(this.rowListPage);
497
498 }
499
500
501
502
503 public void sortFullList()
504 {
505 if (log.isDebugEnabled())
506 {
507 log.debug("[" + this.id + "] sorting full data");
508 }
509 sortRowList(this.rowListFull);
510 }
511
512
513
514
515
516 public TableProperties getProperties()
517 {
518 return this.properties;
519 }
520
521
522
523
524
525 public String getEncoding()
526 {
527 return encoding;
528 }
529
530
531
532
533
534 public String getCaption()
535 {
536 return this.caption;
537 }
538
539
540
541
542
543 public void setCaption(String caption)
544 {
545 this.caption = caption;
546 }
547
548
549
550
551
552 public String getFooter()
553 {
554 return this.footer;
555 }
556
557
558
559
560
561 public void setFooter(String footer)
562 {
563 this.footer = footer;
564 }
565
566
567
568
569 public String toString()
570 {
571 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
572 .append("rowListFull", this.rowListFull)
573 .append("rowListPage", this.rowListPage)
574 .append("properties", this.properties)
575 .append("empty", this.isEmpty())
576 .append("encoding", this.encoding)
577 .append("numberOfColumns", this.getNumberOfColumns())
578 .append("headerCellList", this.headerCellList)
579 .append("sortFullTable", this.sortFullTable)
580 .append("sortedColumnNumber", this.getSortedColumnNumber())
581 .append("sortOrderAscending", this.sortOrderAscending)
582 .append("sortedColumnHeader", this.getSortedColumnHeader())
583 .append("sorted", this.isSorted())
584 .append("tableDecorator", this.tableDecorator)
585 .append("caption", this.caption)
586 .append("footer", this.footer)
587 .append("media", this.media)
588 .toString();
589 }
590
591 }