1 31 32 package org.opencms.workplace.list; 33 34 import org.opencms.main.CmsIllegalStateException; 35 import org.opencms.util.CmsIdentifiableObjectContainer; 36 import org.opencms.util.CmsStringUtil; 37 import org.opencms.workplace.CmsWorkplace; 38 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Locale ; 42 import java.util.Set ; 43 import java.util.TreeSet ; 44 45 54 public class CmsListMetadata { 55 56 57 public static final String SEARCH_BAR_INPUT_ID = "listSearchFilter"; 58 59 60 private CmsIdentifiableObjectContainer m_columns = new CmsIdentifiableObjectContainer(true, false); 61 62 63 private CmsIdentifiableObjectContainer m_indepActions = new CmsIdentifiableObjectContainer(true, false); 64 65 66 private CmsIdentifiableObjectContainer m_itemDetails = new CmsIdentifiableObjectContainer(true, false); 67 68 69 private String m_listId; 70 71 72 private CmsIdentifiableObjectContainer m_multiActions = new CmsIdentifiableObjectContainer(true, false); 73 74 75 private CmsListSearchAction m_searchAction; 76 77 78 private boolean m_volatile = false; 79 80 85 public CmsListMetadata(String listId) { 86 87 m_listId = listId; 88 } 89 90 104 public void addColumn(CmsListColumnDefinition listColumn) { 105 106 setListIdForColumn(listColumn); 107 if (m_columns.elementList().isEmpty()) { 108 listColumn.setPrintable(true); 109 } else { 110 listColumn.setPrintable(listColumn.isSorteable()); 111 } 112 m_columns.addIdentifiableObject(listColumn.getId(), listColumn); 113 } 114 115 130 public void addColumn(CmsListColumnDefinition listColumn, int position) { 131 132 setListIdForColumn(listColumn); 133 if (m_columns.elementList().isEmpty()) { 134 listColumn.setPrintable(true); 135 } else { 136 listColumn.setPrintable(listColumn.isSorteable()); 137 } 138 m_columns.addIdentifiableObject(listColumn.getId(), listColumn, position); 139 } 140 141 146 public void addIndependentAction(I_CmsListAction action) { 147 148 action.setListId(getListId()); 149 m_indepActions.addIdentifiableObject(action.getId(), action); 150 } 151 152 159 public void addItemDetails(CmsListItemDetails itemDetail) { 160 161 itemDetail.setListId(getListId()); 162 m_itemDetails.addIdentifiableObject(itemDetail.getId(), itemDetail); 163 } 164 165 173 public void addItemDetails(CmsListItemDetails itemDetail, int position) { 174 175 itemDetail.setListId(getListId()); 176 m_itemDetails.addIdentifiableObject(itemDetail.getId(), itemDetail, position); 177 } 178 179 186 public void addMultiAction(CmsListMultiAction multiAction) { 187 188 multiAction.setListId(getListId()); 189 m_multiActions.addIdentifiableObject(multiAction.getId(), multiAction); 190 } 191 192 197 public String csvEmptyList() { 198 199 StringBuffer html = new StringBuffer (512); 200 html.append("\n"); 201 return html.toString(); 202 } 203 204 211 public String csvHeader(CmsWorkplace wp) { 212 213 StringBuffer csv = new StringBuffer (1024); 214 Iterator itCols = m_columns.elementList().iterator(); 215 while (itCols.hasNext()) { 216 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 217 if (!col.isVisible()) { 218 continue; 219 } 220 csv.append(col.csvHeader(wp)); 221 csv.append("\t"); 222 } 223 csv.append("\n\n"); 224 return csv.toString(); 225 } 226 227 235 public String csvItem(CmsListItem item, CmsWorkplace wp) { 236 237 StringBuffer csv = new StringBuffer (1024); 238 Iterator itCols = m_columns.elementList().iterator(); 239 while (itCols.hasNext()) { 240 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 241 if (!col.isVisible()) { 242 continue; 243 } 244 csv.append(col.csvCell(item, wp)); 245 csv.append("\t"); 246 } 247 csv.append("\n"); 248 return csv.toString(); 249 } 250 251 258 public CmsListColumnDefinition getColumnDefinition(String columnId) { 259 260 return (CmsListColumnDefinition)m_columns.getObject(columnId); 261 } 262 263 268 public List getColumnDefinitions() { 269 270 return m_columns.elementList(); 271 } 272 273 280 public I_CmsListAction getIndependentAction(String actionId) { 281 282 return (I_CmsListAction)m_indepActions.getObject(actionId); 283 } 284 285 290 public List getIndependentActions() { 291 292 return m_indepActions.elementList(); 293 } 294 295 302 public CmsListItemDetails getItemDetailDefinition(String itemDetailId) { 303 304 return (CmsListItemDetails)m_itemDetails.getObject(itemDetailId); 305 } 306 307 312 public List getItemDetailDefinitions() { 313 314 return m_itemDetails.elementList(); 315 } 316 317 322 public String getListId() { 323 324 return m_listId; 325 } 326 327 334 public CmsListMultiAction getMultiAction(String actionId) { 335 336 return (CmsListMultiAction)m_multiActions.getObject(actionId); 337 } 338 339 344 public List getMultiActions() { 345 346 return m_multiActions.elementList(); 347 } 348 349 354 public CmsListSearchAction getSearchAction() { 355 356 return m_searchAction; 357 } 358 359 364 public int getWidth() { 365 366 return m_columns.elementList().size() + (hasCheckMultiActions() ? 1 : 0); 367 } 368 369 374 public boolean hasActions() { 375 376 return !m_indepActions.elementList().isEmpty(); 377 } 378 379 384 public boolean hasCheckMultiActions() { 385 386 Iterator it = m_multiActions.elementList().iterator(); 387 while (it.hasNext()) { 388 CmsListMultiAction action = (CmsListMultiAction)it.next(); 389 if (!(action instanceof CmsListRadioMultiAction)) { 390 return true; 391 } 392 } 393 return false; 394 } 395 396 401 public boolean hasMultiActions() { 402 403 return !m_multiActions.elementList().isEmpty(); 404 } 405 406 411 public boolean hasSingleActions() { 412 413 Iterator itCols = m_columns.elementList().iterator(); 414 while (itCols.hasNext()) { 415 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 416 if (!col.getDefaultActions().isEmpty() || !col.getDirectActions().isEmpty()) { 417 return true; 418 } 419 } 420 return false; 421 } 422 423 430 public String htmlActionBar(CmsWorkplace wp) { 431 432 StringBuffer html = new StringBuffer (1024); 433 html.append("<td class='misc'>\n"); 434 html.append("\t<div>\n"); 435 Iterator itDetails = m_itemDetails.elementList().iterator(); 436 while (itDetails.hasNext()) { 437 I_CmsListAction detailAction = ((CmsListItemDetails)itDetails.next()).getAction(); 438 html.append("\t\t"); 439 html.append(detailAction.buttonHtml(wp)); 440 if (itDetails.hasNext()) { 441 html.append(" "); 442 } 443 html.append("\n"); 444 } 445 Iterator itActions = m_indepActions.elementList().iterator(); 446 while (itActions.hasNext()) { 447 I_CmsListAction indepAction = (I_CmsListAction)itActions.next(); 448 html.append("\t\t"); 449 html.append(" "); 450 html.append(indepAction.buttonHtml(wp)); 451 html.append("\n"); 452 } 453 html.append("\t</div>\n"); 454 html.append("</td>\n"); 455 return html.toString(); 456 } 457 458 465 public String htmlEmptyTable(Locale locale) { 466 467 StringBuffer html = new StringBuffer (512); 468 html.append("<tr class='oddrowbg'>\n"); 469 html.append("\t<td align='center' colspan='"); 470 html.append(getWidth()); 471 html.append("'>\n"); 472 html.append(Messages.get().getBundle(locale).key(Messages.GUI_LIST_EMPTY_0)); 473 html.append("\t</td>\n"); 474 html.append("</tr>\n"); 475 return html.toString(); 476 } 477 478 486 public String htmlHeader(CmsHtmlList list, CmsWorkplace wp) { 487 488 StringBuffer html = new StringBuffer (1024); 489 html.append("<tr>\n"); 490 Iterator itCols = m_columns.elementList().iterator(); 491 while (itCols.hasNext()) { 492 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 493 if (!list.isPrintable() || col.isPrintable()) { 494 html.append(col.htmlHeader(list, wp)); 495 } 496 } 497 if (!list.isPrintable() && hasCheckMultiActions()) { 498 html.append("\t<th width='0' class='select'>\n"); 499 html.append("\t\t<input type='checkbox' class='checkbox' name='listSelectAll' value='true' onClick=\"listSelect('"); 500 html.append(list.getId()); 501 html.append("')\">\n"); 502 html.append("\t</th>\n"); 503 } 504 html.append("</tr>\n"); 505 return html.toString(); 506 } 507 508 518 public String htmlItem(CmsListItem item, CmsWorkplace wp, boolean odd, boolean isPrintable) { 519 520 StringBuffer html = new StringBuffer (1024); 521 html.append("<tr "); 522 if (!isPrintable) { 523 html.append("class='"); 524 html.append(odd ? "oddrowbg" : "evenrowbg"); 525 html.append("'"); 526 } 527 html.append(">\n"); 528 Iterator itCols = m_columns.elementList().iterator(); 529 int width = 0; 530 while (itCols.hasNext()) { 531 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 532 if (!col.isVisible() || (isPrintable && !col.isPrintable())) { 533 continue; 534 } 535 width++; 536 StringBuffer style = new StringBuffer (64); 537 html.append("<td"); 538 CmsListColumnAlignEnum align = col.getAlign(); 539 if (align != CmsListColumnAlignEnum.ALIGN_LEFT && CmsStringUtil.isNotEmpty(align.toString())) { 540 style.append("text-align: "); 541 style.append(col.getAlign()); 542 style.append("; "); 543 } 544 if (col.isTextWrapping()) { 545 style.append("white-space: normal;"); 546 } 547 if (isPrintable) { 548 style.append("border-top: 1px solid black;"); 549 } 550 if (style.length() > 0) { 551 html.append(" style='"); 552 html.append(style); 553 html.append("'"); 554 } 555 html.append(">\n"); 556 html.append(col.htmlCell(item, wp, isPrintable)); 557 html.append("</td>\n"); 558 } 559 if (!isPrintable && hasCheckMultiActions()) { 560 width++; 561 html.append("\t<td class='select' align='center'>\n"); 562 html.append("\t\t<input type='checkbox' class='checkbox' name='listMultiAction' value='"); 563 html.append(item.getId()); 564 html.append("'>\n"); 565 html.append("\t</td>\n"); 566 } 567 html.append("</tr>\n"); 568 569 Iterator itDet = m_itemDetails.elementList().iterator(); 570 while (itDet.hasNext()) { 571 CmsListItemDetails lid = (CmsListItemDetails)itDet.next(); 572 if (lid.isVisible() 573 && item.get(lid.getId()) != null 574 && CmsStringUtil.isNotEmptyOrWhitespaceOnly(item.get(lid.getId()).toString())) { 575 int padCols = 0; 576 itCols = m_columns.elementList().iterator(); 577 while (itCols.hasNext()) { 578 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 579 if (!col.isVisible() || (isPrintable && !col.isPrintable())) { 580 continue; 581 } 582 if (col.getId().equals(lid.getAtColumn())) { 583 break; 584 } 585 padCols++; 586 } 587 int spanCols = width - padCols; 588 589 html.append("<tr "); 590 if (!isPrintable) { 591 html.append("class='"); 592 html.append(odd ? "oddrowbg" : "evenrowbg"); 593 html.append("'"); 594 } 595 html.append(">\n"); 596 if (padCols > 0) { 597 html.append("<td colspan='"); 598 html.append(padCols); 599 html.append("'> </td>\n"); 600 } 601 html.append("<td colspan='"); 602 html.append(spanCols); 603 html.append("' style='padding-left: 20px; white-space:normal;'>\n"); 604 html.append(lid.htmlCell(item, wp, isPrintable)); 605 html.append("\n</td>\n"); 606 html.append("\n"); 607 html.append("</tr>\n"); 608 } 609 } 610 return html.toString(); 611 } 612 613 620 public String htmlMultiActionBar(CmsWorkplace wp) { 621 622 StringBuffer html = new StringBuffer (1024); 623 html.append("<td class='misc'>\n"); 624 html.append("\t<div>\n"); 625 Iterator itActions = m_multiActions.elementList().iterator(); 626 while (itActions.hasNext()) { 627 CmsListMultiAction multiAction = (CmsListMultiAction)itActions.next(); 628 html.append("\t\t"); 629 html.append(multiAction.buttonHtml(wp)); 630 if (itActions.hasNext()) { 631 html.append(" "); 632 } 633 html.append("\n"); 634 } 635 html.append("\t</div>\n"); 636 html.append("</td>\n"); 637 return html.toString(); 638 } 639 640 647 public String htmlSearchBar(CmsWorkplace wp) { 648 649 if (!isSearchable()) { 650 return ""; 651 } 652 StringBuffer html = new StringBuffer (1024); 653 html.append("<td class='main'>\n"); 654 html.append("\t<div>\n"); 655 html.append("\t\t<input type='text' name='listSearchFilter' id='" + SEARCH_BAR_INPUT_ID + "' value='' size='20' maxlength='245' style='vertical-align: bottom;'>\n"); 656 html.append(m_searchAction.buttonHtml(wp)); 657 I_CmsListAction showAllAction = m_searchAction.getShowAllAction(); 658 if (showAllAction != null) { 659 html.append(" "); 660 html.append(showAllAction.buttonHtml(wp)); 661 } 662 html.append("\t</div>\n"); 663 html.append("</td>\n"); 664 return html.toString(); 665 } 666 667 672 public boolean isSearchable() { 673 674 return m_searchAction != null; 675 } 676 677 682 public boolean isSorteable() { 683 684 Iterator itCols = m_columns.elementList().iterator(); 685 while (itCols.hasNext()) { 686 CmsListColumnDefinition col = (CmsListColumnDefinition)itCols.next(); 687 if (col.isSorteable()) { 688 return true; 689 } 690 } 691 return false; 692 } 693 694 699 public boolean isVolatile() { 700 701 return m_volatile; 702 } 703 704 709 public void setSearchAction(CmsListSearchAction searchAction) { 710 711 m_searchAction = searchAction; 712 m_searchAction.setListId(getListId()); 713 } 714 715 720 public void setVolatile(boolean volatileFlag) { 721 722 m_volatile = volatileFlag; 723 } 724 725 731 public void toogleDetailState(String itemDetailId) { 732 733 CmsListItemDetails lid = (CmsListItemDetails)m_itemDetails.getObject(itemDetailId); 734 lid.setVisible(!lid.isVisible()); 735 } 736 737 750 void checkIds() { 751 752 Set ids = new TreeSet (); 753 Iterator itIndepActions = getIndependentActions().iterator(); 755 while (itIndepActions.hasNext()) { 756 String id = ((CmsListIndependentAction)itIndepActions.next()).getId(); 757 if (ids.contains(id)) { 758 throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_DUPLICATED_ID_1, id)); 759 } 760 ids.add(id); 761 } 762 Iterator itMultiActions = getMultiActions().iterator(); 764 while (itMultiActions.hasNext()) { 765 String id = ((CmsListMultiAction)itMultiActions.next()).getId(); 766 if (ids.contains(id)) { 767 throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_DUPLICATED_ID_1, id)); 768 } 769 ids.add(id); 770 } 771 Iterator itItemDetails = getItemDetailDefinitions().iterator(); 773 while (itItemDetails.hasNext()) { 774 String id = ((CmsListItemDetails)itItemDetails.next()).getId(); 775 if (ids.contains(id)) { 776 throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_DUPLICATED_ID_1, id)); 777 } 778 ids.add(id); 779 } 780 Iterator itColumns = getColumnDefinitions().iterator(); 782 while (itColumns.hasNext()) { 783 CmsListColumnDefinition col = (CmsListColumnDefinition)itColumns.next(); 784 if (ids.contains(col.getId())) { 785 throw new CmsIllegalStateException(Messages.get().container(Messages.ERR_DUPLICATED_ID_1, col.getId())); 786 } 787 ids.add(col.getId()); 788 Iterator itDefaultActions = col.getDefaultActions().iterator(); 790 while (itDefaultActions.hasNext()) { 791 CmsListDefaultAction action = (CmsListDefaultAction)itDefaultActions.next(); 792 if (ids.contains(action.getId())) { 793 throw new CmsIllegalStateException(Messages.get().container( 794 Messages.ERR_DUPLICATED_ID_1, 795 action.getId())); 796 } 797 ids.add(action.getId()); 798 } 799 Iterator itDirectActions = col.getDirectActions().iterator(); 801 while (itDirectActions.hasNext()) { 802 CmsListDirectAction action = (CmsListDirectAction)itDirectActions.next(); 803 if (ids.contains(action.getId())) { 804 throw new CmsIllegalStateException(Messages.get().container( 805 Messages.ERR_DUPLICATED_ID_1, 806 action.getId())); 807 } 808 ids.add(action.getId()); 809 } 810 } 811 } 812 813 818 private void setListIdForColumn(CmsListColumnDefinition col) { 819 820 col.setListId(getListId()); 821 Iterator itDefaultActions = col.getDefaultActions().iterator(); 823 while (itDefaultActions.hasNext()) { 824 ((CmsListDefaultAction)itDefaultActions.next()).setListId(getListId()); 825 } 826 Iterator itDirectActions = col.getDirectActions().iterator(); 828 while (itDirectActions.hasNext()) { 829 ((CmsListDirectAction)itDirectActions.next()).setListId(getListId()); 830 } 831 } 832 } | Popular Tags |