1 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 34 public class TableModel 35 { 36 37 40 private static Log log = LogFactory.getLog(TableModel.class); 41 42 45 private List headerCellList; 46 47 50 private List rowListFull; 51 52 55 private List rowListPage; 56 57 60 private String sortedColumnName; 61 62 65 private boolean sortOrderAscending = true; 66 67 70 private boolean sortFullTable = true; 71 72 75 private int sortedColumn = -1; 76 77 80 private TableDecorator tableDecorator; 81 82 85 private String id; 86 87 90 private TableProperties properties; 91 92 95 private int pageOffset; 96 97 100 private String encoding; 101 102 105 private boolean localSort = true; 106 107 110 private String caption; 111 112 115 private String footer; 116 117 120 private PageContext pageContext; 121 122 125 private MediaTypeEnum media; 126 127 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 145 protected PageContext getPageContext() 146 { 147 return this.pageContext; 148 } 149 150 154 public MediaTypeEnum getMedia() 155 { 156 return this.media; 157 } 158 159 163 public void setMedia(MediaTypeEnum media) 164 { 165 this.media = media; 166 } 167 168 172 public void setLocalSort(boolean localSort) 173 { 174 this.localSort = localSort; 175 } 176 177 180 public boolean isLocalSort() 181 { 182 return localSort; 183 } 184 185 189 public void setPageOffset(int offset) 190 { 191 this.pageOffset = offset; 192 } 193 194 198 public void setId(String tableId) 199 { 200 this.id = tableId; 201 } 202 203 207 public String getId() 208 { 209 return this.id; 210 } 211 212 216 public List getRowListFull() 217 { 218 return this.rowListFull; 219 } 220 221 225 public List getRowListPage() 226 { 227 return this.rowListPage; 228 } 229 230 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 249 public void setSortedColumnName(String sortedColumnName) 250 { 251 this.sortedColumnName = sortedColumnName; 252 } 253 254 259 public void setSortFullTable(boolean sortFull) 260 { 261 this.sortFullTable = sortFull; 262 } 263 264 268 public boolean isSortFullTable() 269 { 270 return this.sortFullTable; 271 } 272 273 277 public boolean isSortOrderAscending() 278 { 279 return this.sortOrderAscending; 280 281 } 282 283 287 public void setSortOrderAscending(boolean isSortOrderAscending) 288 { 289 this.sortOrderAscending = isSortOrderAscending; 290 } 291 292 295 public void setRowListPage(List rowList) 296 { 297 this.rowListPage = rowList; 298 } 299 300 304 public TableDecorator getTableDecorator() 305 { 306 return this.tableDecorator; 307 } 308 309 313 public void setTableDecorator(TableDecorator decorator) 314 { 315 this.tableDecorator = decorator; 316 } 317 318 322 public boolean isSorted() 323 { 324 return this.sortedColumn != -1; 325 } 326 327 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 344 public int getNumberOfColumns() 345 { 346 return this.headerCellList.size(); 347 } 348 349 353 public boolean isEmpty() 354 { 355 return this.headerCellList.size() == 0; 356 } 357 358 362 public int getSortedColumnNumber() 363 { 364 return this.sortedColumn; 365 } 366 367 371 public void setSortedColumnNumber(int sortIndex) 372 { 373 this.sortedColumn = sortIndex; 374 } 375 376 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 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 406 public List getHeaderCellList() 407 { 408 return this.headerCellList; 409 } 410 411 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 iterator.setId(this.id); 427 return iterator; 428 } 429 430 434 private void sortRowList(List list) 435 { 436 if (isSorted()) 437 { 438 HeaderCell sortedHeaderCell = getSortedColumnHeader(); 439 440 if (sortedHeaderCell != null) 441 { 442 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 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 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 493 public TableProperties getProperties() 494 { 495 return this.properties; 496 } 497 498 502 public String getEncoding() 503 { 504 return encoding; 505 } 506 507 511 public String getCaption() 512 { 513 return this.caption; 514 } 515 516 520 public void setCaption(String caption) 521 { 522 this.caption = caption; 523 } 524 525 529 public String getFooter() 530 { 531 return this.footer; 532 } 533 534 538 public void setFooter(String footer) 539 { 540 this.footer = footer; 541 } 542 543 546 public String toString() 547 { 548 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) .append("rowListFull", this.rowListFull) .append("rowListPage", this.rowListPage) .append("properties", this.properties) .append("empty", this.isEmpty()) .append("encoding", this.encoding) .append("numberOfColumns", this.getNumberOfColumns()) .append("headerCellList", this.headerCellList) .append("sortFullTable", this.sortFullTable) .append("sortedColumnNumber", this.getSortedColumnNumber()) .append("sortOrderAscending", this.sortOrderAscending) .append("sortedColumnHeader", this.getSortedColumnHeader()) .append("sorted", this.isSorted()) .append("tableDecorator", this.tableDecorator) .append("caption", this.caption) .append("footer", this.footer) .append("media", this.media) .toString(); 566 } 567 568 } | Popular Tags |