001: package com.icesoft.faces.component.panelcollapsible;
002:
003: import java.io.IOException;
004: import java.util.Iterator;
005:
006: import javax.faces.component.UIComponent;
007: import javax.faces.context.FacesContext;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.w3c.dom.Element;
012:
013: import com.icesoft.faces.component.util.CustomComponentUtils;
014: import com.icesoft.faces.context.DOMContext;
015: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
016: import com.icesoft.faces.renderkit.dom_html_basic.FormRenderer;
017: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
018:
019: public class PanelCollapsibleRenderer extends DomBasicRenderer {
020:
021: private static Log log = LogFactory
022: .getLog(PanelCollapsibleRenderer.class);
023:
024: public boolean getRendersChildren() {
025: return true;
026: }
027:
028: public void encodeBegin(FacesContext facesContext,
029: UIComponent uiComponent) throws IOException {
030: PanelCollapsible panelCollapsible = (PanelCollapsible) uiComponent;
031: DOMContext domContext = DOMContext.attachDOMContext(
032: facesContext, uiComponent);
033: if (!domContext.isInitialized()) {
034: Element rootSpan = domContext.createElement(HTML.DIV_ELEM);
035: domContext.setRootNode(rootSpan);
036: setRootElementId(facesContext, rootSpan, uiComponent);
037: }
038: Element root = (Element) domContext.getRootNode();
039: String style = panelCollapsible.getStyle();
040:
041: if (style != null) {
042: root.setAttribute(HTML.STYLE_ATTR, style);
043: }
044: root.setAttribute(HTML.CLASS_ATTR, panelCollapsible
045: .getStyleClass());
046:
047: //create "header" div and append to the parent, don't render any children yet
048: Element header = (Element) domContext
049: .createElement(HTML.DIV_ELEM);
050: header.setAttribute(HTML.CLASS_ATTR, panelCollapsible
051: .getHeaderClass());
052: root.appendChild(header);
053:
054: //create "contents" div and append to the parent, don't render any children yet
055: Element contents = (Element) domContext
056: .createElement(HTML.DIV_ELEM);
057: contents.setAttribute(HTML.CLASS_ATTR, panelCollapsible
058: .getContentClass());
059: root.appendChild(contents);
060:
061: //add click handler if not disabled and toggleOnClick is set to true
062: if (panelCollapsible.isToggleOnClick()
063: && !panelCollapsible.isDisabled()) {
064: FormRenderer.addHiddenField(facesContext, uiComponent
065: .getClientId(facesContext)
066: + "Expanded");
067: UIComponent form = findForm(uiComponent);
068: header.setAttribute(HTML.ONCLICK_ATTR, "document.forms['"
069: + form.getClientId(facesContext) + "']" + "['"
070: + uiComponent.getClientId(facesContext)
071: + "Expanded" + "'].value='"
072: + panelCollapsible.isExpanded() + "'; "
073: + "iceSubmit(document.forms['"
074: + form.getClientId(facesContext)
075: + "'],this,event); return false;");
076: }
077:
078: }
079:
080: public void encodeChildren(FacesContext facesContext,
081: UIComponent uiComponent) throws IOException {
082: validateParameters(facesContext, uiComponent, null);
083: PanelCollapsible panelCollapsible = (PanelCollapsible) uiComponent;
084: DOMContext domContext = DOMContext.getDOMContext(facesContext,
085: uiComponent);
086:
087: //if headerfacet found, get the header div and render all its children
088: UIComponent headerFacet = uiComponent.getFacet("header");
089: if (headerFacet != null) {
090: Element header = (Element) domContext.getRootNode()
091: .getFirstChild();
092: domContext.setCursorParent(header);
093: CustomComponentUtils.renderChild(facesContext, headerFacet);
094: }
095:
096: //if expanded get the content div and render all its children
097: if (panelCollapsible.isExpanded()) {
098: Element contents = (Element) domContext.getRootNode()
099: .getFirstChild().getNextSibling();
100: domContext.setCursorParent(contents);
101: Iterator children = uiComponent.getChildren().iterator();
102: while (children.hasNext()) {
103: UIComponent nextChild = (UIComponent) children.next();
104: if (nextChild.isRendered()) {
105: encodeParentAndChildren(facesContext, nextChild);
106: }
107: }
108: }
109: domContext.stepOver();
110: }
111: }
|