1 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 ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 50 public class HtmlNavigationRenderer 51 extends HtmlLinkRenderer 52 { 53 private static final Log log = LogFactory.getLog(HtmlNavigationRenderer.class); 54 55 private static final Integer ZERO_INTEGER = new Integer (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 super.decode(facesContext, component); 68 } 69 } 70 71 public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException 72 { 73 if (component instanceof HtmlCommandNavigation) 74 { 75 super.encodeBegin(facesContext, component); 77 } 78 } 79 80 public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException 81 { 82 if (component instanceof HtmlCommandNavigation) 83 { 84 super.encodeChildren(facesContext, component); 86 } 87 } 88 89 public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException 90 { 91 if (component instanceof HtmlCommandNavigation) 92 { 93 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 children, 128 int level) 129 throws IOException 130 { 131 for (Iterator it = children.iterator(); it.hasNext(); ) 132 { 133 UIComponent child = (UIComponent)it.next(); 134 if (!child.isRendered()) continue; 135 if (child instanceof HtmlCommandNavigation) 136 { 137 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 139 140 String style = getNavigationItemStyle(panelNav, (HtmlCommandNavigation)child); 141 String 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 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 172 173 String style = panelNav.getSeparatorStyle(); 174 String 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 202 { 203 StringBuffer buf = new StringBuffer (); 204 for (int i = 0; i < level; i++) 205 { 206 buf.append("    "); 207 } 208 writer.write(buf.toString()); 209 } 210 211 212 213 protected String 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 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 style, 251 String styleClass) 252 throws IOException 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 getStyle(FacesContext facesContext, UIComponent link) 260 { 261 if (!(link instanceof HtmlCommandNavigation)) 262 { 263 throw new IllegalArgumentException (); 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 ("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 getStyleClass(FacesContext facesContext, UIComponent link) 293 { 294 if (!(link instanceof HtmlCommandNavigation)) 295 { 296 throw new IllegalArgumentException (); 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 ("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 |