KickJava   Java API By Example, From Geeks To Geeks.

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


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.UIColumn;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIData;
24 import javax.faces.context.FacesContext;
25 import javax.faces.context.ResponseWriter;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.myfaces.wap.component.DataTable;
30 import org.apache.myfaces.wap.renderkit.Attributes;
31 import org.apache.myfaces.wap.renderkit.RendererUtils;
32 import org.apache.myfaces.wap.renderkit.WmlRenderer;
33
34
35 /**
36  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
37  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
38  * $Log: TableRenderer.java,v $
39  * Revision 1.1 2004/12/30 09:37:26 matzew
40  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
41  *
42  */

43 public class TableRenderer extends WmlRenderer {
44     private static Log log = LogFactory.getLog(TableRenderer.class);
45     
46     /** Creates a new instance of TableRenderer */
47     public TableRenderer() {
48         super();
49         log.debug("created object " + this.getClass().getName());
50     }
51     
52     public boolean getRendersChildren(){
53         return(true);
54     }
55     
56     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
57         log.debug("encodeBegin(" + component.getId() + ")");
58         
59         if (context == null || component == null) {
60             throw new NullPointerException JavaDoc();
61         }
62         if (!component.isRendered()) return;
63         // rendered in method encodeEnd
64
}
65     
66     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
67         log.debug("encodeChildren(" + component.getId() + ")");
68         if (context == null || component == null) {
69             throw new NullPointerException JavaDoc();
70         }
71         if (!component.isRendered()) return;
72         
73         // rendered in method encodeEnd
74
}
75     
76     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
77         log.debug("encodeEnd(" + component.getId() + ")");
78         if (context == null || component == null) {
79             throw new NullPointerException JavaDoc();
80         }
81         if (!component.isRendered()) return;
82         
83         ResponseWriter writer = context.getResponseWriter();
84         DataTable comp = (DataTable)component;
85         
86         // number of columns equals count of childern (UIColumn elements)
87
int columns = component.getChildCount();
88         
89         // If no columns was found, the table element will not be rendered.
90
if (columns == 0) return;
91         
92         writer.startElement(Attributes.TABLE, component);
93         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
94         RendererUtils.writeAttribute(Attributes.CLASS, comp.getStyleClass(), writer);
95         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
96         
97         RendererUtils.writeAttribute(Attributes.ALIGN, comp.getAlign(), writer);
98                 
99         RendererUtils.writeAttribute(Attributes.COLUMNS, new Integer JavaDoc(columns), writer);
100         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
101         
102         if (hasHeaderOrFooter(context, component, true))
103             renderHeaderOrFooter(context, component, true);
104         
105         renderChildren(context, component);
106         
107         if (hasHeaderOrFooter(context, component, false))
108             renderHeaderOrFooter(context, component, false);
109         
110         writer.endElement(Attributes.TABLE);
111     }
112     
113     public void decode(FacesContext context, UIComponent component) {
114         if (component == null ) throw new NullPointerException JavaDoc();
115     }
116     
117     private void renderChildren(FacesContext context, UIComponent component) throws IOException JavaDoc {
118         ResponseWriter writer = context.getResponseWriter();
119         
120         
121         log.debug("Childern: " + component.getChildCount() + " of Component " + component.getFamily());
122         
123         UIData comp = (UIData)component;
124         
125         int first = comp.getFirst();
126         int rows = comp.getRows();
127         int rowCount = comp.getRowCount();
128         if (rows <= 0) {
129             rows = rowCount - first;
130         }
131         int last = first + rows;
132         if (last > rowCount) last = rowCount;
133         
134         for (int i = first; i < last; i++){
135             writer.startElement(Attributes.TR, component);
136             comp.setRowIndex(i);
137             
138             if (!comp.isRowAvailable()){
139                 log.error("Row: " + i + " is not available.");
140                 continue;
141             }
142             
143             Iterator JavaDoc columns = component.getChildren().iterator();
144             while (columns.hasNext()){
145                 UIComponent column = (UIComponent)columns.next();
146                 if (!(column instanceof UIColumn)) {
147                     log.error("Only column elements can be nested in dataTable.");
148                     break;
149                 }
150                 
151                 writer.startElement(Attributes.TD, component);
152                 RendererUtils.renderChild(context, column);
153                 writer.endElement(Attributes.TD);
154             }
155             writer.endElement(Attributes.TR);
156         }
157     }
158     
159     /** renders header or footer of the table */
160     private void renderHeaderOrFooter(FacesContext context, UIComponent component, boolean header) throws IOException JavaDoc {
161         ResponseWriter writer = context.getResponseWriter();
162         
163         writer.startElement(Attributes.TR, component);
164         
165         Iterator JavaDoc columns = component.getChildren().iterator();
166         while (columns.hasNext()){
167             UIComponent column = (UIComponent)columns.next();
168             if (!(column instanceof UIColumn)) {
169                 log.error("Only column elements can be nested in dataTable.");
170                 break;
171             }
172             
173             UIComponent facet = header ? ((UIColumn)column).getHeader() : ((UIColumn)column).getFooter();
174             
175             writer.startElement(Attributes.TD, component);
176             RendererUtils.renderChild(context, facet);
177             writer.endElement(Attributes.TD);
178         }
179         writer.endElement(Attributes.TR);
180     }
181     
182     /** Checks if the table has any header or footer. */
183     private boolean hasHeaderOrFooter(FacesContext context, UIComponent component, boolean header){
184         Iterator JavaDoc childn = component.getChildren().iterator();
185         while (childn.hasNext()){
186             UIComponent child = (UIComponent)childn.next();
187             if (child instanceof UIColumn && child.isRendered()){
188                 if (header) {
189                     if (((UIColumn)child).getHeader() != null) return(true);
190                 }
191                 else{
192                     if (((UIColumn)child).getFooter() != null) return(true);
193                 }
194             }
195         }
196         return (false);
197     }
198 }
199
200
Popular Tags