View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.document.web.renderers;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.jsp.JspException;
21  import javax.servlet.jsp.JspWriter;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.Tag;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.sys.document.web.AccountingLineTableCell;
27  
28  /**
29   * Renders a cell within a table
30   */
31  public class TableCellRenderer implements Renderer {
32      private AccountingLineTableCell cell;
33  
34      /**
35       * Resets the cell to null
36       * @see org.kuali.ole.sys.document.web.renderers.Renderer#clear()
37       */
38      public void clear() {
39          this.cell = null;
40      }
41  
42      /**
43       * Renders the table cell as a header cell as well as rendering all children renderable elements of the cell
44       * @see org.kuali.ole.sys.document.web.renderers.Renderer#render(javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)
45       */
46      public void render(PageContext pageContext, Tag parentTag) throws JspException {
47          JspWriter out = pageContext.getOut();
48          try {
49              out.write(buildBeginningTag());
50              if (cell.hasChildElements()) {
51                  cell.renderChildrenElements(pageContext, parentTag);
52              } else {
53                  out.write(" ");
54              }
55              out.write(buildEndingTag());
56          }
57          catch (IOException ioe) {
58              throw new JspException("Difficulty rendering table cell", ioe);
59          }
60      }
61      
62      /**
63       * Builds the opening cell tag, ie <td>
64       * @return the opening cell tag
65       */
66      protected String buildBeginningTag() {
67          StringBuilder builder = new StringBuilder();
68          builder.append("<");
69          builder.append(getTagName());
70          if (cell.getColSpan() > 1) {
71              builder.append(" colspan=\"");
72              builder.append(cell.getColSpan());
73              builder.append('"');
74          }
75          if (cell.getRowSpan() > 1) {
76              builder.append(" rowspan=\"");
77              builder.append(cell.getRowSpan());
78              builder.append('"');
79          }
80          if (verticallyAlignTowardsTop()) {
81              builder.append(" valign=\"top\"");
82          }
83          if (!StringUtils.isBlank(cell.getExtraStyle())) {
84              builder.append(" style=\"");
85              builder.append(cell.getExtraStyle());
86              builder.append("\"");
87          } else {
88              builder.append(" class=\""+getStyleClass()+"\"");
89          }
90          builder.append(">\n");
91          return builder.toString();
92      }
93      
94      /**
95       * Returns what style class to use - using the styleClassOverride of the cell if possible
96       * @return the styleClassOverride if it exists, otherwise "infoline"
97       */
98      protected String getStyleClass() {
99          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 }