View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.table;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.myfaces.custom.dojo.DojoUtils;
24  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
25  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
26  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
27  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
28  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
29  
30  import javax.faces.component.UIColumn;
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.UIData;
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  import java.io.IOException;
36  import java.util.List;
37  import java.util.Iterator;
38  
39  /**
40   * 
41   * @JSFRenderer
42   *   renderKitId = "HTML_BASIC" 
43   *   family = "javax.faces.Data"
44   *   type = "org.apache.myfaces.FilterTable"
45   * 
46   * @author Thomas Spiegl
47   */
48  public class FilterTableRenderer extends HtmlRenderer {
49  
50      public static final String RENDERER_TYPE = "org.apache.myfaces.FilterTable";
51  
52      private Log log = LogFactory.getLog(FilterTableRenderer.class);
53  
54      public boolean getRendersChildren() {
55          return true;
56      }
57  
58      public void decode(FacesContext context, UIComponent component) {
59          super.decode(context, component);
60      }
61  
62      public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
63          RendererUtils.checkParamValidity(context, component, FilterTable.class);
64          ResponseWriter writer = context.getResponseWriter();
65          
66          FilterTable table = (FilterTable) component;
67  
68          String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
69          DojoUtils.addMainInclude(context, component, javascriptLocation, DojoUtils.getDjConfigInstance(context));
70          DojoUtils.addRequire(context, component, "dojo.widget.FilteringTable");
71  
72          writer.write("<table dojoType=\"filteringTable\" id=\"" + component.getClientId(context) +  "\" ");
73          if (table.getStyleClass() != null) {
74               writer.write("class=\"" + table.getStyleClass() + "\" " );
75          }
76          if (table.getHeadClass() != null) {
77              writer.write("headClass=\"" + table.getHeadClass() + "\" " );
78          } else {
79              writer.write("headClass=\"fixedHeader\" " );
80          }
81          if (table.getTbodyClass() != null) {
82              writer.write("tbodyClass=\"" + table.getTbodyClass() + "\" ");
83          } else {
84              writer.write("tbodyClass=\"scrollContent\" ");
85          }
86          if (table.getMultiple() != null) {
87              String value = table.getMultiple().booleanValue() ? "true" : "false";
88              writer.write("multiple=\"" + value + "\" ");
89          } else {
90              writer.write("multiple=\"true\" ");
91          }
92          if (table.getAlternateRows() != null) {
93              String value = table.getAlternateRows().booleanValue() ? "true" : "false";
94              writer.write("alternateRows=\""+ value + "\" ");
95          } else {
96              writer.write("alternateRows=\"true\" ");
97          }
98          if (table.getMaxSortable() != null) {
99              writer.write("maxSortable=\"" + table.getMaxSortable() + "\" ");
100         } else {
101             writer.write("maxSortable=\"1\" ");
102         }
103         if (table.getCellpadding() != null) {
104             writer.write("cellpadding=\"" + table.getCellpadding() + "\" ");
105         } else {
106             writer.write("cellpadding=\"0\" ");
107         }
108         if (table.getCellspacing() != null) {
109             writer.write("cellspacing=\"" + table.getCellspacing() + "\" ");
110         } else {
111             writer.write("cellspacing=\"0\" ");
112         }
113         if (table.getBorder() != null) {
114             writer.write("border=\"" + table.getBorder() + "\"");
115         } else {
116             writer.write("border=\"0\"");
117         }
118         writer.write(">");
119         encodeHeader(context, component);
120     }
121 
122     public void encodeHeader(FacesContext context, UIComponent component) throws IOException {
123         ResponseWriter writer = context.getResponseWriter();
124         writer.startElement(HTML.THEAD_ELEM, component);
125         writer.startElement(HTML.TR_ELEM, component);
126         for (Iterator it = component.getChildren().iterator(); it.hasNext(); ) {
127             UIComponent child = (UIComponent) it.next();
128             if (child.isRendered() && child instanceof SortableColumn) {
129                 SortableColumn col = (SortableColumn) child;
130                 writer.startElement(HTML.TH_ELEM, col);
131                 if (col.getField() != null) {
132                     writer.writeAttribute("field", col.getField(), null);
133                 }
134                 if (col.getDataType() != null) {
135                     writer.writeAttribute("dataType", col.getDataType(), null);
136                 } else {
137                     writer.writeAttribute("dataType", "String", null);
138                 }
139                 if (col.getSort() != null) {
140                      writer.writeAttribute("sort", col.getSort(), null);
141                  }
142                  if (col.getAlign() != null) {
143                      writer.writeAttribute("align", col.getAlign(), null);
144                  }
145                  if (col.getValign() != null) {
146                      writer.writeAttribute("valign", col.getValign(), null);
147                  }
148                  if (col.getFormat() != null) {
149                      writer.writeAttribute("format", col.getFormat(), null);
150                  }
151                 Boolean escape = col.getEscape();
152                 if (escape == null || escape.booleanValue())  {
153                     writer.writeText(col.getText(), JSFAttr.VALUE_ATTR);
154                 } else {
155                     writer.writeText(col.getText(), null);
156                 }
157                 writer.endElement(HTML.TH_ELEM);
158             }
159         }
160         writer.endElement(HTML.TR_ELEM);
161         writer.endElement(HTML.THEAD_ELEM);
162     }
163 
164     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException
165     {
166         HtmlRendererUtils.writePrettyLineSeparator(facesContext);
167         ResponseWriter writer = facesContext.getResponseWriter();
168 
169         writer.startElement(HTML.TBODY_ELEM, component);
170         encodeInnerHtml(facesContext, component);
171         writer.endElement(HTML.TBODY_ELEM);
172     }
173 
174     protected void encodeInnerHtml(FacesContext facesContext, UIComponent component)throws IOException{
175         UIData uiData = (UIData) component;
176         ResponseWriter writer = facesContext.getResponseWriter();
177 
178         int first = uiData.getFirst();
179         int rows = uiData.getRows();
180         int last;
181         if (rows <= 0) {
182             last = uiData.getRowCount();
183         }
184         else {
185             last = first + rows;
186             if (last > uiData.getRowCount()) {
187                 last=uiData.getRowCount();
188             }
189         }
190         for(int idx = first; idx < last; idx++) {
191             // bail if any row does not exist
192             uiData.setRowIndex(idx);
193             if(!uiData.isRowAvailable()) {
194                 log.error("Row is not available. Rowindex = " + idx);
195                 break;
196             }
197 
198             writer.startElement(HTML.TR_ELEM, component);
199             writer.writeAttribute("value", Integer.toString(idx + 1), null);
200             List children = getChildren(component);
201             for (int j = 0, size = getChildCount(component); j < size; j++) {
202                 UIComponent child = (UIComponent) children.get(j);
203                 if (child.isRendered()) {
204                     boolean columnRendering = child instanceof UIColumn;
205                     if (columnRendering)
206                         encodeColumnChild(facesContext, writer, uiData, child);
207                 }
208             }
209             writer.endElement(HTML.TR_ELEM);
210         }
211     }
212 
213     protected void encodeColumnChild(FacesContext facesContext,  ResponseWriter writer, UIData uiData,
214                                      UIComponent component) throws IOException {
215         writer.startElement(HTML.TD_ELEM, uiData);
216         RendererUtils.renderChild(facesContext, component);
217         writer.endElement(HTML.TD_ELEM);
218     }
219 
220     public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
221         RendererUtils.checkParamValidity(context, component, UIData.class);
222         ResponseWriter writer = context.getResponseWriter();
223         writer.endElement(HTML.TABLE_ELEM);
224         HtmlRendererUtils.writePrettyLineSeparator(context);
225     }
226 }