Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright ${year}, Red Hat, Inc. and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.richfaces.renderkit.html;
 
 import static org.richfaces.component.AbstractTogglePanelTitledItem.HeaderStates.active;
 import static org.richfaces.component.AbstractTogglePanelTitledItem.HeaderStates.disabled;
 import static org.richfaces.component.AbstractTogglePanelTitledItem.HeaderStates.inactive;
 import static org.richfaces.renderkit.HtmlConstants.CLASS_ATTRIBUTE;
 import static org.richfaces.renderkit.HtmlConstants.DIV_ELEM;
 import static org.richfaces.renderkit.HtmlConstants.ID_ATTRIBUTE;
 import static org.richfaces.renderkit.HtmlConstants.SPAN_ELEM;
 import static org.richfaces.renderkit.HtmlConstants.STYLE_ATTRIBUTE;
 import static org.richfaces.renderkit.HtmlConstants.TBODY_ELEMENT;
 import static org.richfaces.renderkit.HtmlConstants.TD_ELEM;
 import static org.richfaces.renderkit.HtmlConstants.TR_ELEMENT;
 import static org.richfaces.renderkit.RenderKitUtils.renderPassThroughAttributes;
 
 import java.util.Map;
 
 import  javax.faces.FacesException;
 import  javax.faces.application.ResourceDependencies;
 import  javax.faces.application.ResourceDependency;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.visit.VisitResult;
 import  javax.faces.context.FacesContext;
 import  javax.faces.context.ResponseWriter;
 
 import  org.ajax4jsf.javascript.JSObject;
 import  org.richfaces.renderkit.HtmlConstants;

