1 /*
2 * $Id: GridRenderer.java,v 1.50.4.3 2007/09/04 16:32:30 rlubke Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.faces.renderkit.html_basic;
42
43
44 import java.io.IOException;
45 import java.util.Iterator;
46
47 import javax.faces.component.UIComponent;
48 import javax.faces.context.FacesContext;
49 import javax.faces.context.ResponseWriter;
50
51 import com.sun.faces.renderkit.AttributeManager;
52
53 /**
54 * <B>GridRenderer</B> is a class that renders <code>UIPanel</code> component
55 * as a "Grid".
56 */
57
58 public class GridRenderer extends BaseTableRenderer {
59
60 private static final String[] ATTRIBUTES =
61 AttributeManager.getAttributes(AttributeManager.Key.PANELGRID);
62
63 // ---------------------------------------------------------- Public Methods
64
65
66 @Override
67 public void encodeBegin(FacesContext context, UIComponent component)
68 throws IOException {
69
70 rendererParamsNotNull(context, component);
71
72 if (!shouldEncode(component)) {
73 return;
74 }
75
76 // Render the beginning of this panel
77 ResponseWriter writer = context.getResponseWriter();
78 renderTableStart(context, component, writer, ATTRIBUTES);
79
80 // render the caption facet (if present)
81 renderCaption(context, component, writer);
82
83 // Render the header facet (if any)
84 renderHeader(context, component, writer);
85
86 // Render the footer facet (if any)
87 renderFooter(context, component, writer);
88
89 }
90
91
92 @Override
93 public void encodeChildren(FacesContext context, UIComponent component)
94 throws IOException {
95
96 rendererParamsNotNull(context, component);
97
98 if (!shouldEncodeChildren(component)) {
99 return;
100 }
101
102 // Set up the variables we will need
103 ResponseWriter writer = context.getResponseWriter();
104 TableMetaInfo info = getMetaInfo(component);
105 int columnCount = info.columns.size();
106 boolean open = false;
107 int i = 0;
108
109 // Render our children, starting a new row as needed
110 renderTableBodyStart(component, writer);
111 for (Iterator<UIComponent> kids = getChildren(component);
112 kids.hasNext();) {
113 UIComponent child = kids.next();
114 if (!child.isRendered()) {
115 continue;
116 }
117 if ((i % columnCount) == 0) {
118 if (open) {
119 renderRowEnd(component, writer);
120 open = false;
121 }
122 renderRowStart(component, writer);
123 open = true;
124 info.columnStyleCounter = 0;
125 }
126 renderRow(context, component, child, writer);
127 i++;
128 }
129 if (open) {
130 renderRowEnd(component, writer);
131 }
132 renderTableBodyEnd(component, writer);
133 }
134
135
136 @Override
137 public void encodeEnd(FacesContext context, UIComponent component)
138 throws IOException {
139
140 rendererParamsNotNull(context, component);
141
142 if (!shouldEncode(component)) {
143 return;
144 }
145
146 // Render the ending of this panel
147 renderTableEnd(component, context.getResponseWriter());
148
149 clearMetaInfo(component);
150
151 }
152
153
154 @Override
155 public boolean getRendersChildren() {
156
157 return true;
158
159 }
160
161
162 // ------------------------------------------------------- Protected Methods
163
164
165 protected void renderRow(FacesContext context,
166 UIComponent table,
167 UIComponent child,
168 ResponseWriter writer)
169 throws IOException {
170
171 TableMetaInfo info = getMetaInfo(table);
172 writer.startElement("td", table);
173 if (info.columnClasses.length > 0) {
174 writer.writeAttribute("class",
175 info.getCurrentColumnClass(),
176 "columns");
177 }
178 encodeRecursive(context, child);
179 writer.endElement("td");
180 writer.writeText("\n", table, null);
181
182 }
183
184
185 protected void renderHeader(FacesContext context,
186 UIComponent table,
187 ResponseWriter writer) throws IOException {
188
189 TableMetaInfo info = getMetaInfo(table);
190 UIComponent header = getFacet(table, "header");
191 String headerClass =
192 (String) table.getAttributes().get("headerClass");
193 if (header != null) {
194 writer.startElement("thead", table);
195 writer.writeText("\n", table, null);
196 writer.startElement("tr", header);
197 writer.startElement("th", header);
198 if (headerClass != null) {
199 writer.writeAttribute("class", headerClass, "headerClass");
200 }
201 writer.writeAttribute("colspan",
202 String.valueOf(info.columns.size()),
203 null);
204 writer.writeAttribute("scope", "colgroup", null);
205 encodeRecursive(context, header);
206 writer.endElement("th");
207 writer.endElement("tr");
208 writer.writeText("\n", table, null);
209 writer.endElement("thead");
210 writer.writeText("\n", table, null);
211 }
212
213 }
214
215
216 protected void renderFooter(FacesContext context,
217 UIComponent table,
218 ResponseWriter writer) throws IOException {
219
220 TableMetaInfo info = getMetaInfo(table);
221 UIComponent footer = getFacet(table, "footer");
222 String footerClass =
223 (String) table.getAttributes().get("footerClass");
224 if (footer != null) {
225 writer.startElement("tfoot", table);
226 writer.writeText("\n", table, null);
227 writer.startElement("tr", footer);
228 writer.startElement("td", footer);
229 if (footerClass != null) {
230 writer.writeAttribute("class", footerClass, "footerClass");
231 }
232 writer.writeAttribute("colspan",
233 String.valueOf(info.columns.size()),
234 null);
235 encodeRecursive(context, footer);
236 writer.endElement("td");
237 writer.endElement("tr");
238 writer.writeText("\n", table, null);
239 writer.endElement("tfoot");
240 writer.writeText("\n", table, null);
241 }
242
243 }
244
245 }