📄 htmlnavigationrenderer.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.myfaces.custom.navigation;import org.apache.myfaces.renderkit.RendererUtils;import org.apache.myfaces.renderkit.html.HTML;import org.apache.myfaces.renderkit.html.HtmlRendererUtils;import org.apache.myfaces.renderkit.html.ext.HtmlLinkRenderer;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import javax.faces.component.UIComponent;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import java.io.IOException;import java.util.Iterator;import java.util.List;/** * @author Manfred Geiler (latest modification by $Author: oros $) * @version $Revision: 1.5 $ $Date: 2005/02/16 00:50:37 $ * $Log: HtmlNavigationRenderer.java,v $ * Revision 1.5 2005/02/16 00:50:37 oros * SF issue #1043331: replaced all by the corresponding numeric entity   so safari users will be happy, too, with MyFaces output * * Revision 1.4 2004/10/13 11:50:57 matze * renamed packages to org.apache * * Revision 1.3 2004/07/01 21:53:08 mwessendorf * ASF switch * * Revision 1.2 2004/05/18 17:08:21 manolito * no message * */public class HtmlNavigationRenderer extends HtmlLinkRenderer{ private static final Log log = LogFactory.getLog(HtmlNavigationRenderer.class); private static final Integer ZERO_INTEGER = new Integer(0); public boolean getRendersChildren() { return true; } public void decode(FacesContext facesContext, UIComponent component) { if (component instanceof HtmlCommandNavigation) { //HtmlCommandNavigation super.decode(facesContext, component); } } public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException { if (component instanceof HtmlCommandNavigation) { //HtmlCommandNavigation super.encodeBegin(facesContext, component); } } public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException { if (component instanceof HtmlCommandNavigation) { //HtmlCommandNavigation super.encodeChildren(facesContext, component); } } public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException { if (component instanceof HtmlCommandNavigation) { //HtmlCommandNavigation super.encodeEnd(facesContext, component); return; } RendererUtils.checkParamValidity(facesContext, component, HtmlPanelNavigation.class); ResponseWriter writer = facesContext.getResponseWriter(); HtmlPanelNavigation panelNav = (HtmlPanelNavigation)component; if (panelNav.getChildCount() > 0) { HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.startElement(HTML.TABLE_ELEM, null); HtmlRendererUtils.renderHTMLAttributes(writer, panelNav, HTML.TABLE_PASSTHROUGH_ATTRIBUTES); if (panelNav.getStyle() == null && panelNav.getStyleClass() == null) { writer.writeAttribute(HTML.BORDER_ATTR, ZERO_INTEGER, null); } renderChildren(facesContext, writer, panelNav, panelNav.getChildren(), 0); HtmlRendererUtils.writePrettyLineSeparator(facesContext); writer.endElement(HTML.TABLE_ELEM); } else { if (log.isWarnEnabled()) log.warn("Navigation panel without children."); } } protected void renderChildren(FacesContext facesContext, ResponseWriter writer, HtmlPanelNavigation panelNav, List children, int level) throws IOException { for (Iterator it = children.iterator(); it.hasNext(); ) { UIComponent child = (UIComponent)it.next(); if (!child.isRendered()) continue; if (child instanceof HtmlCommandNavigation) { //navigation item HtmlRendererUtils.writePrettyLineSeparator(facesContext); String style = getNavigationItemStyle(panelNav, (HtmlCommandNavigation)child); String styleClass = getNavigationItemClass(panelNav, (HtmlCommandNavigation)child); writer.startElement(HTML.TR_ELEM, null); writer.startElement(HTML.TD_ELEM, null); writeStyleAttributes(writer, style, styleClass); if (style != null || styleClass != null) { writer.startElement(HTML.SPAN_ELEM, null); writeStyleAttributes(writer, style, styleClass); } indent(writer, level); child.encodeBegin(facesContext); child.encodeEnd(facesContext); if (style != null || styleClass != null) { writer.endElement(HTML.SPAN_ELEM); } writer.endElement(HTML.TD_ELEM); writer.endElement(HTML.TR_ELEM); if (child.getChildCount() > 0) { renderChildren(facesContext, writer, panelNav, child.getChildren(), level + 1); } } else { //separator HtmlRendererUtils.writePrettyLineSeparator(facesContext); String style = panelNav.getSeparatorStyle(); String styleClass = panelNav.getSeparatorClass(); writer.startElement(HTML.TR_ELEM, null); writer.startElement(HTML.TD_ELEM, null); writeStyleAttributes(writer, style, styleClass); if (style != null || styleClass != null) { writer.startElement(HTML.SPAN_ELEM, null); writeStyleAttributes(writer, style, styleClass); } indent(writer, level); RendererUtils.renderChild(facesContext, child); if (style != null || styleClass != null) { writer.endElement(HTML.SPAN_ELEM); } writer.endElement(HTML.TD_ELEM); writer.endElement(HTML.TR_ELEM); } } } protected void indent(ResponseWriter writer, int level) throws IOException { StringBuffer buf = new StringBuffer(); for (int i = 0; i < level; i++) { buf.append("    "); } writer.write(buf.toString()); } protected String getNavigationItemStyle(HtmlPanelNavigation navPanel, HtmlCommandNavigation navItem) { if (navItem.isActive()) { return navPanel.getActiveItemStyle(); } else if (navItem.isOpen()) { return navPanel.getOpenItemStyle(); } else { return navPanel.getItemStyle(); } } protected String getNavigationItemClass(HtmlPanelNavigation navPanel, HtmlCommandNavigation navItem) { if (navItem.isActive()) { return navPanel.getActiveItemClass(); } else if (navItem.isOpen()) { return navPanel.getOpenItemClass(); } else { return navPanel.getItemClass(); } } protected void writeStyleAttributes(ResponseWriter writer, String style, String styleClass) throws IOException { HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style); HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass); } protected String getStyle(FacesContext facesContext, UIComponent link) { if (!(link instanceof HtmlCommandNavigation)) { throw new IllegalArgumentException(); } UIComponent navPanel = link.getParent(); while (navPanel != null && !(navPanel instanceof HtmlPanelNavigation)) { navPanel = navPanel.getParent(); } if (navPanel == null) { throw new IllegalStateException("HtmlCommandNavigation not nested in HtmlPanelNavigation!?"); } HtmlCommandNavigation navItem = (HtmlCommandNavigation)link; if (navItem.isActive()) { return ((HtmlPanelNavigation)navPanel).getActiveItemStyle(); } else if (navItem.isOpen()) { return ((HtmlPanelNavigation)navPanel).getOpenItemStyle(); } else { return ((HtmlPanelNavigation)navPanel).getItemStyle(); } } protected String getStyleClass(FacesContext facesContext, UIComponent link) { if (!(link instanceof HtmlCommandNavigation)) { throw new IllegalArgumentException(); } UIComponent navPanel = link.getParent(); while (navPanel != null && !(navPanel instanceof HtmlPanelNavigation)) { navPanel = navPanel.getParent(); } if (navPanel == null) { throw new IllegalStateException("HtmlCommandNavigation not nested in HtmlPanelNavigation!?"); } HtmlCommandNavigation navItem = (HtmlCommandNavigation)link; if (navItem.isActive()) { return ((HtmlPanelNavigation)navPanel).getActiveItemClass(); } else if (navItem.isOpen()) { return ((HtmlPanelNavigation)navPanel).getOpenItemClass(); } else { return ((HtmlPanelNavigation)navPanel).getItemClass(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -