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.renderkit.html.ext;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIData;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  import javax.faces.render.Renderer;
29  
30  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
31  import org.apache.myfaces.component.html.ext.AbstractHtmlDataTable;
32  import org.apache.myfaces.component.html.ext.HtmlDataTable;
33  import org.apache.myfaces.custom.crosstable.UIColumns;
34  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
36  
37  @JSFRenderer(renderKitId = "HTML_BASIC", family = "org.apache.myfaces.HtmlDetailStampRow", type = "org.apache.myfaces.HtmlDetailStampRow")
38  public class HtmlDetailStampRowRenderer extends Renderer
39  {
40  
41      @Override
42      public void encodeEnd(FacesContext facesContext, UIComponent component)
43              throws IOException
44      {
45          super.encodeEnd(facesContext, component);
46          UIData uiData = (UIData) component.getParent();
47          UIComponent detailStampFacet = uiData.getFacet(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME);
48          if (uiData instanceof HtmlDataTable) {
49              HtmlDataTable htmlDataTable = (HtmlDataTable) uiData;
50  
51              if (htmlDataTable.isCurrentDetailExpanded()) {
52  
53                  boolean embedded = false;
54                  if (detailStampFacet != null) {
55                      embedded = isEmbeddedTable(detailStampFacet);
56                  }
57  
58                  ResponseWriter writer = facesContext.getResponseWriter();
59  
60                  if (!embedded) {
61                      writer.startElement(HTML.TR_ELEM, uiData);
62                      writer.writeAttribute(HTML.ID_ATTR, component.getClientId(facesContext), null);
63                      writer.startElement(HTML.TD_ELEM, uiData);
64                      //TOMAHAWK-1087 datatable dont renders a detail correct 
65                      //if a UIColumns is used we have to count UIColumns 
66                      //elements as component.getRowCount()
67                      //instead of just get the number of children available,
68                      //so the colspan could be assigned correctly.
69                      int childCount = 0;
70                      for (Iterator childIter = uiData.getChildren().iterator();
71                          childIter.hasNext();)
72                      {
73                          UIComponent childComp = (UIComponent) childIter.next();
74                          if (childComp instanceof UIColumns)
75                          {
76                              UIColumns v = (UIColumns) childComp;
77                              childCount += v.getRowCount();
78                          }
79                          else
80                          {
81                              childCount++;
82                          }
83                      }
84                      writer.writeAttribute(HTML.COLSPAN_ATTR, new Integer(
85                              childCount), null);
86                  }
87  
88                  if (detailStampFacet != null) {
89                      RendererUtils.renderChild(facesContext, detailStampFacet);
90                  }
91  
92                  if (!embedded) {
93                      writer.endElement(HTML.TD_ELEM);
94                      writer.endElement(HTML.TR_ELEM);
95                  }
96              }
97              else
98              {
99                  boolean embedded = false;
100                 if (detailStampFacet != null) {
101                     embedded = isEmbeddedTable(detailStampFacet);
102                 }
103 
104                 ResponseWriter writer = facesContext.getResponseWriter();
105 
106                 if (!embedded) {
107                     writer.startElement(HTML.TR_ELEM, uiData);
108                     writer.writeAttribute(HTML.ID_ATTR, component.getClientId(facesContext), null);
109                     writer.writeAttribute(HTML.STYLE_ATTR, "display:none", null);
110                     writer.endElement(HTML.TR_ELEM);
111                 }
112             }
113         }
114     }
115     
116     protected boolean isEmbeddedTable(UIComponent uiComponent) {
117         boolean embedded = false;
118         if (uiComponent instanceof HtmlDataTable) {
119             HtmlDataTable table = (HtmlDataTable) uiComponent;
120             embedded = table.isEmbedded();
121         }
122         return embedded;
123     }
124 
125     @Override
126     public boolean getRendersChildren()
127     {
128         return true;
129     }
130     
131     public void encodeChildren(FacesContext context, UIComponent component) throws IOException
132     {
133     }
134 }