001: package nl.knowlogy.validation.jsf.renderkit.html;
002:
003: import java.io.IOException;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import javax.faces.application.FacesMessage;
008: import javax.faces.component.UIComponent;
009: import javax.faces.component.UIMessages;
010: import javax.faces.component.html.HtmlMessages;
011: import javax.faces.context.FacesContext;
012: import javax.faces.context.ResponseWriter;
013:
014: import nl.knowlogy.validation.jsf.renderkit.HtmlRendererUtils;
015: import nl.knowlogy.validation.jsf.utils.HTML;
016: import nl.knowlogy.validation.jsf.utils.JSFAttr;
017: import nl.knowlogy.validation.jsf.utils.NullIterator;
018:
019: import org.apache.log4j.Logger;
020: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
021:
022: public abstract class ValidationHtmlMessagesRendererBase extends
023: ValidationHtmlMessageRendererBase {
024:
025: private static final Logger log = Logger
026: .getLogger(ValidationHtmlMessagesRendererBase.class);
027:
028: protected static final String LAYOUT_LIST = "list";
029:
030: protected static final String LAYOUT_TABLE = "table";
031:
032: protected void renderMessages(FacesContext facesContext,
033: UIComponent messages) throws IOException {
034: MessagesIterator messagesIterator = new MessagesIterator(
035: facesContext, isGlobalOnly(messages));
036:
037: if (messagesIterator.hasNext()) {
038: String layout = getLayout(messages);
039: if (layout == null) {
040: if (log.isDebugEnabled()) {
041: log
042: .debug("No messages layout given, using default layout 'list'.");
043: }
044: renderList(facesContext, messages, messagesIterator);
045: } else if (layout.equalsIgnoreCase(LAYOUT_TABLE)) {
046: renderTable(facesContext, messages, messagesIterator);
047: } else {
048: log.warn("Unsupported messages layout '" + layout
049: + "' - using default layout 'list'.");
050:
051: renderList(facesContext, messages, messagesIterator);
052: }
053: }
054: }
055:
056: private void renderList(FacesContext facesContext,
057: UIComponent messages, MessagesIterator messagesIterator)
058: throws IOException {
059: ResponseWriter writer = facesContext.getResponseWriter();
060:
061: writer.startElement(HTML.UL_ELEM, messages);
062: HtmlRendererUtils.writeIdIfNecessary(writer, messages,
063: facesContext);
064:
065: while (messagesIterator.hasNext()) {
066: writer.startElement(HTML.LI_ELEM, messages);
067: renderSingleFacesMessage(facesContext, messages,
068: (FacesMessage) messagesIterator.next(),
069: messagesIterator.getClientId());
070: writer.endElement(HTML.LI_ELEM);
071: }
072:
073: writer.endElement(HTML.UL_ELEM);
074: }
075:
076: private void renderTable(FacesContext facesContext,
077: UIComponent messages, MessagesIterator messagesIterator)
078: throws IOException {
079: ResponseWriter writer = facesContext.getResponseWriter();
080:
081: writer.startElement(HTML.TABLE_ELEM, messages);
082: HtmlRendererUtils.writeIdIfNecessary(writer, messages,
083: facesContext);
084:
085: while (messagesIterator.hasNext()) {
086: writer.startElement(HTML.TR_ELEM, messages);
087: writer.startElement(HTML.TD_ELEM, messages);
088: renderSingleFacesMessage(facesContext, messages,
089: (FacesMessage) messagesIterator.next(),
090: messagesIterator.getClientId());
091:
092: writer.endElement(HTML.TD_ELEM);
093: writer.endElement(HTML.TR_ELEM);
094: }
095:
096: writer.endElement(HTML.TABLE_ELEM);
097: }
098:
099: public static String[] getStyleAndStyleClass(UIComponent messages,
100: FacesMessage.Severity severity) {
101: String style = null;
102: String styleClass = null;
103: if (messages instanceof HtmlMessages) {
104: if (severity == FacesMessage.SEVERITY_INFO) {
105: style = ((HtmlMessages) messages).getInfoStyle();
106: styleClass = ((HtmlMessages) messages).getInfoClass();
107: } else if (severity == FacesMessage.SEVERITY_WARN) {
108: style = ((HtmlMessages) messages).getWarnStyle();
109: styleClass = ((HtmlMessages) messages).getWarnClass();
110: } else if (severity == FacesMessage.SEVERITY_ERROR) {
111: style = ((HtmlMessages) messages).getErrorStyle();
112: styleClass = ((HtmlMessages) messages).getErrorClass();
113: } else if (severity == FacesMessage.SEVERITY_FATAL) {
114: style = ((HtmlMessages) messages).getFatalStyle();
115: styleClass = ((HtmlMessages) messages).getFatalClass();
116: }
117:
118: if (style == null) {
119: style = ((HtmlMessages) messages).getStyle();
120: }
121:
122: if (styleClass == null) {
123: styleClass = ((HtmlMessages) messages).getStyleClass();
124: }
125: } else {
126: Map attr = messages.getAttributes();
127: if (severity == FacesMessage.SEVERITY_INFO) {
128: style = (String) attr.get(JSFAttr.INFO_STYLE_ATTR);
129: styleClass = (String) attr.get(JSFAttr.INFO_CLASS_ATTR);
130: } else if (severity == FacesMessage.SEVERITY_WARN) {
131: style = (String) attr.get(JSFAttr.WARN_STYLE_ATTR);
132: styleClass = (String) attr.get(JSFAttr.WARN_CLASS_ATTR);
133: } else if (severity == FacesMessage.SEVERITY_ERROR) {
134: style = (String) attr.get(JSFAttr.ERROR_STYLE_ATTR);
135: styleClass = (String) attr
136: .get(JSFAttr.ERROR_CLASS_ATTR);
137: } else if (severity == FacesMessage.SEVERITY_FATAL) {
138: style = (String) attr.get(JSFAttr.FATAL_STYLE_ATTR);
139: styleClass = (String) attr
140: .get(JSFAttr.FATAL_CLASS_ATTR);
141: }
142:
143: if (style == null) {
144: style = (String) attr.get(JSFAttr.STYLE_CLASS_ATTR);
145: }
146:
147: if (styleClass == null) {
148: styleClass = (String) attr
149: .get(JSFAttr.STYLE_CLASS_ATTR);
150: }
151: }
152:
153: return new String[] { style, styleClass };
154: }
155:
156: protected String getTitle(UIComponent component) {
157: if (component instanceof HtmlMessages) {
158: return ((HtmlMessages) component).getTitle();
159: } else {
160: return (String) component.getAttributes().get(
161: JSFAttr.TITLE_ATTR);
162: }
163: }
164:
165: protected boolean isTooltip(UIComponent component) {
166: if (component instanceof HtmlMessages) {
167: return ((HtmlMessages) component).isTooltip();
168: } else {
169: return RendererUtils.getBooleanAttribute(component,
170: JSFAttr.TOOLTIP_ATTR, false);
171: }
172: }
173:
174: protected boolean isShowSummary(UIComponent component) {
175: if (component instanceof UIMessages) {
176: return ((UIMessages) component).isShowSummary();
177: } else {
178: return RendererUtils.getBooleanAttribute(component,
179: JSFAttr.SHOW_SUMMARY_ATTR, false);
180: }
181: }
182:
183: protected boolean isShowDetail(UIComponent component) {
184: if (component instanceof UIMessages) {
185: return ((UIMessages) component).isShowDetail();
186: } else {
187: return RendererUtils.getBooleanAttribute(component,
188: JSFAttr.SHOW_DETAIL_ATTR, false);
189: }
190: }
191:
192: protected boolean isGlobalOnly(UIComponent component) {
193: if (component instanceof UIMessages) {
194: return ((UIMessages) component).isGlobalOnly();
195: } else {
196: return RendererUtils.getBooleanAttribute(component,
197: JSFAttr.GLOBAL_ONLY_ATTR, false);
198: }
199: }
200:
201: protected String getLayout(UIComponent component) {
202: if (component instanceof HtmlMessages) {
203: return ((HtmlMessages) component).getLayout();
204: } else {
205: return (String) component.getAttributes().get(
206: JSFAttr.LAYOUT_ATTR);
207: }
208: }
209:
210: private static class MessagesIterator implements Iterator {
211: private FacesContext facesContext;
212:
213: private Iterator globalMessagesIterator;
214:
215: private Iterator clientIdsWithMessagesIterator;
216:
217: private Iterator componentMessagesIterator = null;
218:
219: private String clientId = null;
220:
221: public MessagesIterator(FacesContext facesContext,
222: boolean globalOnly) {
223: this .facesContext = facesContext;
224: if (globalOnly) {
225: globalMessagesIterator = facesContext.getMessages(null);
226: clientIdsWithMessagesIterator = NullIterator.instance();
227: } else {
228: globalMessagesIterator = NullIterator.instance();
229: clientIdsWithMessagesIterator = facesContext
230: .getClientIdsWithMessages();
231: }
232: componentMessagesIterator = null;
233: clientId = null;
234: }
235:
236: public boolean hasNext() {
237: return globalMessagesIterator.hasNext()
238: || clientIdsWithMessagesIterator.hasNext()
239: || (componentMessagesIterator != null && componentMessagesIterator
240: .hasNext());
241: }
242:
243: public Object next() {
244: if (globalMessagesIterator.hasNext()) {
245: return globalMessagesIterator.next();
246: } else if (componentMessagesIterator != null
247: && componentMessagesIterator.hasNext()) {
248: return componentMessagesIterator.next();
249: } else {
250: clientId = (String) clientIdsWithMessagesIterator
251: .next();
252: componentMessagesIterator = facesContext
253: .getMessages(clientId);
254: return componentMessagesIterator.next();
255: }
256: }
257:
258: public void remove() {
259: throw new UnsupportedOperationException(this .getClass()
260: .getName()
261: + " UnsupportedOperationException");
262: }
263:
264: public String getClientId() {
265: return clientId;
266: }
267: }
268:
269: }
|