KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > layout > HtmlLayoutRenderer


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.custom.layout;
17
18 import org.apache.myfaces.renderkit.RendererUtils;
19 import org.apache.myfaces.renderkit.html.HTML;
20 import org.apache.myfaces.renderkit.html.HtmlRenderer;
21 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * @author Manfred Geiler (latest modification by $Author: matze $)
33  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:57 $
34  */

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