001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.kfs.sys.document.web.renderers; 017 018 import java.io.IOException; 019 020 import javax.servlet.jsp.JspException; 021 import javax.servlet.jsp.JspWriter; 022 import javax.servlet.jsp.PageContext; 023 import javax.servlet.jsp.tagext.Tag; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.kuali.kfs.sys.document.web.AccountingLineTableCell; 027 028 /** 029 * Renders a cell within a table 030 */ 031 public class TableCellRenderer implements Renderer { 032 private AccountingLineTableCell cell; 033 034 /** 035 * Resets the cell to null 036 * @see org.kuali.kfs.sys.document.web.renderers.Renderer#clear() 037 */ 038 public void clear() { 039 this.cell = null; 040 } 041 042 /** 043 * Renders the table cell as a header cell as well as rendering all children renderable elements of the cell 044 * @see org.kuali.kfs.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag) 045 */ 046 public void render(PageContext pageContext, Tag parentTag) throws JspException { 047 JspWriter out = pageContext.getOut(); 048 try { 049 out.write(buildBeginningTag()); 050 if (cell.hasChildElements()) { 051 cell.renderChildrenElements(pageContext, parentTag); 052 } else { 053 out.write(" "); 054 } 055 out.write(buildEndingTag()); 056 } 057 catch (IOException ioe) { 058 throw new JspException("Difficulty rendering table cell", ioe); 059 } 060 } 061 062 /** 063 * Builds the opening cell tag, ie <td> 064 * @return the opening cell tag 065 */ 066 protected String buildBeginningTag() { 067 StringBuilder builder = new StringBuilder(); 068 builder.append("<"); 069 builder.append(getTagName()); 070 if (cell.getColSpan() > 1) { 071 builder.append(" colspan=\""); 072 builder.append(cell.getColSpan()); 073 builder.append('"'); 074 } 075 if (cell.getRowSpan() > 1) { 076 builder.append(" rowspan=\""); 077 builder.append(cell.getRowSpan()); 078 builder.append('"'); 079 } 080 if (verticallyAlignTowardsTop()) { 081 builder.append(" valign=\"top\""); 082 } 083 if (!StringUtils.isBlank(cell.getExtraStyle())) { 084 builder.append(" style=\""); 085 builder.append(cell.getExtraStyle()); 086 builder.append("\""); 087 } else { 088 builder.append(" class=\""+getStyleClass()+"\""); 089 } 090 builder.append(">\n"); 091 return builder.toString(); 092 } 093 094 /** 095 * Returns what style class to use - using the styleClassOverride of the cell if possible 096 * @return the styleClassOverride if it exists, otherwise "infoline" 097 */ 098 protected String getStyleClass() { 099 return !StringUtils.isBlank(cell.getStyleClassOverride()) ? cell.getStyleClassOverride() : "infoline"; 100 } 101 102 /** 103 * Builds the closing cell tag, ie </td> 104 * @return the closing cell tag 105 */ 106 protected String buildEndingTag() { 107 StringBuilder builder = new StringBuilder(); 108 builder.append("</"); 109 builder.append(getTagName()); 110 builder.append(">"); 111 return builder.toString(); 112 } 113 114 /** 115 * Returns the name of the cell tag we want to create - in this case, "td" 116 * @return the String td, which is the tag name of the tags we want to produce 117 */ 118 protected String getTagName() { 119 return "td"; 120 } 121 122 /** 123 * Gets the cell attribute. 124 * @return Returns the cell. 125 */ 126 public AccountingLineTableCell getCell() { 127 return cell; 128 } 129 130 /** 131 * Sets the cell attribute value. 132 * @param cell The cell to set. 133 */ 134 public void setCell(AccountingLineTableCell cell) { 135 this.cell = cell; 136 } 137 138 /** 139 * Determines if the cell should be veritically aligned to the top 140 * @return true if the cell should vertically align to the top; false otherwise 141 */ 142 protected boolean verticallyAlignTowardsTop() { 143 return true; 144 } 145 }