1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.layout;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.behavior.ClientBehavior;
27 import javax.faces.component.behavior.ClientBehaviorHolder;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
34 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
35 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
36 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
37 import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
38
39
40
41
42
43
44
45
46
47
48 public class HtmlLayoutRenderer
49 extends HtmlRenderer
50 {
51 private static final Log log = LogFactory.getLog(HtmlLayoutRenderer.class);
52
53 public static final String CLASSIC_LAYOUT = "classic";
54 public static final String NAV_RIGHT_LAYOUT = "navigationRight";
55 public static final String UPSIDE_DOWN_LAYOUT = "upsideDown";
56
57 public boolean getRendersChildren()
58 {
59 return true;
60 }
61
62 @Override
63 public void decode(FacesContext context, UIComponent component)
64 {
65 super.decode(context, component);
66
67 HtmlRendererUtils.decodeClientBehaviors(context, component);
68 }
69
70 public void encodeBegin(FacesContext context, UIComponent component) throws IOException
71 {
72 }
73
74 public void encodeChildren(FacesContext context, UIComponent component) throws IOException
75 {
76 }
77
78 public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException
79 {
80 RendererUtils.checkParamValidity(facesContext, component, HtmlPanelLayout.class);
81
82 HtmlPanelLayout panelLayout = (HtmlPanelLayout)component;
83
84 Map<String, List<ClientBehavior>> behaviors = null;
85 if (component instanceof ClientBehaviorHolder)
86 {
87 behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
88 if (!behaviors.isEmpty())
89 {
90 ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
91 }
92 }
93
94 String layout = panelLayout.getLayout();
95 if (layout == null || layout.equals(CLASSIC_LAYOUT))
96 {
97 renderClassic(facesContext, panelLayout);
98 }
99 else if (layout.equals(NAV_RIGHT_LAYOUT))
100 {
101 renderNavRight(facesContext, panelLayout);
102 }
103 else if (layout.equals(UPSIDE_DOWN_LAYOUT))
104 {
105 renderUpsideDown(facesContext, panelLayout);
106 }
107 else
108 {
109 log.error("Unknown panel layout '" + layout + "'!");
110 }
111
112 if (panelLayout.getChildCount() > 0)
113 {
114 log.error("PanelLayout must not have children, only facets allowed!");
115 }
116 }
117
118 protected void renderClassic(FacesContext facesContext, HtmlPanelLayout panelLayout)
119 throws IOException
120 {
121 ResponseWriter writer = facesContext.getResponseWriter();
122 UIComponent header = panelLayout.getHeader();
123 UIComponent navigation = panelLayout.getNavigation();
124 UIComponent body = panelLayout.getBody();
125 UIComponent footer = panelLayout.getFooter();
126
127 writer.startElement(HTML.TABLE_ELEM, panelLayout);
128
129 Map<String, List<ClientBehavior>> behaviors = panelLayout.getClientBehaviors();
130
131 if (behaviors != null && !behaviors.isEmpty())
132 {
133 writer.writeAttribute(HTML.ID_ATTR, panelLayout.getClientId(facesContext),null);
134 HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, panelLayout, behaviors);
135 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
136 }
137 else
138 {
139 HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
140 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
141 }
142 if (header != null)
143 {
144 writer.startElement(HTML.TR_ELEM, panelLayout);
145 renderTableCell(facesContext, writer, header,
146 (navigation != null && body != null) ? 2 : 1,
147 panelLayout.getHeaderClass(),
148 panelLayout.getHeaderStyle());
149 writer.endElement(HTML.TR_ELEM);
150 }
151 if (navigation != null || body != null)
152 {
153 writer.startElement(HTML.TR_ELEM, panelLayout);
154 if (navigation != null)
155 {
156 renderTableCell(facesContext, writer, navigation, 0,
157 panelLayout.getNavigationClass(),
158 panelLayout.getNavigationStyle());
159 }
160 if (body != null)
161 {
162 renderTableCell(facesContext, writer, body, 0,
163 panelLayout.getBodyClass(),
164 panelLayout.getBodyStyle());
165 }
166 writer.endElement(HTML.TR_ELEM);
167 }
168 if (footer != null)
169 {
170 writer.startElement(HTML.TR_ELEM, panelLayout);
171 renderTableCell(facesContext, writer, footer,
172 (navigation != null && body != null) ? 2 : 1,
173 panelLayout.getFooterClass(),
174 panelLayout.getFooterStyle());
175 writer.endElement(HTML.TR_ELEM);
176 }
177 writer.endElement(HTML.TABLE_ELEM);
178 }
179
180
181 protected void renderNavRight(FacesContext facesContext, HtmlPanelLayout panelLayout)
182 throws IOException
183 {
184 ResponseWriter writer = facesContext.getResponseWriter();
185 UIComponent header = panelLayout.getHeader();
186 UIComponent navigation = panelLayout.getNavigation();
187 UIComponent body = panelLayout.getBody();
188 UIComponent footer = panelLayout.getFooter();
189
190 writer.startElement(HTML.TABLE_ELEM, panelLayout);
191
192 Map<String, List<ClientBehavior>> behaviors = panelLayout.getClientBehaviors();
193
194 if (behaviors != null && !behaviors.isEmpty())
195 {
196 writer.writeAttribute(HTML.ID_ATTR, panelLayout.getClientId(facesContext),null);
197 HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, panelLayout, behaviors);
198 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
199 }
200 else
201 {
202 HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
203 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
204 }
205 if (header != null)
206 {
207 writer.startElement(HTML.TR_ELEM, panelLayout);
208 renderTableCell(facesContext, writer, header,
209 (navigation != null && body != null) ? 2 : 1,
210 panelLayout.getHeaderClass(),
211 panelLayout.getHeaderStyle());
212 writer.endElement(HTML.TR_ELEM);
213 }
214 if (navigation != null || body != null)
215 {
216 writer.startElement(HTML.TR_ELEM, panelLayout);
217 if (body != null)
218 {
219 renderTableCell(facesContext, writer, body, 0,
220 panelLayout.getBodyClass(),
221 panelLayout.getBodyStyle());
222 }
223 if (navigation != null)
224 {
225 renderTableCell(facesContext, writer, navigation, 0,
226 panelLayout.getNavigationClass(),
227 panelLayout.getNavigationStyle());
228 }
229 writer.endElement(HTML.TR_ELEM);
230 }
231 if (footer != null)
232 {
233 writer.startElement(HTML.TR_ELEM, panelLayout);
234 renderTableCell(facesContext, writer, footer,
235 (navigation != null && body != null) ? 2 : 1,
236 panelLayout.getFooterClass(),
237 panelLayout.getFooterStyle());
238 writer.endElement(HTML.TR_ELEM);
239 }
240 writer.endElement(HTML.TABLE_ELEM);
241 }
242
243
244 protected void renderUpsideDown(FacesContext facesContext, HtmlPanelLayout panelLayout)
245 throws IOException
246 {
247 ResponseWriter writer = facesContext.getResponseWriter();
248 UIComponent header = panelLayout.getHeader();
249 UIComponent navigation = panelLayout.getNavigation();
250 UIComponent body = panelLayout.getBody();
251 UIComponent footer = panelLayout.getFooter();
252
253 writer.startElement(HTML.TABLE_ELEM, panelLayout);
254
255 Map<String, List<ClientBehavior>> behaviors = panelLayout.getClientBehaviors();
256
257 if (behaviors != null && !behaviors.isEmpty())
258 {
259 writer.writeAttribute(HTML.ID_ATTR, panelLayout.getClientId(facesContext),null);
260 HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, panelLayout, behaviors);
261 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES_WITHOUT_EVENTS);
262 }
263 else
264 {
265 HtmlRendererUtils.writeIdIfNecessary(writer, panelLayout, facesContext);
266 HtmlRendererUtils.renderHTMLAttributes(writer, panelLayout, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
267 }
268 if (footer != null)
269 {
270 writer.startElement(HTML.TR_ELEM, panelLayout);
271 renderTableCell(facesContext, writer, footer,
272 (navigation != null && body != null) ? 2 : 1,
273 panelLayout.getFooterClass(),
274 panelLayout.getFooterStyle());
275 writer.endElement(HTML.TR_ELEM);
276 }
277 if (navigation != null || body != null)
278 {
279 writer.startElement(HTML.TR_ELEM, panelLayout);
280 if (navigation != null)
281 {
282 renderTableCell(facesContext, writer, navigation, 0,
283 panelLayout.getNavigationClass(),
284 panelLayout.getNavigationStyle());
285 }
286 if (body != null)
287 {
288 renderTableCell(facesContext, writer, body, 0,
289 panelLayout.getBodyClass(),
290 panelLayout.getBodyStyle());
291 }
292 writer.endElement(HTML.TR_ELEM);
293 }
294 if (header != null)
295 {
296 writer.startElement(HTML.TR_ELEM, panelLayout);
297 renderTableCell(facesContext, writer, header,
298 (navigation != null && body != null) ? 2 : 1,
299 panelLayout.getHeaderClass(),
300 panelLayout.getHeaderStyle());
301 writer.endElement(HTML.TR_ELEM);
302 }
303 writer.endElement(HTML.TABLE_ELEM);
304 }
305
306
307 protected void renderTableCell(FacesContext facesContext,
308 ResponseWriter writer,
309 UIComponent component,
310 int colspan,
311 String styleClass,
312 String style)
313 throws IOException
314 {
315 writer.startElement(HTML.TD_ELEM, component);
316 if (colspan > 0)
317 {
318 writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(colspan), null);
319 }
320 if (styleClass != null)
321 {
322 writer.writeAttribute(HTML.CLASS_ATTR, styleClass, null);
323 }
324 if (style != null)
325 {
326 writer.writeAttribute(HTML.STYLE_ATTR, style, null);
327 }
328
329 RendererUtils.renderChild(facesContext, component);
330
331 writer.endElement(HTML.TD_ELEM);
332 }
333
334 }