1 16 package org.apache.myfaces.custom.popup; 17 18 import org.apache.myfaces.component.html.util.AddResource; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 import org.apache.myfaces.renderkit.html.HtmlRenderer; 21 import org.apache.myfaces.renderkit.html.HTML; 22 23 import javax.faces.component.UIComponent; 24 import javax.faces.context.FacesContext; 25 import javax.faces.context.ResponseWriter; 26 import java.io.IOException ; 27 import java.util.List ; 28 29 74 public class HtmlPopupRenderer 75 extends HtmlRenderer 76 { 77 public static final String RENDERER_TYPE = "org.apache.myfaces.Popup"; 78 80 public boolean getRendersChildren() 81 { 82 return true; 83 } 84 85 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException 86 { 87 } 88 89 public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException 90 { 91 92 } 93 94 95 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 96 { 97 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPopup.class); 98 99 HtmlPopup popup = (HtmlPopup) uiComponent; 100 101 UIComponent popupFacet = popup.getPopup(); 102 103 String popupId = writePopupScript( 104 facesContext, popup.getClientId(facesContext), 105 popup.getDisplayAtDistanceX(),popup.getDisplayAtDistanceY()); 106 107 109 writeMouseOverAttribs(popupId, uiComponent.getChildren(), 110 popup.getClosePopupOnExitingElement()==null || 111 popup.getClosePopupOnExitingElement().booleanValue()); 112 113 RendererUtils.renderChildren(facesContext, uiComponent); 114 115 ResponseWriter writer = facesContext.getResponseWriter(); 116 117 writer.startElement(HTML.DIV_ELEM, popup); 118 writer.writeAttribute(HTML.STYLE_ATTR,(popup.getStyle()!=null?(popup.getStyle()+ 119 (popup.getStyle().trim().endsWith(";")?"":";")):"")+ 120 "position:absolute;display:none;",null); 121 if(popup.getStyleClass()!=null) 122 { 123 writer.writeAttribute(HTML.CLASS_ATTR,popup.getStyleClass(),null); 124 } 125 writer.writeAttribute(HTML.ID_ATTR, popup.getClientId(facesContext),null); 126 writer.writeAttribute(HTML.ONMOUSEOVER_ATTR, new String (popupId+".redisplay();"),null); 127 128 Boolean closeExitPopup = popup.getClosePopupOnExitingPopup(); 129 130 if(closeExitPopup==null || closeExitPopup.booleanValue()) 131 writer.writeAttribute(HTML.ONMOUSEOUT_ATTR, new String (popupId+".hide();"),null); 132 133 RendererUtils.renderChildren(facesContext, popupFacet); 134 writer.endElement(HTML.DIV_ELEM); 135 } 136 137 private void writeMouseOverAttribs(String popupId, List children, boolean renderMouseOut) 138 { 139 for (int i = 0; i < children.size(); i++) 140 { 141 UIComponent uiComponent = (UIComponent) children.get(i); 142 143 callMethod(uiComponent,"onmouseover",new String (popupId+".display(event);")); 144 145 if(renderMouseOut) 146 callMethod(uiComponent,"onmouseout",new String (popupId+".hide(event);")); 147 148 writeMouseOverAttribs(popupId, uiComponent.getChildren(),renderMouseOut); 149 } 150 } 151 152 private String writePopupScript(FacesContext context, String clientId, 153 Integer displayAtDistanceX, Integer displayAtDistanceY) 154 throws IOException 155 { 156 AddResource.addJavaScriptToHeader( 157 HtmlPopupRenderer.class, "JSPopup.js", false, context); 158 159 String popupId = (clientId+"Popup").replaceAll(":","_"); 160 161 while(popupId.startsWith("_")) 162 { 163 popupId = popupId.substring(1); 164 } 165 166 ResponseWriter writer = context.getResponseWriter(); 167 writer.startElement(HTML.SCRIPT_ELEM,null); 168 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null); 169 writer.writeText("var "+popupId+"=new orgApacheMyfacesPopup('"+clientId+"',"+ 170 (displayAtDistanceX==null?-5:displayAtDistanceX.intValue())+","+ 171 (displayAtDistanceY==null?-5:displayAtDistanceY.intValue())+");",null); 172 writer.endElement(HTML.SCRIPT_ELEM); 173 174 return popupId; 175 } 176 177 private void writeMouseOverAndOutAttribs(String popupId, List children) 178 { 179 for (int i = 0; i < children.size(); i++) 180 { 181 UIComponent uiComponent = (UIComponent) children.get(i); 182 183 callMethod(uiComponent,"onmouseover",new String (popupId+".redisplay();")); 184 callMethod(uiComponent,"onmouseout",new String (popupId+".hide();")); 185 186 writeMouseOverAndOutAttribs(popupId, uiComponent.getChildren()); 187 } 188 } 189 190 private void callMethod(UIComponent uiComponent, String propName, String value) 191 { 192 Object oldValue = uiComponent.getAttributes().get(propName); 193 194 String oldValueStr = ""; 195 196 String genCommentary = "/* generated code */"; 197 198 if(oldValue != null) 199 { 200 oldValueStr = oldValue.toString().trim(); 201 202 int genCommentaryIndex; 203 204 if((genCommentaryIndex=oldValueStr.indexOf(genCommentary))!=-1) 206 { 207 oldValueStr = oldValueStr.substring(0,genCommentaryIndex); 208 } 209 210 if(oldValueStr.length()>0 && !oldValueStr.endsWith(";")) 211 oldValueStr +=";"; 212 } 213 214 value = oldValueStr + genCommentary+value; 215 216 uiComponent.getAttributes().put(propName, value); 217 } 218 } 219 | Popular Tags |