PageRenderTime 127ms CodeModel.GetById 20ms RepoModel.GetById 9ms app.codeStats 0ms

/src/main/java/org/primefaces/component/panelgrid/PanelGridRenderer.java

http://primefaces.googlecode.com/
Java | 317 lines | 245 code | 56 blank | 16 comment | 62 complexity | db4e8b232b97c395c6dd936d5a60fd18 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2009-2014 PrimeTek.
  3. *
  4. * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
  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.primefaces.component.panelgrid;
  17. import java.io.IOException;
  18. import javax.faces.FacesException;
  19. import javax.faces.component.UIComponent;
  20. import javax.faces.component.UIPanel;
  21. import javax.faces.context.FacesContext;
  22. import javax.faces.context.ResponseWriter;
  23. import org.primefaces.component.column.Column;
  24. import org.primefaces.component.row.Row;
  25. import org.primefaces.renderkit.CoreRenderer;
  26. import org.primefaces.util.Constants;
  27. import org.primefaces.util.GridLayoutUtils;
  28. public class PanelGridRenderer extends CoreRenderer {
  29. @Override
  30. public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
  31. PanelGrid grid = (PanelGrid) component;
  32. if(grid.getLayout().equals("tabular")) {
  33. encodeTableLayout(context, grid);
  34. }
  35. else if(grid.getLayout().equals("grid")){
  36. encodeGridLayout(context, grid);
  37. }
  38. else {
  39. throw new FacesException("The value of 'layout' attribute must be 'grid' or 'tabular'. Default value is 'tabular'.");
  40. }
  41. }
  42. public void encodeTableLayout(FacesContext context, PanelGrid grid) throws IOException {
  43. ResponseWriter writer = context.getResponseWriter();
  44. String clientId = grid.getClientId(context);
  45. int columns = grid.getColumns();
  46. String style = grid.getStyle();
  47. String styleClass = grid.getStyleClass();
  48. styleClass = styleClass == null ? PanelGrid.CONTAINER_CLASS : PanelGrid.CONTAINER_CLASS + " " + styleClass;
  49. writer.startElement("table", grid);
  50. writer.writeAttribute("id", clientId, "id");
  51. writer.writeAttribute("class", styleClass, "styleClass");
  52. if(style != null) {
  53. writer.writeAttribute("style", style, "style");
  54. }
  55. writer.writeAttribute("role", "grid", null);
  56. encodeTableFacet(context, grid, columns, "header", "thead", PanelGrid.HEADER_CLASS);
  57. encodeTableFacet(context, grid, columns, "footer", "tfoot", PanelGrid.FOOTER_CLASS);
  58. encodeTableBody(context, grid, columns);
  59. writer.endElement("table");
  60. }
  61. public void encodeGridLayout(FacesContext context, PanelGrid grid) throws IOException {
  62. ResponseWriter writer = context.getResponseWriter();
  63. String clientId = grid.getClientId(context);
  64. int columns = grid.getColumns();
  65. if(columns == 0) {
  66. throw new FacesException("Columns of PanelGrid \"" + grid.getClientId(context) + "\" must be greater than zero in grid layout.");
  67. }
  68. String style = grid.getStyle();
  69. String styleClass = grid.getStyleClass();
  70. styleClass = styleClass == null ? PanelGrid.CONTAINER_CLASS : PanelGrid.CONTAINER_CLASS + " " + styleClass;
  71. writer.startElement("div", grid);
  72. writer.writeAttribute("id", clientId, "id");
  73. writer.writeAttribute("class", styleClass, "styleClass");
  74. if(style != null) {
  75. writer.writeAttribute("style", style, "style");
  76. }
  77. encodeGridFacet(context, grid, columns, "header", PanelGrid.HEADER_CLASS);
  78. encodeGridBody(context, grid, columns);
  79. encodeGridFacet(context, grid, columns, "footer", PanelGrid.FOOTER_CLASS);
  80. writer.endElement("div");
  81. }
  82. public void encodeTableBody(FacesContext context, PanelGrid grid, int columns) throws IOException {
  83. ResponseWriter writer = context.getResponseWriter();
  84. writer.startElement("tbody", grid);
  85. if(columns > 0)
  86. encodeDynamicBody(context, grid, grid.getColumns());
  87. else
  88. encodeStaticBody(context, grid);
  89. writer.endElement("tbody");
  90. }
  91. public void encodeDynamicBody(FacesContext context, PanelGrid grid, int columns) throws IOException {
  92. ResponseWriter writer = context.getResponseWriter();
  93. String columnClassesValue = grid.getColumnClasses();
  94. String[] columnClasses = columnClassesValue == null ? new String[0] : columnClassesValue.split(",");
  95. int i = 0;
  96. for(UIComponent child : grid.getChildren()) {
  97. if(!child.isRendered()) {
  98. continue;
  99. }
  100. int colMod = i % columns;
  101. if(colMod == 0) {
  102. writer.startElement("tr", null);
  103. writer.writeAttribute("class", PanelGrid.TABLE_ROW_CLASS, null);
  104. writer.writeAttribute("role", "row", null);
  105. }
  106. String columnClass = (colMod < columnClasses.length) ? PanelGrid.CELL_CLASS + " " + columnClasses[colMod].trim() : PanelGrid.CELL_CLASS;
  107. writer.startElement("td", null);
  108. writer.writeAttribute("role", "gridcell", null);
  109. writer.writeAttribute("class", columnClass, null);
  110. child.encodeAll(context);
  111. writer.endElement("td");
  112. i++;
  113. colMod = i % columns;
  114. if(colMod == 0) {
  115. writer.endElement("tr");
  116. }
  117. }
  118. }
  119. public void encodeStaticBody(FacesContext context, PanelGrid grid) throws IOException {
  120. context.getAttributes().put(Constants.HELPER_RENDERER, "panelGridBody");
  121. int i=0;
  122. for(UIComponent child : grid.getChildren()) {
  123. if(child.isRendered()) {
  124. if(child instanceof Row) {
  125. String rowStyleClass = i % 2 == 0 ? PanelGrid.TABLE_ROW_CLASS + " " + PanelGrid.EVEN_ROW_CLASS : PanelGrid.TABLE_ROW_CLASS + " " + PanelGrid.ODD_ROW_CLASS;
  126. encodeRow(context, (Row) child, "gridcell", rowStyleClass, PanelGrid.CELL_CLASS);
  127. i++;
  128. }
  129. else {
  130. child.encodeAll(context);
  131. }
  132. }
  133. }
  134. context.getAttributes().remove(Constants.HELPER_RENDERER);
  135. }
  136. public void encodeRow(FacesContext context, Row row, String columnRole, String rowClass, String columnClass) throws IOException {
  137. ResponseWriter writer = context.getResponseWriter();
  138. String style = row.getStyle();
  139. writer.startElement("tr", null);
  140. if(shouldWriteId(row)) {
  141. writer.writeAttribute("id", row.getClientId(context), null);
  142. }
  143. if(row.getStyleClass() != null) {
  144. rowClass += " " + row.getStyleClass();
  145. }
  146. if(style != null) writer.writeAttribute("style", style, null);
  147. writer.writeAttribute("class", rowClass, null);
  148. writer.writeAttribute("role", "row", null);
  149. for(UIComponent child : row.getChildren()) {
  150. if(child instanceof Column && child.isRendered()) {
  151. Column column = (Column) child;
  152. String userStyleClass = column.getStyleClass();
  153. String styleClass = (userStyleClass == null) ? columnClass : columnClass + " " + userStyleClass;
  154. writer.startElement("td", null);
  155. if(shouldWriteId(column)) {
  156. writer.writeAttribute("id", column.getClientId(context), null);
  157. }
  158. writer.writeAttribute("role", columnRole, null);
  159. writer.writeAttribute("class", styleClass, null);
  160. if(column.getStyle() != null) writer.writeAttribute("style", column.getStyle(), null);
  161. if(column.getColspan() > 1) writer.writeAttribute("colspan", column.getColspan(), null);
  162. if(column.getRowspan() > 1) writer.writeAttribute("rowspan", column.getRowspan(), null);
  163. renderChildren(context, column);
  164. writer.endElement("td");
  165. }
  166. }
  167. writer.endElement("tr");
  168. }
  169. public void encodeGridBody(FacesContext context, PanelGrid grid, int columns) throws IOException {
  170. String clientId = grid.getClientId(context);
  171. ResponseWriter writer = context.getResponseWriter();
  172. String columnClassesValue = grid.getColumnClasses();
  173. String[] columnClasses = columnClassesValue == null ? new String[0] : columnClassesValue.split(",");
  174. writer.startElement("div", grid);
  175. writer.writeAttribute("id", clientId + "_content", null);
  176. writer.writeAttribute("class", PanelGrid.CONTENT_CLASS, null);
  177. int i = 0;
  178. for(UIComponent child : grid.getChildren()) {
  179. if(!child.isRendered()) {
  180. continue;
  181. }
  182. int colMod = i % columns;
  183. if(colMod == 0) {
  184. writer.startElement("div", null);
  185. writer.writeAttribute("class", PanelGrid.GRID_ROW_CLASS, null);
  186. }
  187. String columnClass = (colMod < columnClasses.length) ? PanelGrid.CELL_CLASS + " " + columnClasses[colMod].trim() : PanelGrid.CELL_CLASS;
  188. if (!columnClass.contains("ui-grid-col-")) {
  189. columnClass = columnClass + " " + GridLayoutUtils.getColumnClass(columns);
  190. }
  191. writer.startElement("div", null);
  192. writer.writeAttribute("class", columnClass, null);
  193. child.encodeAll(context);
  194. writer.endElement("div");
  195. i++;
  196. colMod = i % columns;
  197. if(colMod == 0) {
  198. writer.endElement("div");
  199. }
  200. }
  201. writer.endElement("div");
  202. }
  203. public void encodeTableFacet(FacesContext context, PanelGrid grid, int columns, String facet, String tag, String styleClass) throws IOException {
  204. UIComponent component = grid.getFacet(facet);
  205. if(component != null && component.isRendered()) {
  206. ResponseWriter writer = context.getResponseWriter();
  207. writer.startElement(tag, null);
  208. writer.writeAttribute("class", styleClass, null);
  209. if(columns > 0) {
  210. writer.startElement("tr", null);
  211. writer.writeAttribute("class", "ui-widget-header", null);
  212. writer.writeAttribute("role", "row", null);
  213. writer.startElement("td", null);
  214. writer.writeAttribute("colspan", columns, null);
  215. writer.writeAttribute("role", "columnheader", null);
  216. component.encodeAll(context);
  217. writer.endElement("td");
  218. writer.endElement("tr");
  219. }
  220. else {
  221. context.getAttributes().put(Constants.HELPER_RENDERER, "panelGridFacet");
  222. if(component instanceof Row) {
  223. encodeRow(context, (Row) component, "columnheader", "ui-widget-header", PanelGrid.CELL_CLASS + " ui-widget-header");
  224. }
  225. else if(component instanceof UIPanel) {
  226. for(UIComponent child : component.getChildren()) {
  227. if(child.isRendered()) {
  228. if(child instanceof Row)
  229. encodeRow(context, (Row) child, "columnheader", "ui-widget-header", PanelGrid.CELL_CLASS + " ui-widget-header");
  230. else
  231. component.encodeAll(context);
  232. }
  233. }
  234. }
  235. else {
  236. component.encodeAll(context);
  237. }
  238. context.getAttributes().remove(Constants.HELPER_RENDERER);
  239. }
  240. writer.endElement(tag);
  241. }
  242. }
  243. public void encodeGridFacet(FacesContext context, PanelGrid grid, int columns, String facet, String styleClass) throws IOException {
  244. UIComponent component = grid.getFacet(facet);
  245. if(component != null && component.isRendered()) {
  246. ResponseWriter writer = context.getResponseWriter();
  247. writer.startElement("div", null);
  248. writer.writeAttribute("class", styleClass + " ui-widget-header", null);
  249. component.encodeAll(context);
  250. writer.endElement("div");
  251. }
  252. }
  253. @Override
  254. public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
  255. //Rendering happens on encodeEnd
  256. }
  257. @Override
  258. public boolean getRendersChildren() {
  259. return true;
  260. }
  261. }