1 17 package org.alfresco.web.ui.common.renderer.data; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.component.UIComponent; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 29 import org.alfresco.web.ui.common.Utils; 30 import org.alfresco.web.ui.common.component.data.UIColumn; 31 import org.alfresco.web.ui.common.component.data.UIRichList; 32 import org.alfresco.web.ui.common.renderer.BaseRenderer; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 39 public class RichListRenderer extends BaseRenderer 40 { 41 44 47 public void encodeBegin(FacesContext context, UIComponent component) 48 throws IOException 49 { 50 if (component.isRendered() == true) 52 { 53 ResponseWriter out = context.getResponseWriter(); 54 Map attrs = component.getAttributes(); 55 out.write("<table cellspacing=0 cellpadding=0"); 56 outputAttribute(out, attrs.get("styleClass"), "class"); 57 outputAttribute(out, attrs.get("style"), "style"); 58 outputAttribute(out, attrs.get("width"), "width"); 59 out.write(">"); 60 } 61 } 62 63 66 public void encodeChildren(FacesContext context, UIComponent component) 67 throws IOException 68 { 69 if (component.isRendered() == true) 70 { 71 UIRichList richList = (UIRichList)component; 73 74 richList.bind(); 76 77 List <UIColumn> columnList = new ArrayList <UIColumn>(8); 79 for (Iterator i=richList.getChildren().iterator(); i.hasNext(); ) 80 { 81 UIComponent child = (UIComponent)i.next(); 82 if (child instanceof UIColumn) 83 { 84 columnList.add((UIColumn)child); 85 } 86 } 87 88 UIColumn[] columns = new UIColumn[columnList.size()]; 89 columnList.toArray(columns); 90 91 IRichListRenderer renderer = (IRichListRenderer)richList.getViewRenderer(); 93 if (renderer == null) 94 { 95 throw new IllegalStateException ("IRichListRenderer must be available in UIRichList!"); 96 } 97 98 ResponseWriter out = context.getResponseWriter(); 100 out.write("<thead>"); 101 renderer.renderListBefore(context, richList, columns); 102 out.write("</thead>"); 103 out.write("<tbody>"); 104 if (richList.isDataAvailable() == true) 105 { 106 while (richList.isDataAvailable() == true) 107 { 108 renderer.renderListRow(context, richList, columns, richList.nextRow()); 110 } 111 } 112 else 113 { 114 UIComponent emptyComponent = richList.getEmptyMessage(); 116 if (emptyComponent != null) 117 { 118 emptyComponent.encodeBegin(context); 119 emptyComponent.encodeChildren(context); 120 emptyComponent.encodeEnd(context); 121 } 122 } 123 renderer.renderListAfter(context, richList, columns); 125 out.write("</tbody>"); 126 } 127 } 128 129 132 public void encodeEnd(FacesContext context, UIComponent component) 133 throws IOException 134 { 135 if (component.isRendered() == true) 137 { 138 ResponseWriter out = context.getResponseWriter(); 139 out.write("</table>"); 140 } 141 } 142 143 146 public boolean getRendersChildren() 147 { 148 return true; 153 } 154 155 156 159 164 public static class DetailsViewRenderer implements IRichListRenderer 165 { 166 public static final String VIEWMODEID = "details"; 167 168 171 public String getViewModeID() 172 { 173 return VIEWMODEID; 174 } 175 176 179 public void renderListBefore(FacesContext context, UIRichList richList, UIColumn[] columns) 180 throws IOException 181 { 182 ResponseWriter out = context.getResponseWriter(); 183 184 out.write("<tr"); 186 outputAttribute(out, richList.getAttributes().get("headerStyleClass"), "class"); 187 out.write('>'); 188 for (int i=0; i<columns.length; i++) 189 { 190 UIColumn column = columns[i]; 191 192 if (column.isRendered() == true) 193 { 194 out.write("<th"); 196 outputAttribute(out, column.getAttributes().get("width"), "width"); 197 outputAttribute(out, column.getAttributes().get("style"), "style"); 198 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 199 out.write('>'); 200 201 UIComponent header = column.getHeader(); 203 if (header != null) 204 { 205 header.encodeBegin(context); 206 if (header.getRendersChildren()) 207 { 208 header.encodeChildren(context); 209 } 210 header.encodeEnd(context); 211 } 212 213 out.write("</th>"); 215 } 216 } 217 out.write("</tr>"); 218 219 this.rowIndex = 0; 220 } 221 222 225 public void renderListRow(FacesContext context, UIRichList richList, UIColumn[] columns, Object row) 226 throws IOException 227 { 228 ResponseWriter out = context.getResponseWriter(); 229 230 out.write("<tr"); 232 String rowStyle = (String )richList.getAttributes().get("rowStyleClass"); 233 String altStyle = (String )richList.getAttributes().get("altRowStyleClass"); 234 if (altStyle != null && (this.rowIndex++ & 1) == 1) 235 { 236 rowStyle = altStyle; 237 } 238 outputAttribute(out, rowStyle, "class"); 239 out.write('>'); 240 241 UIColumn actionsColumn = null; 243 for (int i=0; i<columns.length; i++) 244 { 245 if (columns[i].isRendered() == true && columns[i].getActions() == true) 246 { 247 actionsColumn = columns[i]; 248 break; 249 } 250 } 251 252 boolean renderedFirst = false; 254 for (int i=0; i<columns.length; i++) 255 { 256 UIColumn column = columns[i]; 257 258 if (column.isRendered() == true) 259 { 260 out.write("<td"); 261 outputAttribute(out, column.getAttributes().get("style"), "style"); 262 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 263 out.write('>'); 264 265 if (renderedFirst == false) 267 { 268 UIComponent smallIcon = column.getSmallIcon(); 269 if (smallIcon != null) 270 { 271 smallIcon.encodeBegin(context); 272 if (smallIcon.getRendersChildren()) 273 { 274 smallIcon.encodeChildren(context); 275 } 276 smallIcon.encodeEnd(context); 277 out.write(" "); 278 } 279 renderedFirst = true; 280 } 281 282 if (column.getChildCount() != 0) 283 { 284 if (column == actionsColumn) 285 { 286 out.write("<nobr>"); 287 } 288 289 Utils.encodeRecursive(context, column); 291 292 if (column == actionsColumn) 293 { 294 out.write("</nobr>"); 295 } 296 } 297 298 out.write("</td>"); 299 } 300 } 301 out.write("</tr>"); 302 } 303 304 307 public void renderListAfter(FacesContext context, UIRichList richList, UIColumn[] columns) 308 throws IOException 309 { 310 ResponseWriter out = context.getResponseWriter(); 311 312 out.write("<tr><td colspan=99 align=right>"); 313 for (Iterator i=richList.getChildren().iterator(); i.hasNext(); ) 314 { 315 UIComponent child = (UIComponent)i.next(); 317 if (child instanceof UIColumn == false) 318 { 319 Utils.encodeRecursive(context, child); 320 } 321 } 322 out.write("</td></tr>"); 323 } 324 325 private int rowIndex = 0; 326 } 327 328 329 334 public static class ListViewRenderer implements IRichListRenderer 335 { 336 private final static int MAX_DISPLAYABLE_LINES = 3; 338 339 private final static String END_ROW_SEPARATOR = "</tr><tr><td colspan=10><div style='padding:3px'></div></td></tr>"; 340 private final static String COLUMN_SPACER = "<td><div style='padding-left:8px'></div></td>"; 341 342 public static final String VIEWMODEID = "list"; 343 344 347 public String getViewModeID() 348 { 349 return VIEWMODEID; 350 } 351 352 355 public void renderListBefore(FacesContext context, UIRichList richList, UIColumn[] columns) 356 throws IOException 357 { 358 360 393 394 this.rowIndex = 0; 395 } 396 397 400 public void renderListRow(FacesContext context, UIRichList richList, UIColumn[] columns, Object row) throws IOException 401 { 402 ResponseWriter out = context.getResponseWriter(); 403 404 String rowStyle = (String )richList.getAttributes().get("rowStyleClass"); 407 if ((this.rowIndex & 1) == 0) 408 { 409 out.write("<tr"); 410 outputAttribute(out, rowStyle, "class"); 411 out.write('>'); 412 } 413 414 UIColumn primaryColumn = null; 417 UIColumn actionsColumn = null; 418 for (int i=0; i<columns.length; i++) 419 { 420 if (columns[i].isRendered() == true) 421 { 422 if (columns[i].getPrimary() == true) 423 { 424 primaryColumn = columns[i]; 425 } 426 else if (columns[i].getActions() == true) 427 { 428 actionsColumn = columns[i]; 429 } 430 } 431 } 432 if (primaryColumn == null) 433 { 434 logger.warn("No primary column found for RichList definition: " + richList.getId()); 435 } 436 437 out.write("<td width=50%><table cellspacing=0 cellpadding=2 border=0>"); 439 if (primaryColumn != null) 440 { 441 UIColumn column = primaryColumn; 442 443 if (column.isRendered() == true) 444 { 445 out.write("<tr><td rowspan=10"); 446 outputAttribute(out, column.getAttributes().get("style"), "style"); 447 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 448 out.write('>'); 449 450 UIComponent icon = column.getLargeIcon(); 452 if (icon != null) 453 { 454 icon.encodeBegin(context); 455 if (icon.getRendersChildren()) 456 { 457 icon.encodeChildren(context); 458 } 459 icon.encodeEnd(context); 460 } 461 out.write("</td>"); 462 463 out.write("<td width=100%"); 465 outputAttribute(out, column.getAttributes().get("style"), "style"); 466 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 467 out.write('>'); 468 if (column.getChildCount() != 0) 469 { 470 Utils.encodeRecursive(context, column); 472 } 473 out.write("</td>"); 474 475 if (actionsColumn != null) 477 { 478 out.write("<td"); 479 outputAttribute(out, actionsColumn.getAttributes().get("style"), "style"); 480 outputAttribute(out, actionsColumn.getAttributes().get("styleClass"), "class"); 481 out.write("><nobr>"); 482 483 if (actionsColumn.getChildCount() != 0) 484 { 485 Utils.encodeRecursive(context, actionsColumn); 487 } 488 out.write("</nobr></td>"); 489 } 490 491 out.write("</tr>"); 492 } 493 } 494 495 for (int i = 0; i < columns.length; i++) 497 { 498 UIColumn column = columns[i]; 499 500 int count = 1; 501 if (column.isRendered() == true && count < MAX_DISPLAYABLE_LINES && 502 column.getActions() == false && column.getPrimary() == false) 503 { 504 out.write("<tr valign=top"); 506 outputAttribute(out, rowStyle, "class"); 507 out.write("><td colspan=2"); outputAttribute(out, column.getAttributes().get("style"), "style"); 509 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 510 out.write('>'); 511 if (column.getChildCount() != 0) 512 { 513 Utils.encodeRecursive(context, column); 515 } 516 out.write("</td></tr>"); 518 519 count++; 520 } 521 } 522 523 out.write("</table></td>"); 524 525 if ((this.rowIndex & 1) == 1) 526 { 527 out.write(END_ROW_SEPARATOR); 529 } 530 else 531 { 532 out.write(COLUMN_SPACER); 533 } 534 535 this.rowIndex++; 536 } 537 538 541 public void renderListAfter(FacesContext context, UIRichList richList, UIColumn[] columns) 542 throws IOException 543 { 544 ResponseWriter out = context.getResponseWriter(); 545 546 if ( ((this.rowIndex-1) & 1) != 1) 548 { 549 out.write(END_ROW_SEPARATOR); 550 } 551 552 out.write("<tr><td colspan=99 align=right>"); 553 for (Iterator i=richList.getChildren().iterator(); i.hasNext(); ) 554 { 555 UIComponent child = (UIComponent)i.next(); 557 if (child instanceof UIColumn == false) 558 { 559 Utils.encodeRecursive(context, child); 560 } 561 } 562 out.write("</td></tr>"); 563 } 564 565 private int rowIndex = 0; 566 } 567 568 569 574 public static class IconViewRenderer implements IRichListRenderer 575 { 576 private final static int COLUMNS = 3; 578 579 private final static String COLUMN_PERCENT = Integer.toString(100/COLUMNS) + "%"; 581 582 private final static int MAX_DISPLAYABLE_LINES = 3; 584 585 private final static String END_ROW_SEPARATOR = "</tr><tr><td colspan=10><div style='padding:3px'></div></td></tr>"; 586 587 public static final String VIEWMODEID = "icons"; 588 589 592 public String getViewModeID() 593 { 594 return VIEWMODEID; 595 } 596 597 600 public void renderListBefore(FacesContext context, UIRichList richList, UIColumn[] columns) 601 throws IOException 602 { 603 this.rowIndex = 0; 605 } 606 607 610 public void renderListRow(FacesContext context, UIRichList richList, UIColumn[] columns, Object row) 611 throws IOException 612 { 613 ResponseWriter out = context.getResponseWriter(); 614 615 if (this.rowIndex % COLUMNS == 0) 617 { 618 out.write("<tr"); 619 outputAttribute(out, richList.getAttributes().get("rowStyleClass"), "class"); 620 out.write('>'); 621 } 622 623 UIColumn primaryColumn = null; 625 for (int i=0; i<columns.length; i++) 626 { 627 if (columns[i].isRendered() == true && columns[i].getPrimary() == true) 628 { 629 primaryColumn = columns[i]; 630 break; 631 } 632 } 633 if (primaryColumn == null) 634 { 635 logger.warn("No primary column found for RichList definition: " + richList.getId()); 636 } 637 638 out.write("<td width="); 640 out.write(COLUMN_PERCENT); 641 out.write("><table cellspacing=0 cellpadding=2 border=0>"); 642 if (primaryColumn != null) 643 { 644 UIColumn column = primaryColumn; 645 646 if (column.isRendered() == true) 647 { 648 out.write("<tr><td rowspan=10"); 649 outputAttribute(out, column.getAttributes().get("style"), "style"); 650 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 651 out.write('>'); 652 653 UIComponent largeIcon = column.getLargeIcon(); 655 if (largeIcon != null) 656 { 657 largeIcon.encodeBegin(context); 658 if (largeIcon.getRendersChildren()) 659 { 660 largeIcon.encodeChildren(context); 661 } 662 largeIcon.encodeEnd(context); 663 } 664 out.write("</td>"); 665 666 out.write("<td"); 668 outputAttribute(out, column.getAttributes().get("style"), "style"); 669 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 670 out.write('>'); 671 if (column.getChildCount() != 0) 672 { 673 Utils.encodeRecursive(context, column); 675 } 676 out.write("</td></tr>"); 677 } 678 } 679 680 for (int i=0; i<columns.length; i++) 682 { 683 UIColumn column = columns[i]; 684 685 int count = 1; 686 if (column.isRendered() == true && column.getPrimary() == false && 687 (count < MAX_DISPLAYABLE_LINES || column.getActions() == true) ) 688 { 689 out.write("<tr><td"); 690 outputAttribute(out, column.getAttributes().get("style"), "style"); 691 outputAttribute(out, column.getAttributes().get("styleClass"), "class"); 692 out.write('>'); 693 if (column.getChildCount() != 0) 694 { 695 Utils.encodeRecursive(context, column); 697 } 698 out.write("</td></tr>"); 699 700 count++; 701 } 702 } 703 704 out.write("</table></td>"); 705 706 if (this.rowIndex % COLUMNS == COLUMNS-1) 707 { 708 out.write(END_ROW_SEPARATOR); 710 } 711 712 this.rowIndex++; 713 } 714 715 718 public void renderListAfter(FacesContext context, UIRichList richList, UIColumn[] columns) 719 throws IOException 720 { 721 ResponseWriter out = context.getResponseWriter(); 722 723 if ((this.rowIndex-1) % COLUMNS != COLUMNS-1) 725 { 726 out.write(END_ROW_SEPARATOR); 727 } 728 729 out.write("<tr><td colspan=99 align=right>"); 730 for (Iterator i=richList.getChildren().iterator(); i.hasNext(); ) 731 { 732 UIComponent child = (UIComponent)i.next(); 734 if (child instanceof UIColumn == false) 735 { 736 Utils.encodeRecursive(context, child); 737 } 738 } 739 out.write("</td></tr>"); 740 } 741 742 private int rowIndex = 0; 743 } 744 745 746 private static Log logger = LogFactory.getLog(RichListRenderer.class); 747 } 748 | Popular Tags |