1 16 package org.apache.myfaces.wap.renderkit.wml; 17 18 import java.util.Iterator ; 19 20 import javax.faces.application.FacesMessage; 21 import javax.faces.component.UIComponent; 22 import javax.faces.context.FacesContext; 23 import javax.faces.context.ResponseWriter; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.myfaces.util.NullIterator; 28 import org.apache.myfaces.wap.component.Messages; 29 import org.apache.myfaces.wap.renderkit.Attributes; 30 import org.apache.myfaces.wap.renderkit.WmlRenderer; 31 32 40 public class MessagesRenderer extends WmlRenderer { 41 private static Log log = LogFactory.getLog(MessagesRenderer.class); 42 43 44 public MessagesRenderer() { 45 super(); 46 log.debug("created object " + this.getClass().getName()); 47 } 48 49 public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException { 50 log.debug("encodeBegin(" + component.getId() + ")"); 51 if (context == null || component == null) { 52 throw new NullPointerException (); 53 } 54 } 55 56 public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException { 57 log.debug("encodeChildren(" + component.getId() + ")"); 58 if (context == null || component == null) { 59 throw new NullPointerException (); 60 } 61 } 62 63 public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException { 64 log.debug("encodeEnd(" + component.getId() + ")"); 65 if (context == null || component == null) { 66 throw new NullPointerException (); 67 } 68 if (!component.isRendered()) return; 69 70 Messages comp = (Messages)component; 71 ResponseWriter writer = context.getResponseWriter(); 72 73 MessagesIterator messagesIterator = new MessagesIterator(context, comp.isGlobalOnly()); 74 75 if (messagesIterator.hasNext()) { 76 String layout = comp.getLayout(); 77 78 if (layout == null || layout.equalsIgnoreCase(Attributes.LIST)) { 79 while(messagesIterator.hasNext()) { 80 renderMessage(context, comp, (FacesMessage)messagesIterator.next()); 81 82 if (messagesIterator.hasNext()) 83 writer.startElement(Attributes.BR, comp); 84 } 85 } 86 else { writer.startElement(Attributes.TABLE, comp); 88 writer.writeAttribute(Attributes.COLUMNS, "1", null); 89 90 while(messagesIterator.hasNext()) { 91 writer.startElement(Attributes.TR, comp); 92 writer.startElement(Attributes.TD, comp); 93 renderMessage(context, comp, (FacesMessage)messagesIterator.next()); 94 95 if (messagesIterator.hasNext()) 96 writer.startElement(Attributes.BR, comp); 97 98 writer.endElement(Attributes.TD); 99 writer.endElement(Attributes.TR); 100 } 101 writer.endElement(Attributes.TABLE); 102 } 103 } 104 } 105 106 private void renderMessage(FacesContext context, Messages comp, FacesMessage message) throws java.io.IOException { 107 ResponseWriter writer = context.getResponseWriter(); 108 109 if (comp.isShowSummary()) 110 writer.write(message.getSummary()); 111 if (comp.isShowDetail()) 112 writer.write(message.getDetail()); 113 } 114 115 public void decode(FacesContext context, UIComponent component) { 116 if (component == null ) throw new NullPointerException (); 117 } 118 119 private static class MessagesIterator implements Iterator { 120 private FacesContext _facesContext; 121 private Iterator _globalMessagesIterator; 122 private Iterator _clientIdsWithMessagesIterator; 123 private Iterator _componentMessagesIterator = null; 124 private String _clientId = null; 125 126 public MessagesIterator(FacesContext facesContext, boolean globalOnly) { 127 _facesContext = facesContext; 128 _globalMessagesIterator = facesContext.getMessages(null); 129 if (globalOnly) { 130 _clientIdsWithMessagesIterator = NullIterator.instance(); 131 } 132 else { 133 _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages(); 134 } 135 _componentMessagesIterator = null; 136 _clientId = null; 137 } 138 139 public boolean hasNext() { 140 return _globalMessagesIterator.hasNext() || 141 _clientIdsWithMessagesIterator.hasNext() || 142 (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()); 143 } 144 145 public Object next() { 146 if (_globalMessagesIterator.hasNext()) { 147 return _globalMessagesIterator.next(); 148 } 149 else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()) { 150 return _componentMessagesIterator.next(); 151 } 152 else { 153 _clientId = (String )_clientIdsWithMessagesIterator.next(); 154 _componentMessagesIterator = _facesContext.getMessages(_clientId); 155 return _componentMessagesIterator.next(); 156 } 157 } 158 159 public void remove() { 160 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 161 } 162 163 public String getClientId() { 164 return _clientId; 165 } 166 } 167 168 } 169 170 | Popular Tags |