1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.util.Debug; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Text ; 40 41 import javax.faces.application.FacesMessage; 42 import javax.faces.component.UIComponent; 43 import javax.faces.component.UIMessage; 44 import javax.faces.context.FacesContext; 45 import java.io.IOException ; 46 import java.util.Iterator ; 47 48 public class MessageRenderer extends DomBasicRenderer { 49 50 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 51 throws IOException { 52 53 validateParameters(facesContext, uiComponent, UIMessage.class); 54 55 DOMContext domContext = 56 DOMContext.attachDOMContext(facesContext, uiComponent); 57 if (domContext.isStreamWriting()) { 58 writeStream(facesContext, uiComponent); 59 return; 60 } 61 62 FacesMessage facesMessage = 63 getSingleMessage(facesContext, (UIMessage) uiComponent); 64 65 66 if (!domContext.isInitialized()) { 67 Element span = domContext.createElement(HTML.SPAN_ELEM); 68 domContext.setRootNode(span); 69 setRootElementId(facesContext, span, uiComponent); 70 } 71 Element root = (Element ) domContext.getRootNode(); 72 73 74 if (facesMessage == null) { 75 if (root != null) { 76 DOMContext.removeChildren(root); 77 domContext.stepOver(); 78 } 79 return; 80 } 81 82 DOMContext.removeChildren(root); 84 85 String [] styleAndStyleClass = 86 getStyleAndStyleClass(uiComponent, facesMessage); 87 String style = styleAndStyleClass[0]; 88 String styleClass = styleAndStyleClass[1]; 89 90 if (styleClass != null) { 91 root.setAttribute("class", styleClass); 92 } 93 if (style != null && style.length() > 0) { 94 root.setAttribute("style", style); 95 } 96 else { 97 root.removeAttribute("style"); 98 } 99 100 boolean tooltip = getToolTipAttribute(uiComponent); 102 103 String [] summaryAndDetail = getSummaryAndDetail(facesMessage); 104 String summary = summaryAndDetail[0]; 105 String detail = summaryAndDetail[1]; 106 107 boolean showSummary = ((UIMessage) uiComponent).isShowSummary(); 109 boolean showDetail = ((UIMessage) uiComponent).isShowDetail(); 110 111 if (tooltip && showSummary && showDetail) { 112 root.setAttribute("title", summary); 113 Text textNode = domContext.getDocument().createTextNode(detail); 114 root.appendChild(textNode); 115 116 } else { 117 if (showSummary) { 118 Text textNode = 119 domContext.getDocument().createTextNode(summary); 120 root.appendChild(textNode); 121 } 122 if (showDetail) { 123 Text textNode = domContext.getDocument().createTextNode(detail); 124 root.appendChild(textNode); 125 } 126 } 127 128 domContext.stepOver(); 129 } 130 131 132 private void writeStream(FacesContext facesContext, UIComponent uiComponent) 133 throws IOException { 134 DOMContext domContext = 135 DOMContext.getDOMContext(facesContext, uiComponent); 136 Element root = domContext.createRootElement(HTML.SPAN_ELEM); 137 Text text = domContext.createTextNode("Message goes here"); 138 Object style = uiComponent.getAttributes().get("style"); 139 String sstyle = ( (style == null) ? null : style.toString() ); 140 if (sstyle != null && sstyle.length() > 0) { 141 root.setAttribute(HTML.STYLE_ATTR, sstyle); 142 } 143 else { 144 root.removeAttribute(HTML.STYLE_ATTR); 145 } 146 root.appendChild(text); 147 domContext.streamWrite(facesContext, uiComponent); 148 domContext.stepOver(); 149 } 150 151 158 private FacesMessage getSingleMessage(FacesContext facesContext, 159 UIMessage uiMessage) { 160 String forComponentId = uiMessage.getFor(); 161 Debug.assertTrue(forComponentId != null, 162 "For component must not be null"); 163 Iterator messages = null; 164 if (forComponentId.length() == 0) { 165 messages = facesContext.getMessages(null); 167 } else { 168 UIComponent forComponent = 169 findForComponent(facesContext, uiMessage); 170 if (forComponent != null) { 171 messages = facesContext 172 .getMessages(forComponent.getClientId(facesContext)); 173 } 174 } 175 if (messages == null || !messages.hasNext()) { 176 return null; 178 } 179 FacesMessage firstMessage = (FacesMessage) messages.next(); 180 return firstMessage; 181 } 182 } 183 184 | Popular Tags |