Author(s):
akolonitsky
Brian Leathem
 
 @ResourceDependencies({ @ResourceDependency(library = "org.richfaces", name = "ajax.reslib"),
         @ResourceDependency(library = "org.richfaces", name = "base-component.reslib"),
         @ResourceDependency(name = "richfaces-event.js"),
         @ResourceDependency(library = "org.richfaces", name = "togglePanel.js"),
         @ResourceDependency(library = "org.richfaces", name = "tabPanel.js"),
         @ResourceDependency(library = "org.richfaces", name = "tabPanel.ecss") })
 @JsfRenderer(type = "org.richfaces.TabPanelRenderer", family = .)
 public class TabPanelRenderer extends TogglePanelRenderer {
     private static final RenderKitUtils.Attributes HEADER_ATTRIBUTES = RenderKitUtils.attributes()
             .generic("onclick""onheaderclick""headerclick").generic("ondblclick""onheaderdblclick""headerdblclick")
             .generic("onmousedown""onheadermousedown""headermousedown")
             .generic("onmousemove""onheadermousemove""headermousemove")
             .generic("onmouseup""onheadermouseup""headermouseup");
     private static final String DIV = DIV_ELEM;
     private static final String STYLE = STYLE_ATTRIBUTE;
     private static final String CLASS = CLASS_ATTRIBUTE;
 
     @Override
     protected boolean isSubmitted(FacesContext contextAbstractTogglePanel panel) {
         String activePanelName = panel.getSubmittedActiveItem();
         String clientId = panel.getClientIdByName(activePanelName);
         if (clientId == null) {
             return false;
         }
         Map<StringStringparameterMap = context.getExternalContext().getRequestParameterMap();
         return parameterMap.get(clientId) != null;
     }
 
     @Override
     protected void doEncodeBegin(ResponseWriter w, FacesContext context, UIComponent componentthrows IOException {
         super.doEncodeBegin(wcontextcomponent);
 
         AbstractTabPanel tabPanel = (AbstractTabPanelcomponent;
         if (tabPanel.isHeaderPositionedTop()) {
             writeTabsLine(wcontextcomponent);
            writeTabsLineSeparator(wcomponent);
        }
    }
    private void writeTabsLineSeparator(ResponseWriter writer, UIComponent componentthrows IOException {
        writer.startElement(component);
        writer.writeAttribute(CLASS_ATTRIBUTE, "rf-tab-hdr-brd"null);
        writer.endElement();
    }
    private void writeTabsLine(ResponseWriter w, FacesContext context, UIComponent compthrows IOException {
        w.startElement(comp);
        AbstractTabPanel tabPanel = (AbstractTabPanelcomp;
        if (tabPanel.isHeaderPositionedTop()) {
            w.writeAttribute("rf-tab-hdr-tabline-vis rf-tab-hdr-tabline-top"null);
        } else {
            w.writeAttribute("rf-tab-hdr-tabline-vis rf-tab-hdr-tabline-btm"null);
        }
        w.startElement("table"comp);
        w.writeAttribute(CLASS_ATTRIBUTE, "rf-tab-hdr-tabs"null);
        w.writeAttribute("cellspacing""0"null);
        w.startElement(TBODY_ELEMENT, comp);
        w.startElement(TR_ELEMENT, comp);
        writeTopTabFirstSpacer(wcomp);
        writeTabLine(wcontexttabPanel);
        writeTopTabLastSpacer(wcomp);
        w.endElement(TR_ELEMENT);
        w.endElement(TBODY_ELEMENT);
        w.endElement("table");
        writeTopTabsControl(wcomp"rf-tab-hdr-scrl-lft rf-tab-hdn""\u00AB");
        writeTopTabsControl(wcomp"rf-tab-hdr-tablst rf-tab-hdn""\u2193");
        writeTopTabsControl(wcomp"rf-tab-hdr-scrl-rgh rf-tab-hdn""\u00BB");
        w.endElement(DIV_ELEM);
    }
    private void writeTabLine(final ResponseWriter wfinal FacesContext contextfinal AbstractTabPanel panelthrows IOException {
        panel.visitTogglePanelItems(panelnew TogglePanelVisitCallback() {
            @Override
            public VisitResult visit(FacesContext contextTogglePanelVisitState visitState) {
                AbstractTogglePanelItemInterface item = visitState.getItem();
                if (item.isRendered() && item instanceof AbstractTab) {
                    try {
                        AbstractTab tab = (AbstractTabitem;
                        writeTopTabHeader(contextwtab);
                        writeTopTabSpacer(wpanel);
                    } catch (IOException e) {
                        throw new FacesException(e);
                    }
                }
                return VisitResult.ACCEPT;
            }
        });
    }
    @Override
    protected String getStyle(UIComponent component) {
        return attributeAsString(component"style");
    }
    @Override
    protected String getStyleClass(UIComponent component) {
        return HtmlUtil.concatClasses("rf-tbp", attributeAsString(component"styleClass"));
    }
    private void writeTopTabHeader(FacesContext context, ResponseWriter writerAbstractTab tabthrows IOException {
        boolean isActive = tab.isActive();
        boolean isDisabled = tab.isDisabled();
        // TODO: Ilya, review. Much HTML because we always encoding all states. Need to optimize somehow.
        encodeTabHeader(contexttabwriter, !isActive && !isDisabled);
        encodeTabHeader(contexttabwriterisActive && !isDisabled);
        encodeTabHeader(contexttabwriterisDisabled);
    }
    private String positionAbbreviation(AbstractTab tab) {
        if (tab.getParentPanel().isHeaderPositionedTop()) {
            return "top";
        } else {
            return "btm";
        }
    }
    private void encodeTabHeader(FacesContext contextAbstractTab tab, ResponseWriter writer,
            AbstractTogglePanelTitledItem.HeaderStates stateBoolean isDisplaythrows IOException {
        String headerStateClass = "rf-tab-hdr-" + state.abbreviation();
        String headerPositionClass = "rf-tab-hdr-" + positionAbbreviation(tab);
        writer.startElement(TD_ELEM, tab);
        writer.writeAttribute(ID_ATTRIBUTE, tab.getClientId(context) + ":header:" + state.toString(), null);
        renderPassThroughAttributes(contexttab);
        writer.writeAttribute(
                CLASS_ATTRIBUTE,
                concatClasses("rf-tab-hdr"headerStateClassheaderPositionClass,
                        attributeAsString(tab"headerClass"), attributeAsString(tabstate.headerClass())), null);
        writer.writeAttribute(STYLE_ATTRIBUTE,
                concatStyles(isDisplay ? "" : "display : none"attributeAsString(tab"headerStyle")), null);
        writer.startElement(SPAN_ELEM, tab);
        writer.writeAttribute(CLASS_ATTRIBUTE, "rf-tab-lbl"null);
        UIComponent headerFacet = tab.getHeaderFacet(state);
        if (headerFacet != null && headerFacet.isRendered()) {
            headerFacet.encodeAll(context);
        } else {
            Object headerText = tab.getAttributes().get("header");
            if (headerText != null && !headerText.equals("")) {
                writer.writeText(headerTextnull);
            }
        }
        writer.endElement(SPAN_ELEM);
        writer.endElement(TD_ELEM);
    }
    private void writeTopTabsControl(ResponseWriter w, UIComponent compString stylesString textthrows IOException {
        w.startElement(DIV_ELEM, comp);
        w.writeAttribute(CLASS_ATTRIBUTE, stylesnull);
        w.writeText(textnull);
        w.endElement(DIV_ELEM);
    }
    private void writeTopTabFirstSpacer(ResponseWriter w, UIComponent compthrows IOException {
        AbstractTabPanel tabPanel = (AbstractTabPanelcomp;
        if (tabPanel.isHeaderAlignedLeft()) {
            writeTopTabSpacer(wcomp"padding-left: 5px;""rf-tab-hdr-spcr");
        } else {
            writeTopTabSpacer(wcomp"padding-left: 5px; width:100%""rf-tab-hdr-spcr");
        }
    }
    private void writeTopTabSpacer(ResponseWriter w, UIComponent compthrows IOException {
        writeTopTabSpacer(wcomp"""rf-tab-hdr-spcr rf-tab-hortab-tabspcr-wdh");
    }
    private void writeTopTabLastSpacer(ResponseWriter w, UIComponent compthrows IOException {
        AbstractTabPanel tabPanel = (AbstractTabPanelcomp;
        if (tabPanel.isHeaderAlignedLeft()) {
            writeTopTabSpacer(wcomp"padding-right: 5px; width: 100%;""rf-tab-hdr-spcr");
        } else {
            writeTopTabSpacer(wcomp"padding-right: 5px;""rf-tab-hdr-spcr");
        }
    }
    private void writeTopTabSpacer(ResponseWriter w, UIComponent compString styleString styleClassthrows IOException {
        w.startElement(TD_ELEM, comp);
        w.writeAttribute(stylenull);
        w.writeAttribute(styleClassnull);
        w.write("<br />");
        w.endElement(TD_ELEM);
    }
    @Override
    protected void doEncodeEnd(final ResponseWriter writer, FacesContext context, UIComponent componentthrows IOException {
        AbstractTabPanel panel = (AbstractTabPanelcomponent;
        if (!panel.isHeaderPositionedTop()) {
            writeTabsLineSeparator(writercomponent);
            writeTabsLine(writercontextcomponent);
        }
        writer.endElement(HtmlConstants.DIV_ELEM);
    }
    @Override
    protected JSObject getScriptObject(FacesContext context, UIComponent component) {
        return new JSObject("RichFaces.ui.TabPanel"component.getClientId(context), getScriptObjectOptions(contextcomponent));
    }
    @Override
    protected Map<StringObjectgetScriptObjectOptions(FacesContext context, UIComponent component) {
        Map<StringObjectoptions = super.getScriptObjectOptions(contextcomponent);
        options.put("isKeepHeight", attributeAsString(component"height").length() > 0);
        return options;
    }
    @Override
    protected Class<? extends UIComponent> getComponentClass() {
        return AbstractTabPanel.class;
    }
New to GrepCode? Check out our FAQ X