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.layout;
20  
21  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
22  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
23  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
24  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  import java.io.IOException;
33  
34  /**
35   * @JSFRenderer
36   *   renderKitId = "HTML_BASIC" 
37   *   family = "javax.faces.Panel"
38   *   type = "org.apache.myfaces.Layout"
39   * 
40   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
41   * @version $Revision: 980618 $ $Date: 2010-07-29 19:39:35 -0500 (Thu, 29 Jul 2010) $
42   */
43  public class HtmlLayoutRenderer
44          extends HtmlRenderer
45  {
46      private static final Log log = LogFactory.getLog(HtmlLayoutRenderer.class);
47  
48      public static final String CLASSIC_LAYOUT = "classic";
49      public static final String NAV_RIGHT_LAYOUT = "navigationRight";
50      public static final String UPSIDE_DOWN_LAYOUT = "upsideDown";
51  
52      public boolean getRendersChildren()
53      {
54          return true;
55      }
56  
57      public void encodeBegin(FacesContext context, UIComponent component) throws IOException
58      {
59      }
60  
61      public void encodeChildren(FacesContext context, UIComponent component) throws IOException
62      {
63      }
64  
65      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException
66      {
67          RendererUtils.checkParamValidity(facesContext, component, HtmlPanelLayout.class);
68  
69          HtmlPanelLayout panelLayout = (HtmlPanelLayout)component;
70  
71          String layout = panelLayout.getLayout();
72          if (layout == null || layout.equals(CLASSIC_LAYOUT))
73          {
74              renderClassic(facesContext, panelLayout);
75          }
76          else if (layout.equals(NAV_RIGHT_LAYOUT))
77          {
78              renderNavRight(facesContext, panelLayout);
79          }
80          else if (layout.equals(UPSIDE_DOWN_LAYOUT))
81          {
82              renderUpsideDown(facesContext, panelLayout);
83          }
84          else
85          {
86              log.error("Unknown panel layout '" + layout + "'!");
87          }
88  
89          if (panelLayout.getChildCount() > 0)
90          {
91              log.error("PanelLayout must not have children, only facets allowed!");
92          }
93      }
94  
95      protected void renderClassic(FacesContext facesContext, HtmlPanelLayout panelLayout)
96              throws IOException
97      {
98          ResponseWriter writer = facesContext.getResponseWriter();
99          UIComponent header = panelLayout.getHeader();
100         UIComponent navigation = panelLayout.getNavigation();
101         UIComponent body = panelLayout.getBody();
102         UIComponent footer = panelLayout.getFooter();
103 
104         writer.startElement(HTML.TABLE_ELEM, panelLayout);
105         HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
106         HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
107         if (header != null)
108         {
109             writer.startElement(HTML.TR_ELEM, panelLayout);
110             renderTableCell(facesContext, writer, header,
111                             (navigation != null && body != null) ? 2 : 1,
112                             panelLayout.getHeaderClass(),
113                             panelLayout.getHeaderStyle());
114             writer.endElement(HTML.TR_ELEM);
115         }
116         if (navigation != null || body != null)
117         {
118             writer.startElement(HTML.TR_ELEM, panelLayout);
119             if (navigation != null)
120             {
121                 renderTableCell(facesContext, writer, navigation, 0,
122                                 panelLayout.getNavigationClass(),
123                                 panelLayout.getNavigationStyle());
124             }
125             if (body != null)
126             {
127                 renderTableCell(facesContext, writer, body, 0,
128                                 panelLayout.getBodyClass(),
129                                 panelLayout.getBodyStyle());
130             }
131             writer.endElement(HTML.TR_ELEM);
132         }
133         if (footer != null)
134         {
135             writer.startElement(HTML.TR_ELEM, panelLayout);
136             renderTableCell(facesContext, writer, footer,
137                             (navigation != null && body != null) ? 2 : 1,
138                             panelLayout.getFooterClass(),
139                             panelLayout.getFooterStyle());
140             writer.endElement(HTML.TR_ELEM);
141         }
142         writer.endElement(HTML.TABLE_ELEM);
143     }
144 
145 
146     protected void renderNavRight(FacesContext facesContext, HtmlPanelLayout panelLayout)
147             throws IOException
148     {
149         ResponseWriter writer = facesContext.getResponseWriter();
150         UIComponent header = panelLayout.getHeader();
151         UIComponent navigation = panelLayout.getNavigation();
152         UIComponent body = panelLayout.getBody();
153         UIComponent footer = panelLayout.getFooter();
154 
155         writer.startElement(HTML.TABLE_ELEM, panelLayout);
156         HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
157         HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
158         if (header != null)
159         {
160             writer.startElement(HTML.TR_ELEM, panelLayout);
161             renderTableCell(facesContext, writer, header,
162                             (navigation != null && body != null) ? 2 : 1,
163                             panelLayout.getHeaderClass(),
164                             panelLayout.getHeaderStyle());
165             writer.endElement(HTML.TR_ELEM);
166         }
167         if (navigation != null || body != null)
168         {
169             writer.startElement(HTML.TR_ELEM, panelLayout);
170             if (body != null)
171             {
172                 renderTableCell(facesContext, writer, body, 0,
173                                 panelLayout.getBodyClass(),
174                                 panelLayout.getBodyStyle());
175             }
176             if (navigation != null)
177             {
178                 renderTableCell(facesContext, writer, navigation, 0,
179                                 panelLayout.getNavigationClass(),
180                                 panelLayout.getNavigationStyle());
181             }
182             writer.endElement(HTML.TR_ELEM);
183         }
184         if (footer != null)
185         {
186             writer.startElement(HTML.TR_ELEM, panelLayout);
187             renderTableCell(facesContext, writer, footer,
188                             (navigation != null && body != null) ? 2 : 1,
189                             panelLayout.getFooterClass(),
190                             panelLayout.getFooterStyle());
191             writer.endElement(HTML.TR_ELEM);
192         }
193         writer.endElement(HTML.TABLE_ELEM);
194     }
195 
196 
197     protected void renderUpsideDown(FacesContext facesContext, HtmlPanelLayout panelLayout)
198             throws IOException
199     {
200         ResponseWriter writer = facesContext.getResponseWriter();
201         UIComponent header = panelLayout.getHeader();
202         UIComponent navigation = panelLayout.getNavigation();
203         UIComponent body = panelLayout.getBody();
204         UIComponent footer = panelLayout.getFooter();
205 
206         writer.startElement(HTML.TABLE_ELEM, panelLayout);
207         HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
208         HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
209         if (footer != null)
210         {
211             writer.startElement(HTML.TR_ELEM, panelLayout);
212             renderTableCell(facesContext, writer, footer,
213                             (navigation != null && body != null) ? 2 : 1,
214                             panelLayout.getFooterClass(),
215                             panelLayout.getFooterStyle());
216             writer.endElement(HTML.TR_ELEM);
217         }
218         if (navigation != null || body != null)
219         {
220             writer.startElement(HTML.TR_ELEM, panelLayout);
221             if (navigation != null)
222             {
223                 renderTableCell(facesContext, writer, navigation, 0,
224                                 panelLayout.getNavigationClass(),
225                                 panelLayout.getNavigationStyle());
226             }
227             if (body != null)
228             {
229                 renderTableCell(facesContext, writer, body, 0,
230                                 panelLayout.getBodyClass(),
231                                 panelLayout.getBodyStyle());
232             }
233             writer.endElement(HTML.TR_ELEM);
234         }
235         if (header != null)
236         {
237             writer.startElement(HTML.TR_ELEM, panelLayout);
238             renderTableCell(facesContext, writer, header,
239                             (navigation != null && body != null) ? 2 : 1,
240                             panelLayout.getHeaderClass(),
241                             panelLayout.getHeaderStyle());
242             writer.endElement(HTML.TR_ELEM);
243         }
244         writer.endElement(HTML.TABLE_ELEM);
245     }
246 
247 
248     protected void renderTableCell(FacesContext facesContext,
249                                    ResponseWriter writer,
250                                    UIComponent component,
251                                    int colspan,
252                                    String styleClass,
253                                    String style)
254             throws IOException
255     {
256         writer.startElement(HTML.TD_ELEM, component);
257         if (colspan > 0)
258         {
259             writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(colspan), null);
260         }
261         if (styleClass != null)
262         {
263             writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null);
264         }
265         if (style != null)
266         {
267             writer.writeAttribute(HTML.STYLE_ATTR, style, null);
268         }
269 
270         RendererUtils.renderChild(facesContext, component);
271 
272         writer.endElement(HTML.TD_ELEM);
273     }
274 
275 }