1 16 package org.apache.myfaces.custom.collapsiblepanel; 17 18 import org.apache.myfaces.renderkit.RendererUtils; 19 import org.apache.myfaces.renderkit.html.HTML; 20 import org.apache.myfaces.renderkit.html.HtmlRenderer; 21 import org.apache.myfaces.renderkit.html.HtmlRendererUtils; 22 23 import javax.faces.application.Application; 24 import javax.faces.application.ViewHandler; 25 import javax.faces.component.UIComponent; 26 import javax.faces.component.html.HtmlCommandLink; 27 import javax.faces.component.html.HtmlOutputText; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 31 import java.io.IOException ; 32 import java.util.List ; 33 34 43 public class HtmlCollapsiblePanelRenderer extends HtmlRenderer { 44 private static final String LINK_ID = "ToggleCollapsed".intern(); 46 47 public boolean getRendersChildren() 48 { 49 return true; 50 } 51 52 public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException 53 { 54 ResponseWriter writer = facesContext.getResponseWriter(); 56 HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent; 57 58 HtmlCommandLink link = getLink(facesContext, collapsiblePanel); 59 collapsiblePanel.getChildren().add(link); 60 61 RendererUtils.renderChild(facesContext, link); 63 link.setRendered(false); 64 65 if (!collapsiblePanel.isCollapsed()) { 67 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 68 writer.startElement(HTML.DIV_ELEM, null); 70 RendererUtils.renderChildren(facesContext, uiComponent); 71 writer.endElement(HTML.DIV_ELEM ); 72 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 73 } 74 75 link.setRendered(true); 76 } 77 78 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException 79 { 80 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class); 81 ResponseWriter writer = facesContext.getResponseWriter(); 82 83 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 84 writer.startElement(HTML.DIV_ELEM, null); 85 86 HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent; 87 ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 88 String viewId = facesContext.getViewRoot().getViewId(); 89 String actionURL = viewHandler.getActionURL(facesContext, viewId); 90 91 Application application = facesContext.getApplication(); 92 } 93 94 95 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 96 { 97 ResponseWriter writer = facesContext.getResponseWriter(); 99 writer.endElement(HTML.DIV_ELEM ); 100 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 101 } 102 103 public void decode(FacesContext facesContext, UIComponent uiComponent) 104 { 105 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlCollapsiblePanel.class); 106 HtmlCollapsiblePanel collapsiblePanel = (HtmlCollapsiblePanel)uiComponent; 107 String reqValue = (String )facesContext.getExternalContext().getRequestParameterMap().get(HtmlRendererUtils.getHiddenCommandLinkFieldName(HtmlRendererUtils.getFormName(collapsiblePanel, facesContext))); 108 if ((collapsiblePanel.getClientId(facesContext) + LINK_ID).equals(reqValue)) collapsiblePanel.setCollapsed(!collapsiblePanel.isCollapsed() ); 110 } 111 112 protected HtmlCommandLink getLink(FacesContext facesContext, HtmlCollapsiblePanel collapsiblePanel) 113 throws IOException 114 { 115 Application application = facesContext.getApplication(); 116 HtmlCommandLink link = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE); 117 link.setId(collapsiblePanel.getId() + LINK_ID ); 118 link.setTransient(true); 119 link.setImmediate(true); 120 122 List children = link.getChildren(); 123 HtmlOutputText uiText = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE); 125 uiText.setTransient(true); 126 uiText.setValue(collapsiblePanel.isCollapsed() ? ">" : "ν"); 127 uiText.setEscape(false); 128 uiText.setStyleClass(collapsiblePanel.getStyleClass()); 129 uiText.setStyle(collapsiblePanel.getStyle()); 130 children.add(uiText); 131 132 String label = collapsiblePanel.getValue(); 134 if (label != null) { 135 uiText = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE); 136 uiText.setTransient(true); 137 uiText.setValue(" " + label); 138 uiText.setStyleClass(collapsiblePanel.getStyleClass()); 139 uiText.setStyle(collapsiblePanel.getStyle()); 140 children.add(uiText); 141 } 142 return link; 143 } 144 145 146 157 171 } 172 | Popular Tags |