1 31 32 package org.opencms.workplace.list; 33 34 import org.opencms.i18n.CmsMessageContainer; 35 import org.opencms.i18n.CmsMessages; 36 import org.opencms.main.CmsIllegalArgumentException; 37 import org.opencms.util.CmsStringUtil; 38 import org.opencms.workplace.CmsWorkplace; 39 import org.opencms.workplace.tools.A_CmsHtmlIconButton; 40 import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum; 41 42 import java.text.MessageFormat ; 43 import java.util.ArrayList ; 44 import java.util.Collections ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.Locale ; 48 49 58 public class CmsListColumnDefinition { 59 60 61 public static final String ICON_DOWN = "list/arrow_down.png"; 62 63 64 public static final String ICON_UP = "list/arrow_up.png"; 65 66 67 private CmsListColumnAlignEnum m_align = CmsListColumnAlignEnum.ALIGN_LEFT; 68 69 70 private I_CmsListItemComparator m_comparator = new CmsListItemDefaultComparator(); 71 72 73 private List m_defaultActions = new ArrayList (); 74 75 76 private List m_directActions = new ArrayList (); 77 78 79 private I_CmsListFormatter m_formatter; 80 81 82 private CmsMessageContainer m_helpText; 83 84 85 private final String m_id; 86 87 88 private String m_listId; 89 90 91 private CmsMessageContainer m_name; 92 93 94 private boolean m_printable = true; 95 96 97 private boolean m_textWrapping = false; 98 99 100 private boolean m_visible = true; 101 102 103 private String m_width; 104 105 110 public CmsListColumnDefinition(String id) { 111 112 if (CmsStringUtil.isEmptyOrWhitespaceOnly(id)) { 113 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_LIST_INVALID_NULL_ARG_1, "id")); 114 } 115 m_id = id; 116 } 117 118 125 public void addDefaultAction(CmsListDefaultAction defaultAction) { 126 127 if (m_listId != null) { 128 defaultAction.setListId(m_listId); 130 } 131 defaultAction.setColumnForLink(getId()); 133 134 m_defaultActions.add(defaultAction); 135 } 136 137 142 public void addDirectAction(I_CmsListDirectAction listAction) { 143 144 if (m_listId != null) { 145 listAction.setListId(m_listId); 146 } 147 m_directActions.add(listAction); 148 } 149 150 158 public String csvCell(CmsListItem item, CmsWorkplace wp) { 159 160 if (!isVisible()) { 161 return ""; 162 } 163 StringBuffer csv = new StringBuffer (512); 164 if (m_formatter == null) { 165 if (item.get(m_id) != null) { 167 csv.append(item.get(m_id).toString()); 169 } else { 170 Iterator itActions = m_directActions.iterator(); 171 while (itActions.hasNext()) { 172 I_CmsListDirectAction action = (I_CmsListDirectAction)itActions.next(); 173 if (action.isVisible()) { 174 action.setItem(item); 175 csv.append(action.getName().key(wp.getLocale())); 176 } 177 } 178 } 179 } else { 180 csv.append(m_formatter.format(item.get(m_id), wp.getLocale())); 182 } 183 return csv.toString(); 184 } 185 186 193 public String csvHeader(CmsWorkplace wp) { 194 195 if (!isVisible()) { 196 return ""; 197 } 198 return getName().key(wp.getLocale()); 199 } 200 201 206 public CmsListColumnAlignEnum getAlign() { 207 208 return m_align; 209 } 210 211 218 public CmsListDefaultAction getDefaultAction(String actionId) { 219 220 Iterator it = m_defaultActions.iterator(); 221 while (it.hasNext()) { 222 CmsListDefaultAction action = (CmsListDefaultAction)it.next(); 223 if (action.getId().equals(actionId)) { 224 return action; 225 } 226 } 227 return null; 228 } 229 230 235 public List getDefaultActionIds() { 236 237 List ids = new ArrayList (); 238 Iterator itDefActions = m_defaultActions.iterator(); 239 while (itDefActions.hasNext()) { 240 I_CmsListDirectAction action = (I_CmsListDirectAction)itDefActions.next(); 241 ids.add(action.getId()); 242 } 243 return Collections.unmodifiableList(ids); 244 } 245 246 251 public List getDefaultActions() { 252 253 return Collections.unmodifiableList(m_defaultActions); 254 } 255 256 263 public I_CmsListDirectAction getDirectAction(String actionId) { 264 265 Iterator it = m_directActions.iterator(); 266 while (it.hasNext()) { 267 I_CmsListDirectAction action = (I_CmsListDirectAction)it.next(); 268 if (action.getId().equals(actionId)) { 269 return action; 270 } 271 } 272 return null; 273 } 274 275 280 public List getDirectActionIds() { 281 282 List ids = new ArrayList (); 283 Iterator itDirActions = m_directActions.iterator(); 284 while (itDirActions.hasNext()) { 285 I_CmsListDirectAction action = (I_CmsListDirectAction)itDirActions.next(); 286 ids.add(action.getId()); 287 } 288 return Collections.unmodifiableList(ids); 289 } 290 291 296 public List getDirectActions() { 297 298 return Collections.unmodifiableList(m_directActions); 299 } 300 301 306 public I_CmsListFormatter getFormatter() { 307 308 return m_formatter; 309 } 310 311 318 public CmsMessageContainer getHelpText() { 319 320 return m_helpText; 321 } 322 323 328 public String getId() { 329 330 return m_id; 331 } 332 333 342 public I_CmsListItemComparator getListItemComparator() { 343 344 return m_comparator; 345 } 346 347 352 public CmsMessageContainer getName() { 353 354 return m_name; 355 } 356 357 362 public String getWidth() { 363 364 return m_width; 365 } 366 367 376 public String htmlCell(CmsListItem item, CmsWorkplace wp, boolean isPrintable) { 377 378 StringBuffer html = new StringBuffer (512); 379 Iterator itActions = m_directActions.iterator(); 380 while (itActions.hasNext()) { 381 I_CmsListDirectAction action = (I_CmsListDirectAction)itActions.next(); 382 action.setItem(item); 383 boolean enabled = action.isEnabled(); 384 if (isPrintable) { 385 action.setEnabled(false); 386 } 387 html.append(action.buttonHtml(wp)); 388 if (isPrintable) { 389 action.setEnabled(enabled); 390 } 391 } 392 if (!m_defaultActions.isEmpty()) { 393 Iterator itDefaultActions = m_defaultActions.iterator(); 394 while (itDefaultActions.hasNext()) { 395 I_CmsListDirectAction defAction = (I_CmsListDirectAction)itDefaultActions.next(); 396 defAction.setItem(item); 397 boolean enabled = defAction.isEnabled(); 398 if (isPrintable) { 399 defAction.setEnabled(false); 400 } 401 html.append(defAction.buttonHtml(wp)); 402 if (isPrintable) { 403 defAction.setEnabled(enabled); 404 } 405 } 406 } else { 407 if (m_formatter == null) { 408 if (item.get(m_id) != null) { 410 html.append(item.get(m_id).toString()); 412 } 413 } else { 414 html.append(m_formatter.format(item.get(m_id), wp.getLocale())); 416 } 417 } 418 html.append("\n"); 419 return html.toString(); 420 } 421 422 430 public String htmlHeader(CmsHtmlList list, CmsWorkplace wp) { 431 432 if (!isVisible()) { 433 return ""; 434 } 435 436 String listId = list.getId(); 437 String sortedCol = list.getSortedColumn(); 438 CmsListOrderEnum order = list.getCurrentSortOrder(); 439 440 StringBuffer html = new StringBuffer (512); 441 Locale locale = wp.getLocale(); 442 CmsMessages messages = Messages.get().getBundle(locale); 443 html.append("<th"); 444 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth())) { 445 html.append(" width='"); 446 html.append(getWidth()); 447 html.append("'"); 448 } 449 if (!isTextWrapping()) { 450 html.append(" style='white-space: nowrap;'"); 451 } 452 html.append(">\n"); 453 454 boolean isSorted = getId().equals(sortedCol); 455 CmsListOrderEnum nextOrder = CmsListOrderEnum.ORDER_ASCENDING; 456 if (isSorted && order == CmsListOrderEnum.ORDER_ASCENDING) { 457 nextOrder = CmsListOrderEnum.ORDER_DESCENDING; 458 } 459 String id = listId + getId() + "Sort"; 461 String onClic = "listSort('" + listId + "', '" + getId() + "');"; 462 String helpText = null; 463 if (m_helpText != null) { 464 helpText = new MessageFormat (m_helpText.key(locale), locale).format(new Object [] {getName().key( 465 locale)}); 466 } else { 467 if (isSorteable()) { 468 if (nextOrder.equals(CmsListOrderEnum.ORDER_ASCENDING)) { 469 helpText = messages.key(Messages.GUI_LIST_COLUMN_ASC_SORT_1, new Object [] {getName().key( 470 locale)}); 471 } else { 472 helpText = messages.key(Messages.GUI_LIST_COLUMN_DESC_SORT_1, new Object [] {getName().key( 473 locale)}); 474 } 475 } else { 476 helpText = messages.key( 477 Messages.GUI_LIST_COLUMN_NO_SORT_1, 478 new Object [] {getName().key(locale)}); 479 } 480 } 481 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth()) && getWidth().indexOf('%') < 0) { 482 html.append("\t<div style='display:block; width: "); 483 html.append(getWidth()); 484 html.append("px;'>\n"); 485 } 486 html.append(A_CmsHtmlIconButton.defaultButtonHtml( 487 wp.getJsp(), 488 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, 489 id, 490 getName().key(locale), 491 helpText, 492 list.isPrintable() ? false : isSorteable(), 493 null, 494 null, 495 onClic)); 496 if (isSorted) { 498 if (nextOrder == CmsListOrderEnum.ORDER_ASCENDING) { 499 html.append("\t\t<img SRC='"); 500 html.append(CmsWorkplace.getSkinUri()); 501 html.append(ICON_UP); 502 html.append("' alt=''>\n"); 503 } else { 504 html.append("\t\t<img SRC='"); 505 html.append(CmsWorkplace.getSkinUri()); 506 html.append(ICON_DOWN); 507 html.append("' alt=''>\n"); 508 } 509 } 510 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getWidth()) && getWidth().indexOf('%') < 0) { 511 html.append("\t</div>\n"); 512 } 513 html.append("</th>\n"); 514 return html.toString(); 515 } 516 517 522 public boolean isPrintable() { 523 524 return m_printable; 525 } 526 527 532 public boolean isSorteable() { 533 534 return getListItemComparator() != null; 535 } 536 537 542 public boolean isTextWrapping() { 543 544 return m_textWrapping; 545 } 546 547 552 public boolean isVisible() { 553 554 return m_visible; 555 } 556 557 562 public void setAlign(CmsListColumnAlignEnum align) { 563 564 m_align = align; 565 } 566 567 572 public void setFormatter(I_CmsListFormatter formatter) { 573 574 m_formatter = formatter; 575 } 576 577 584 public void setHelpText(CmsMessageContainer helpText) { 585 586 m_helpText = helpText; 587 } 588 589 594 public void setListItemComparator(I_CmsListItemComparator comparator) { 595 596 m_comparator = comparator; 597 } 598 599 604 public void setName(CmsMessageContainer name) { 605 606 m_name = name; 607 } 608 609 614 public void setPrintable(boolean printable) { 615 616 m_printable = printable; 617 } 618 619 628 public void setSorteable(boolean sorteable) { 629 630 if (sorteable) { 631 setListItemComparator(new CmsListItemDefaultComparator()); 632 } else { 633 setListItemComparator(null); 634 } 635 } 636 637 642 public void setTextWrapping(boolean textWrapping) { 643 644 m_textWrapping = textWrapping; 645 } 646 647 652 public void setVisible(boolean visible) { 653 654 m_visible = visible; 655 } 656 657 662 public void setWidth(String width) { 663 664 m_width = width; 665 } 666 667 672 void setListId(String listId) { 673 674 m_listId = listId; 675 } 676 } | Popular Tags |