1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 21 import javax.faces.FacesException; 22 import javax.faces.application.FacesMessage; 23 import javax.faces.component.UIComponent; 24 import javax.faces.component.UIMessage; 25 import javax.faces.component.UIViewRoot; 26 import javax.faces.component.html.HtmlMessage; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import java.io.IOException ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 77 public abstract class HtmlMessageRendererBase 78 extends HtmlRenderer 79 { 80 82 protected abstract String getSummary(FacesContext facesContext, 83 UIComponent message, 84 FacesMessage facesMessage, 85 String msgClientId); 86 87 protected abstract String getDetail(FacesContext facesContext, 88 UIComponent message, 89 FacesMessage facesMessage, 90 String msgClientId); 91 92 protected void renderMessage(FacesContext facesContext, 93 UIComponent message) 94 throws IOException 95 { 96 String forAttr = getFor(message); 97 if (forAttr == null) 98 { 99 throw new FacesException("Attribute 'for' of UIMessage must not be null"); 100 } 101 102 UIComponent forComponent = message.findComponent(forAttr); 103 if (forComponent == null) 104 { 105 throw new FacesException("Could not render Message. Unable to find component '" + forAttr + "' (calling findComponent on component '" + message.getClientId(facesContext) + "')"); 106 } 107 108 String clientId = forComponent.getClientId(facesContext); 109 110 Iterator messageIterator = facesContext.getMessages(clientId); 111 if (!messageIterator.hasNext()) 112 { 113 return; 115 } 116 117 FacesMessage facesMessage = (FacesMessage)messageIterator.next(); 119 120 renderSingleFacesMessage(facesContext, message, facesMessage,clientId); 122 } 123 124 125 126 protected void renderSingleFacesMessage(FacesContext facesContext, 127 UIComponent message, 128 FacesMessage facesMessage, 129 String messageClientId) 130 throws IOException 131 { 132 String [] styleAndClass = getStyleAndStyleClass(message, facesMessage.getSeverity()); 134 String style = styleAndClass[0]; 135 String styleClass = styleAndClass[1]; 136 137 String summary = getSummary(facesContext, message, facesMessage, messageClientId); 138 String detail = getDetail(facesContext, message, facesMessage, messageClientId); 139 140 String title = getTitle(message); 141 boolean tooltip = isTooltip(message); 142 143 if (title == null && tooltip) 144 { 145 title = summary; 146 } 147 148 ResponseWriter writer = facesContext.getResponseWriter(); 149 150 boolean span = false; 151 152 153 if(message.getId()!=null && !message.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) 154 { 155 span = true; 156 157 writer.startElement(HTML.SPAN_ELEM, message); 158 159 HtmlRendererUtils.writeIdIfNecessary(writer, message, facesContext); 160 161 HtmlRendererUtils.renderHTMLAttributes(writer, message, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS); 162 } 163 else 164 { 165 span=HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement( 166 writer, message, HTML.SPAN_ELEM, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS); 167 } 168 169 span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.TITLE_ATTR, title, span); 170 span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_ATTR, style, span); 171 span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_CLASS_ATTR, styleClass, span); 172 173 174 boolean showSummary = isShowSummary(message) && (summary != null); 175 boolean showDetail = isShowDetail(message) && (detail != null); 176 177 if (showSummary && !(title == null && tooltip)) 178 { 179 writer.writeText(summary, null); 180 if (showDetail) 181 { 182 writer.writeText(" ", null); 183 } 184 } 185 186 if (showDetail) 187 { 188 writer.writeText(detail, null); 189 } 190 191 if (span) 192 { 193 writer.endElement(HTML.SPAN_ELEM); 194 } 195 } 196 197 198 protected String [] getStyleAndStyleClass(UIComponent message, 199 FacesMessage.Severity severity) 200 { 201 String style = null; 202 String styleClass = null; 203 if (message instanceof HtmlMessage) 204 { 205 if (severity == FacesMessage.SEVERITY_INFO) 206 { 207 style = ((HtmlMessage)message).getInfoStyle(); 208 styleClass = ((HtmlMessage)message).getInfoClass(); 209 } 210 else if (severity == FacesMessage.SEVERITY_WARN) 211 { 212 style = ((HtmlMessage)message).getWarnStyle(); 213 styleClass = ((HtmlMessage)message).getWarnClass(); 214 } 215 else if (severity == FacesMessage.SEVERITY_ERROR) 216 { 217 style = ((HtmlMessage)message).getErrorStyle(); 218 styleClass = ((HtmlMessage)message).getErrorClass(); 219 } 220 else if (severity == FacesMessage.SEVERITY_FATAL) 221 { 222 style = ((HtmlMessage)message).getFatalStyle(); 223 styleClass = ((HtmlMessage)message).getFatalClass(); 224 } 225 226 if (style == null) 227 { 228 style = ((HtmlMessage)message).getStyle(); 229 } 230 231 if (styleClass == null) 232 { 233 styleClass = ((HtmlMessage)message).getStyleClass(); 234 } 235 } 236 else 237 { 238 Map attr = message.getAttributes(); 239 if (severity == FacesMessage.SEVERITY_INFO) 240 { 241 style = (String )attr.get(JSFAttr.INFO_STYLE_ATTR); 242 styleClass = (String )attr.get(JSFAttr.INFO_CLASS_ATTR); 243 } 244 else if (severity == FacesMessage.SEVERITY_WARN) 245 { 246 style = (String )attr.get(JSFAttr.WARN_STYLE_ATTR); 247 styleClass = (String )attr.get(JSFAttr.WARN_CLASS_ATTR); 248 } 249 else if (severity == FacesMessage.SEVERITY_ERROR) 250 { 251 style = (String )attr.get(JSFAttr.ERROR_STYLE_ATTR); 252 styleClass = (String )attr.get(JSFAttr.ERROR_CLASS_ATTR); 253 } 254 else if (severity == FacesMessage.SEVERITY_FATAL) 255 { 256 style = (String )attr.get(JSFAttr.FATAL_STYLE_ATTR); 257 styleClass = (String )attr.get(JSFAttr.FATAL_CLASS_ATTR); 258 } 259 260 if (style == null) 261 { 262 style = (String )attr.get(JSFAttr.STYLE_CLASS_ATTR); 263 } 264 265 if (styleClass == null) 266 { 267 styleClass = (String )attr.get(JSFAttr.STYLE_CLASS_ATTR); 268 } 269 } 270 271 return new String [] {style, styleClass}; 272 } 273 274 protected String getFor(UIComponent component) 275 { 276 if (component instanceof UIMessage) 277 { 278 return ((UIMessage)component).getFor(); 279 } 280 else 281 { 282 return (String )component.getAttributes().get(JSFAttr.FOR_ATTR); 283 } 284 } 285 286 protected String getTitle(UIComponent component) 287 { 288 if (component instanceof HtmlMessage) 289 { 290 return ((HtmlMessage)component).getTitle(); 291 } 292 else 293 { 294 return (String )component.getAttributes().get(JSFAttr.TITLE_ATTR); 295 } 296 } 297 298 protected boolean isTooltip(UIComponent component) 299 { 300 if (component instanceof HtmlMessage) 301 { 302 return ((HtmlMessage)component).isTooltip(); 303 } 304 else 305 { 306 return RendererUtils.getBooleanAttribute(component, JSFAttr.TOOLTIP_ATTR, false); 307 } 308 } 309 310 protected boolean isShowSummary(UIComponent component) 311 { 312 if (component instanceof UIMessage) 313 { 314 return ((UIMessage)component).isShowSummary(); 315 } 316 else 317 { 318 return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false); 319 } 320 } 321 322 protected boolean isShowDetail(UIComponent component) 323 { 324 if (component instanceof UIMessage) 325 { 326 return ((UIMessage)component).isShowDetail(); 327 } 328 else 329 { 330 return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false); 331 } 332 } 333 334 335 336 337 338 339 340 341 342 343 344 345 } 346 | Popular Tags |