KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > GridRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.context.DOMContext;
37 import org.w3c.dom.Element JavaDoc;
38
39 import javax.faces.component.UIComponent;
40 import javax.faces.component.html.HtmlPanelGrid;
41 import javax.faces.context.FacesContext;
42 import java.io.IOException JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 public class GridRenderer extends DomBasicRenderer {
46
47     public boolean getRendersChildren() {
48         return true;
49     }
50
51     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
52             throws IOException JavaDoc {
53         validateParameters(facesContext, uiComponent, null);
54         DOMContext domContext =
55                 DOMContext.attachDOMContext(facesContext, uiComponent);
56         if (!domContext.isInitialized()) {
57             Element JavaDoc root = domContext.createElement("table");
58             domContext.setRootNode(root);
59             setRootElementId(facesContext, root, uiComponent);
60             PassThruAttributeRenderer.renderAttributes(
61                     facesContext, uiComponent, null);
62         }
63         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
64         String JavaDoc styleClass = ((HtmlPanelGrid) uiComponent).getStyleClass();
65         if (styleClass != null) {
66             root.setAttribute("class", styleClass);
67         }
68     }
69
70     private void renderHeaderFacet(FacesContext facesContext,
71                                    UIComponent uiComponent,
72                                    DOMContext domContext) throws IOException JavaDoc {
73         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
74         DOMContext.removeChildrenByTagName(root, "thead");
75         UIComponent headerFacet = getFacetByName(uiComponent, "header");
76         if (headerFacet != null && headerFacet.isRendered()) {
77             Element JavaDoc thead = domContext.createElement("thead");
78             Element JavaDoc tr = domContext.createElement("tr");
79             Element JavaDoc th = domContext.createElement("th");
80             root.appendChild(thead);
81             thead.appendChild(tr);
82             tr.appendChild(th);
83             String JavaDoc headerClassAttribute =
84                     ((HtmlPanelGrid) uiComponent).getHeaderClass();
85             if (headerClassAttribute != null) {
86                 th.setAttribute("class", headerClassAttribute);
87             }
88             th.setAttribute("scope", "colgroup");
89             th.setAttribute("colspan",
90                             String.valueOf(
91                                     getConvertedColumnAttribute(uiComponent)));
92             domContext.setCursorParent(th);
93             domContext.streamWrite(facesContext, uiComponent,
94                                    domContext.getRootNode(), th);
95             encodeParentAndChildren(facesContext, headerFacet);
96         }
97     }
98
99     private void renderFooterFacet(FacesContext facesContext,
100                                    UIComponent uiComponent,
101                                    DOMContext domContext) throws IOException JavaDoc {
102         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
103         DOMContext.removeChildrenByTagName(root, "tfoot");
104         UIComponent footerFacet = getFacetByName(uiComponent, "footer");
105         if (footerFacet != null && footerFacet.isRendered()) {
106             Element JavaDoc tfoot = domContext.createElement("tfoot");
107             Element JavaDoc tr = domContext.createElement("tr");
108             Element JavaDoc td = domContext.createElement("td");
109             root.appendChild(tfoot);
110             tfoot.appendChild(tr);
111             tr.appendChild(td);
112             String JavaDoc footerClassAttribute =
113                     ((HtmlPanelGrid) uiComponent).getFooterClass();
114             if (footerClassAttribute != null) {
115                 td.setAttribute("class", footerClassAttribute);
116             }
117             td.setAttribute("colspan",
118                             String.valueOf(
119                                     getConvertedColumnAttribute(uiComponent)));
120             domContext.setCursorParent(td);
121             domContext.streamWrite(facesContext, uiComponent,
122                                    domContext.getRootNode(), td);
123             encodeParentAndChildren(facesContext, footerFacet);
124         }
125     }
126
127     public void encodeChildren(FacesContext facesContext,
128                                UIComponent uiComponent)
129             throws IOException JavaDoc {
130         validateParameters(facesContext, uiComponent, null);
131         DOMContext domContext =
132                 DOMContext.getDOMContext(facesContext, uiComponent);
133         renderHeaderFacet(facesContext, uiComponent, domContext);
134         // remove previous children
135
Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
136         DOMContext.removeChildrenByTagName(root, "tbody");
137         Element JavaDoc tbody = domContext.createElement("tbody");
138         root.appendChild(tbody);
139         Element JavaDoc tr = null;
140
141         // Render children inside the tbody element.
142
// Based on the value of the "columns" attribute, create a new row every
143
// time a columns-worth of children has been rendered.
144
// Children with attribute "rendered" == false are not rendered and do
145
// not occupy a table cell.
146
Iterator JavaDoc children = uiComponent.getChildren().iterator();
147         if (children != null) {
148             int numberOfColumns = getConvertedColumnAttribute(uiComponent);
149             int rowIndex =
150                     -1;// this initial value ensures zero-based indexing in ids
151
int columnIndex =
152                     numberOfColumns; // this initial value invoked initialization of first row
153
String JavaDoc columnStyleClasses[] = getColumnStyleClasses(uiComponent);
154             String JavaDoc rowStyleClasses[] = getRowStyles(uiComponent);
155             int columnStyleIndex = 0;
156             int rowStyleIndex = 0;
157             int numberOfColumnStyles = columnStyleClasses.length - 1;
158             int numberOfRowStyles = rowStyleClasses.length;
159             UIComponent facet = null;
160             while (children.hasNext()) {
161                 UIComponent nextChild = (UIComponent) children.next();
162                 if (!nextChild.isRendered()) {
163                     continue;
164                 }
165                 // detect whether a new row is needed
166
if (columnIndex >= numberOfColumns) {
167                     tr = domContext.createElement("tr");
168                     tbody.appendChild(tr);
169                     if (numberOfRowStyles > 0) {
170                         tr.setAttribute("class",rowStyleClasses[rowStyleIndex++]);
171                         if (rowStyleIndex >= numberOfRowStyles) {
172                             rowStyleIndex = 0;
173                         }
174                     }
175                     columnStyleIndex = 0;
176                     columnIndex = 0;
177                     rowIndex++;
178                 }
179                 // create the td for this child
180
Element JavaDoc td = domContext.createElement("td");
181                 tr.appendChild(td);
182                 td.setAttribute("id", getIndexedClientId(facesContext,
183                                                          uiComponent,
184                                                          columnIndex,
185                                                          rowIndex));
186            
187                 writeColStyles(columnStyleClasses, numberOfColumnStyles,
188                                columnStyleIndex, td, columnIndex + 1, uiComponent);
189                 if (++columnStyleIndex > numberOfColumnStyles) {
190                     columnStyleIndex = 0;
191                 }
192
193                 domContext.setCursorParent(td);
194                 domContext.streamWrite(facesContext, uiComponent,
195                                        domContext.getRootNode(), td);
196                 encodeParentAndChildren(facesContext, nextChild);
197                 columnIndex++;
198             }
199         }
200         renderFooterFacet(facesContext, uiComponent, domContext);
201         domContext.stepOver();
202         domContext.streamWrite(facesContext, uiComponent);
203     }
204
205     // this method is overridden in the subclass
206
public String JavaDoc[] getRowStyles(UIComponent uiComponent) {
207         return getRowStyleClasses(uiComponent);
208     }
209
210     private String JavaDoc getIndexedClientId(FacesContext facesContext,
211                                       UIComponent uiComponent,
212                                       int columnIndex, int rowIndex) {
213         return uiComponent.getClientId(facesContext)
214                + "-"
215                + rowIndex
216                + "-"
217                + columnIndex;
218     }
219
220     private int getConvertedColumnAttribute(UIComponent uiComponent) {
221         int convertedColumnAttribute = 1; // default
222
Object JavaDoc columnAttribute = uiComponent.getAttributes().get("columns");
223         int value;
224         if (columnAttribute != null
225             && columnAttribute instanceof Integer JavaDoc
226             && (value = ((Integer JavaDoc) columnAttribute).intValue()) > 0) {
227             convertedColumnAttribute = value;
228         }
229         return (convertedColumnAttribute);
230     }
231
232
233     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
234             throws IOException JavaDoc {
235         validateParameters(facesContext, uiComponent, null);
236     }
237
238     // this method is overridden in the subclass
239
public void writeColStyles(String JavaDoc[] columnStyles, int columnStylesMaxIndex,
240                                int columnStyleIndex, Element JavaDoc td,
241                                int colNumber, UIComponent uiComponent) {
242         if (columnStyles.length > 0) {
243             if (columnStylesMaxIndex >= 0) {
244                 td.setAttribute("class",
245                                 columnStyles[columnStyleIndex]);
246                 if (++columnStyleIndex > columnStylesMaxIndex) {
247                     columnStyleIndex = 0;
248                 }
249             }
250         }
251     }
252     
253     protected String JavaDoc getRowStyle(UIComponent uiComponent, String JavaDoc style) {
254         return style;
255     }
256    
257
258 }
259
Popular Tags