KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > wml > GridRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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.apache.myfaces.wap.renderkit.wml;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.context.ResponseWriter;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.myfaces.wap.component.PanelGrid;
28 import org.apache.myfaces.wap.renderkit.Attributes;
29 import org.apache.myfaces.wap.renderkit.RendererUtils;
30 import org.apache.myfaces.wap.renderkit.WmlRenderer;
31
32
33 /**
34  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
35  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
36  * $Log: GridRenderer.java,v $
37  * Revision 1.1 2004/12/30 09:37:26 matzew
38  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
39  *
40  */

41 public class GridRenderer extends WmlRenderer {
42     private static Log log = LogFactory.getLog(GridRenderer.class);
43     
44     /** Creates a new instance of TextRenderer */
45     public GridRenderer() {
46         super();
47         log.debug("created object " + this.getClass().getName());
48     }
49     
50     public boolean getRendersChildren(){
51         return(true);
52     }
53     
54     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
55         log.debug("encodeBegin(" + component.getId() + ")");
56         
57         if (context == null || component == null) {
58             throw new NullPointerException JavaDoc();
59         }
60         if (!component.isRendered()) return;
61         // renderen in method encodeEnd
62
}
63     
64     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
65         log.debug("encodeChildren(" + component.getId() + ")");
66         if (context == null || component == null) {
67             throw new NullPointerException JavaDoc();
68         }
69         if (!component.isRendered()) return;
70         // renderen in method encodeEnd
71
}
72     
73     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
74         log.debug("encodeEnd(" + component.getId() + ")");
75         if (context == null || component == null) {
76             throw new NullPointerException JavaDoc();
77         }
78         if (!component.isRendered()) return;
79         
80         ResponseWriter writer = context.getResponseWriter();
81         PanelGrid comp = (PanelGrid)component;
82         int columns = getColumnsNumber(comp.getColumns());
83         
84         // If no body, header or footer is found, then the table element will be not rendered.
85
if (!hasTableAnyContext(context, component)) return;
86         
87         writer.startElement(Attributes.TABLE, component);
88         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
89         RendererUtils.writeAttribute(Attributes.CLASS, comp.getStyleClass(), writer);
90         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
91         
92         RendererUtils.writeAttribute(Attributes.ALIGN, comp.getAlign(), writer);
93         RendererUtils.writeAttribute(Attributes.COLUMNS, comp.getColumns(), writer);
94         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
95         
96         renderHeaderOrFooter(context, component, Attributes.HEADER, columns);
97         
98         renderChildern(context, component, columns);
99         
100         renderHeaderOrFooter(context, component, Attributes.FOOTER, columns);
101         
102         writer.endElement(Attributes.TABLE);
103     }
104     
105     public void decode(FacesContext context, UIComponent component) {
106         if (component == null ) throw new NullPointerException JavaDoc();
107     }
108     
109     private void renderChildern(FacesContext context, UIComponent component, int maxColumn) throws IOException JavaDoc {
110         ResponseWriter writer = context.getResponseWriter();
111         int column = 0, row = 0;
112         boolean rowStarted = false;
113         
114         Iterator JavaDoc childn = component.getChildren().iterator();
115         while (childn.hasNext()){
116             if (!rowStarted) {
117                 writer.startElement(Attributes.TR, component);
118                 rowStarted = true; column = 0; row = 1;
119             }
120             
121             // next cell
122
writer.startElement(Attributes.TD, component);
123             RendererUtils.renderChild(context, (UIComponent)childn.next());
124             writer.endElement(Attributes.TD);
125             column++;
126             
127             // end row
128
if (column >= maxColumn) {
129                 writer.endElement(Attributes.TR);
130                 rowStarted = false;
131             }
132         }
133         
134         if (rowStarted) writeRowEnd(component, writer, column, maxColumn);
135     }
136     
137     /**
138      * renders header or footer of the table
139      * @param context
140      * @param component
141      * @param str
142      * @param maxColumn
143      * @throws java.io.IOException
144      */

145     private void renderHeaderOrFooter(FacesContext context, UIComponent component, String JavaDoc str, int maxColumn) throws IOException JavaDoc {
146         ResponseWriter writer = context.getResponseWriter();
147         UIComponent facet = component.getFacet(str);
148         
149         int column = 0;
150         boolean rowStarted = false;
151         
152         if (facet == null) return;
153         
154         log.debug("Facet " + str + " is not null. Child numb:" + facet.getChildCount());
155         Iterator JavaDoc childn = facet.getChildren().iterator();
156         
157         // starts header row
158
if (childn.hasNext()){
159             writer.startElement(Attributes.TR, component);
160             rowStarted = true;
161         }
162         
163         // render all children
164
while (childn.hasNext()){
165             writer.startElement(Attributes.TD, component);
166             // render facet
167
RendererUtils.renderChild(context, (UIComponent)childn.next());
168             writer.endElement(Attributes.TD);
169             column++;
170         }
171         
172         if (rowStarted) writeRowEnd(component, writer, column, maxColumn);
173     }
174     
175     /**
176      * appends free cells to the number of columns
177      * @param component
178      * @param writer
179      * @param column
180      * @param maxColumn
181      * @throws java.io.IOException
182      */

183     private void writeRowEnd(UIComponent component, ResponseWriter writer, int column, int maxColumn) throws IOException JavaDoc {
184         while (column < maxColumn) {
185             writer.startElement(Attributes.TD, component);
186             writer.endElement(Attributes.TD);
187             column++;
188         }
189         writer.endElement(Attributes.TR);
190     }
191     
192     /** Checks if the table has any header, body or footer. */
193     private boolean hasTableAnyContext(FacesContext context, UIComponent component){
194         boolean hasHeader = component.getFacet(Attributes.HEADER) != null;
195         boolean hasBody = component.getChildCount() > 0;
196         boolean hasFooter = component.getFacet(Attributes.FOOTER) != null;
197         
198         return (hasHeader || hasBody || hasFooter);
199     }
200     
201     /** Returns number of columns in table. Converts String parameter to int number, if parameter is not correct int number, return zero. */
202     private int getColumnsNumber(String JavaDoc columns){
203         int number;
204         try {
205             number = Integer.parseInt(columns);
206         } catch (NumberFormatException JavaDoc ex){
207             return (0);
208         }
209         return (number);
210     }
211 }
212
213
Popular Tags