KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > navigation > HtmlNavigationRenderer


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.navigation;
17
18 import org.apache.myfaces.renderkit.RendererUtils;
19 import org.apache.myfaces.renderkit.html.HTML;
20 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
21 import org.apache.myfaces.renderkit.html.ext.HtmlLinkRenderer;
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 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * @author Manfred Geiler (latest modification by $Author: oros $)
35  * @version $Revision: 1.5 $ $Date: 2005/02/16 00:50:37 $
36  * $Log: HtmlNavigationRenderer.java,v $
37  * Revision 1.5 2005/02/16 00:50:37 oros
38  * SF issue #1043331: replaced all   by the corresponding numeric entity   so safari users will be happy, too, with MyFaces output
39  *
40  * Revision 1.4 2004/10/13 11:50:57 matze
41  * renamed packages to org.apache
42  *
43  * Revision 1.3 2004/07/01 21:53:08 mwessendorf
44  * ASF switch
45  *
46  * Revision 1.2 2004/05/18 17:08:21 manolito
47  * no message
48  *
49  */

50 public class HtmlNavigationRenderer
51         extends HtmlLinkRenderer
52 {
53     private static final Log log = LogFactory.getLog(HtmlNavigationRenderer.class);
54
55     private static final Integer JavaDoc ZERO_INTEGER = new Integer JavaDoc(0);
56
57     public boolean getRendersChildren()
58     {
59         return true;
60     }
61
62     public void decode(FacesContext facesContext, UIComponent component)
63     {
64         if (component instanceof HtmlCommandNavigation)
65         {
66             //HtmlCommandNavigation
67
super.decode(facesContext, component);
68         }
69     }
70
71     public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException JavaDoc
72     {
73         if (component instanceof HtmlCommandNavigation)
74         {
75             //HtmlCommandNavigation
76
super.encodeBegin(facesContext, component);
77         }
78     }
79
80     public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException JavaDoc
81     {
82         if (component instanceof HtmlCommandNavigation)
83         {
84             //HtmlCommandNavigation
85
super.encodeChildren(facesContext, component);
86         }
87     }
88
89     public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException JavaDoc
90     {
91         if (component instanceof HtmlCommandNavigation)
92         {
93             //HtmlCommandNavigation
94
super.encodeEnd(facesContext, component);
95             return;
96         }
97
98         RendererUtils.checkParamValidity(facesContext, component, HtmlPanelNavigation.class);
99         ResponseWriter writer = facesContext.getResponseWriter();
100         HtmlPanelNavigation panelNav = (HtmlPanelNavigation)component;
101
102         if (panelNav.getChildCount() > 0)
103         {
104             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
105             writer.startElement(HTML.TABLE_ELEM, null);
106             HtmlRendererUtils.renderHTMLAttributes(writer, panelNav, HTML.TABLE_PASSTHROUGH_ATTRIBUTES);
107             if (panelNav.getStyle() == null && panelNav.getStyleClass() == null)
108             {
109                 writer.writeAttribute(HTML.BORDER_ATTR, ZERO_INTEGER, null);
110             }
111
112             renderChildren(facesContext, writer, panelNav, panelNav.getChildren(), 0);
113
114             HtmlRendererUtils.writePrettyLineSeparator(facesContext);
115             writer.endElement(HTML.TABLE_ELEM);
116         }
117         else
118         {
119             if (log.isWarnEnabled()) log.warn("Navigation panel without children.");
120         }
121     }
122
123
124     protected void renderChildren(FacesContext facesContext,
125                                   ResponseWriter writer,
126                                   HtmlPanelNavigation panelNav,
127                                   List JavaDoc children,
128                                   int level)
129             throws IOException JavaDoc
130     {
131         for (Iterator JavaDoc it = children.iterator(); it.hasNext(); )
132         {
133             UIComponent child = (UIComponent)it.next();
134             if (!child.isRendered()) continue;
135             if (child instanceof HtmlCommandNavigation)
136             {
137                 //navigation item
138
HtmlRendererUtils.writePrettyLineSeparator(facesContext);
139
140                 String JavaDoc style = getNavigationItemStyle(panelNav, (HtmlCommandNavigation)child);
141                 String JavaDoc styleClass = getNavigationItemClass(panelNav, (HtmlCommandNavigation)child);
142
143                 writer.startElement(HTML.TR_ELEM, null);
144                 writer.startElement(HTML.TD_ELEM, null);
145                 writeStyleAttributes(writer, style, styleClass);
146
147                 if (style != null || styleClass != null)
148                 {
149                     writer.startElement(HTML.SPAN_ELEM, null);
150                     writeStyleAttributes(writer, style, styleClass);
151                 }
152                 indent(writer, level);
153                 child.encodeBegin(facesContext);
154                 child.encodeEnd(facesContext);
155                 if (style != null || styleClass != null)
156                 {
157                     writer.endElement(HTML.SPAN_ELEM);
158                 }
159
160                 writer.endElement(HTML.TD_ELEM);
161                 writer.endElement(HTML.TR_ELEM);
162
163                 if (child.getChildCount() > 0)
164                 {
165                     renderChildren(facesContext, writer, panelNav, child.getChildren(), level + 1);
166                 }
167             }
168             else
169             {
170                 //separator
171
HtmlRendererUtils.writePrettyLineSeparator(facesContext);
172
173                 String JavaDoc style = panelNav.getSeparatorStyle();
174                 String JavaDoc styleClass = panelNav.getSeparatorClass();
175
176                 writer.startElement(HTML.TR_ELEM, null);
177                 writer.startElement(HTML.TD_ELEM, null);
178                 writeStyleAttributes(writer, style, styleClass);
179
180                 if (style != null || styleClass != null)
181                 {
182                     writer.startElement(HTML.SPAN_ELEM, null);
183                     writeStyleAttributes(writer, style, styleClass);
184                 }
185                 indent(writer, level);
186                 RendererUtils.renderChild(facesContext, child);
187                 if (style != null || styleClass != null)
188                 {
189                     writer.endElement(HTML.SPAN_ELEM);
190                 }
191
192                 writer.endElement(HTML.TD_ELEM);
193                 writer.endElement(HTML.TR_ELEM);
194             }
195
196         }
197     }
198
199
200     protected void indent(ResponseWriter writer, int level)
201         throws IOException JavaDoc
202     {
203         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
204         for (int i = 0; i < level; i++)
205         {
206             buf.append("&#160;&#160;&#160;&#160;");
207         }
208         writer.write(buf.toString());
209     }
210
211
212
213     protected String JavaDoc getNavigationItemStyle(HtmlPanelNavigation navPanel,
214                                             HtmlCommandNavigation navItem)
215     {
216         if (navItem.isActive())
217         {
218             return navPanel.getActiveItemStyle();
219         }
220         else if (navItem.isOpen())
221         {
222             return navPanel.getOpenItemStyle();
223         }
224         else
225         {
226             return navPanel.getItemStyle();
227         }
228     }
229
230     protected String JavaDoc getNavigationItemClass(HtmlPanelNavigation navPanel,
231                                             HtmlCommandNavigation navItem)
232     {
233         if (navItem.isActive())
234         {
235             return navPanel.getActiveItemClass();
236         }
237         else if (navItem.isOpen())
238         {
239             return navPanel.getOpenItemClass();
240         }
241         else
242         {
243             return navPanel.getItemClass();
244         }
245     }
246
247
248
249     protected void writeStyleAttributes(ResponseWriter writer,
250                                         String JavaDoc style,
251                                         String JavaDoc styleClass)
252             throws IOException JavaDoc
253     {
254         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style);
255         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass);
256     }
257
258
259     protected String JavaDoc getStyle(FacesContext facesContext, UIComponent link)
260     {
261         if (!(link instanceof HtmlCommandNavigation))
262         {
263             throw new IllegalArgumentException JavaDoc();
264         }
265
266         UIComponent navPanel = link.getParent();
267         while (navPanel != null && !(navPanel instanceof HtmlPanelNavigation))
268         {
269             navPanel = navPanel.getParent();
270         }
271         if (navPanel == null)
272         {
273             throw new IllegalStateException JavaDoc("HtmlCommandNavigation not nested in HtmlPanelNavigation!?");
274         }
275
276         HtmlCommandNavigation navItem = (HtmlCommandNavigation)link;
277         if (navItem.isActive())
278         {
279             return ((HtmlPanelNavigation)navPanel).getActiveItemStyle();
280         }
281         else if (navItem.isOpen())
282         {
283             return ((HtmlPanelNavigation)navPanel).getOpenItemStyle();
284         }
285         else
286         {
287             return ((HtmlPanelNavigation)navPanel).getItemStyle();
288         }
289     }
290
291
292     protected String JavaDoc getStyleClass(FacesContext facesContext, UIComponent link)
293     {
294         if (!(link instanceof HtmlCommandNavigation))
295         {
296             throw new IllegalArgumentException JavaDoc();
297         }
298
299         UIComponent navPanel = link.getParent();
300         while (navPanel != null && !(navPanel instanceof HtmlPanelNavigation))
301         {
302             navPanel = navPanel.getParent();
303         }
304         if (navPanel == null)
305         {
306             throw new IllegalStateException JavaDoc("HtmlCommandNavigation not nested in HtmlPanelNavigation!?");
307         }
308
309         HtmlCommandNavigation navItem = (HtmlCommandNavigation)link;
310         if (navItem.isActive())
311         {
312             return ((HtmlPanelNavigation)navPanel).getActiveItemClass();
313         }
314         else if (navItem.isOpen())
315         {
316             return ((HtmlPanelNavigation)navPanel).getOpenItemClass();
317         }
318         else
319         {
320             return ((HtmlPanelNavigation)navPanel).getItemClass();
321         }
322     }
323
324
325
326 }
327
Popular Tags