1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 import org.apache.myfaces.util.NullIterator; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.application.FacesMessage; 25 import javax.faces.component.UIComponent; 26 import javax.faces.component.UIMessages; 27 import javax.faces.component.html.HtmlMessages; 28 import javax.faces.context.FacesContext; 29 import javax.faces.context.ResponseWriter; 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 54 public abstract class HtmlMessagesRendererBase 55 extends HtmlMessageRendererBase 56 { 57 private static final Log log = LogFactory.getLog(HtmlMessagesRendererBase.class); 58 59 protected static final String LAYOUT_LIST = "list"; 60 protected static final String LAYOUT_TABLE = "table"; 61 62 63 protected void renderMessages(FacesContext facesContext, 64 UIComponent messages) 65 throws IOException 66 { 67 MessagesIterator messagesIterator = new MessagesIterator(facesContext, 68 isGlobalOnly(messages)); 69 70 if (messagesIterator.hasNext()) 71 { 72 String layout = getLayout(messages); 73 if (layout == null) 74 { 75 if (log.isDebugEnabled()) 76 { 77 log.debug("No messages layout given, using default layout 'list'."); 78 } 79 renderList(facesContext, messages, messagesIterator); 80 } 81 else if (layout.equalsIgnoreCase(LAYOUT_TABLE)) 82 { 83 renderTable(facesContext, messages, messagesIterator); 84 } 85 else 86 { 87 if (log.isWarnEnabled() && !layout.equalsIgnoreCase(LAYOUT_LIST)) 88 { 89 log.warn("Unsupported messages layout '" + layout + "' - using default layout 'list'."); 90 } 91 renderList(facesContext, messages, messagesIterator); 92 } 93 } 94 } 95 96 97 private void renderList(FacesContext facesContext, 98 UIComponent messages, 99 MessagesIterator messagesIterator) 100 throws IOException 101 { 102 ResponseWriter writer = facesContext.getResponseWriter(); 103 104 writer.startElement(HTML.UL_ELEM, messages); 105 HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext); 106 107 while(messagesIterator.hasNext()) 108 { 109 writer.startElement(HTML.LI_ELEM, messages); 110 renderSingleFacesMessage(facesContext, 111 messages, 112 (FacesMessage)messagesIterator.next(), 113 messagesIterator.getClientId()); 114 writer.endElement(HTML.LI_ELEM); 115 } 116 117 writer.endElement(HTML.UL_ELEM); 118 } 119 120 121 private void renderTable(FacesContext facesContext, 122 UIComponent messages, 123 MessagesIterator messagesIterator) 124 throws IOException 125 { 126 ResponseWriter writer = facesContext.getResponseWriter(); 127 128 writer.startElement(HTML.TABLE_ELEM, messages); 129 HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext); 130 131 while(messagesIterator.hasNext()) 132 { 133 writer.startElement(HTML.TR_ELEM, messages); 134 writer.startElement(HTML.TD_ELEM, messages); 135 renderSingleFacesMessage(facesContext, 136 messages, 137 (FacesMessage)messagesIterator.next(), 138 messagesIterator.getClientId()); 139 140 writer.endElement(HTML.TD_ELEM); 141 writer.endElement(HTML.TR_ELEM); 142 } 143 144 writer.endElement(HTML.TABLE_ELEM); 145 } 146 147 148 protected String [] getStyleAndStyleClass(UIComponent messages, 149 FacesMessage.Severity severity) 150 { 151 String style = null; 152 String styleClass = null; 153 if (messages instanceof HtmlMessages) 154 { 155 if (severity == FacesMessage.SEVERITY_INFO) 156 { 157 style = ((HtmlMessages)messages).getInfoStyle(); 158 styleClass = ((HtmlMessages)messages).getInfoClass(); 159 } 160 else if (severity == FacesMessage.SEVERITY_WARN) 161 { 162 style = ((HtmlMessages)messages).getWarnStyle(); 163 styleClass = ((HtmlMessages)messages).getWarnClass(); 164 } 165 else if (severity == FacesMessage.SEVERITY_ERROR) 166 { 167 style = ((HtmlMessages)messages).getErrorStyle(); 168 styleClass = ((HtmlMessages)messages).getErrorClass(); 169 } 170 else if (severity == FacesMessage.SEVERITY_FATAL) 171 { 172 style = ((HtmlMessages)messages).getFatalStyle(); 173 styleClass = ((HtmlMessages)messages).getFatalClass(); 174 } 175 176 if (style == null) 177 { 178 style = ((HtmlMessages)messages).getStyle(); 179 } 180 181 if (styleClass == null) 182 { 183 styleClass = ((HtmlMessages)messages).getStyleClass(); 184 } 185 } 186 else 187 { 188 Map attr = messages.getAttributes(); 189 if (severity == FacesMessage.SEVERITY_INFO) 190 { 191 style = (String )attr.get(JSFAttr.INFO_STYLE_ATTR); 192 styleClass = (String )attr.get(JSFAttr.INFO_CLASS_ATTR); 193 } 194 else if (severity == FacesMessage.SEVERITY_WARN) 195 { 196 style = (String )attr.get(JSFAttr.WARN_STYLE_ATTR); 197 styleClass = (String )attr.get(JSFAttr.WARN_CLASS_ATTR); 198 } 199 else if (severity == FacesMessage.SEVERITY_ERROR) 200 { 201 style = (String )attr.get(JSFAttr.ERROR_STYLE_ATTR); 202 styleClass = (String )attr.get(JSFAttr.ERROR_CLASS_ATTR); 203 } 204 else if (severity == FacesMessage.SEVERITY_FATAL) 205 { 206 style = (String )attr.get(JSFAttr.FATAL_STYLE_ATTR); 207 styleClass = (String )attr.get(JSFAttr.FATAL_CLASS_ATTR); 208 } 209 210 if (style == null) 211 { 212 style = (String )attr.get(JSFAttr.STYLE_CLASS_ATTR); 213 } 214 215 if (styleClass == null) 216 { 217 styleClass = (String )attr.get(JSFAttr.STYLE_CLASS_ATTR); 218 } 219 } 220 221 return new String [] {style, styleClass}; 222 } 223 224 225 protected String getTitle(UIComponent component) 226 { 227 if (component instanceof HtmlMessages) 228 { 229 return ((HtmlMessages)component).getTitle(); 230 } 231 else 232 { 233 return (String )component.getAttributes().get(JSFAttr.TITLE_ATTR); 234 } 235 } 236 237 protected boolean isTooltip(UIComponent component) 238 { 239 if (component instanceof HtmlMessages) 240 { 241 return ((HtmlMessages)component).isTooltip(); 242 } 243 else 244 { 245 return RendererUtils.getBooleanAttribute(component, JSFAttr.TOOLTIP_ATTR, false); 246 } 247 } 248 249 protected boolean isShowSummary(UIComponent component) 250 { 251 if (component instanceof UIMessages) 252 { 253 return ((UIMessages)component).isShowSummary(); 254 } 255 else 256 { 257 return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false); 258 } 259 } 260 261 protected boolean isShowDetail(UIComponent component) 262 { 263 if (component instanceof UIMessages) 264 { 265 return ((UIMessages)component).isShowDetail(); 266 } 267 else 268 { 269 return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false); 270 } 271 } 272 273 protected boolean isGlobalOnly(UIComponent component) 274 { 275 if (component instanceof UIMessages) 276 { 277 return ((UIMessages)component).isGlobalOnly(); 278 } 279 else 280 { 281 return RendererUtils.getBooleanAttribute(component, JSFAttr.GLOBAL_ONLY_ATTR, false); 282 } 283 } 284 285 protected String getLayout(UIComponent component) 286 { 287 if (component instanceof HtmlMessages) 288 { 289 return ((HtmlMessages)component).getLayout(); 290 } 291 else 292 { 293 return (String )component.getAttributes().get(JSFAttr.LAYOUT_ATTR); 294 } 295 } 296 297 298 299 private static class MessagesIterator implements Iterator 300 { 301 private FacesContext _facesContext; 302 private Iterator _globalMessagesIterator; 303 private Iterator _clientIdsWithMessagesIterator; 304 private Iterator _componentMessagesIterator = null; 305 private String _clientId = null; 306 307 public MessagesIterator(FacesContext facesContext, boolean globalOnly) 308 { 309 _facesContext = facesContext; 310 _globalMessagesIterator = facesContext.getMessages(null); 311 if (globalOnly) 312 { 313 _clientIdsWithMessagesIterator = NullIterator.instance(); 314 } 315 else 316 { 317 _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages(); 318 } 319 _componentMessagesIterator = null; 320 _clientId = null; 321 } 322 323 public boolean hasNext() 324 { 325 return _globalMessagesIterator.hasNext() || 326 _clientIdsWithMessagesIterator.hasNext() || 327 (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()); 328 } 329 330 public Object next() 331 { 332 if (_globalMessagesIterator.hasNext()) 333 { 334 return _globalMessagesIterator.next(); 335 } 336 else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()) 337 { 338 return _componentMessagesIterator.next(); 339 } 340 else 341 { 342 _clientId = (String )_clientIdsWithMessagesIterator.next(); 343 _componentMessagesIterator = _facesContext.getMessages(_clientId); 344 return _componentMessagesIterator.next(); 345 } 346 } 347 348 public void remove() 349 { 350 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 351 } 352 353 public String getClientId() 354 { 355 return _clientId; 356 } 357 } 358 359 } 360 | Popular Tags |