001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.shared_impl.renderkit.html;
017:
018: import java.io.IOException;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import javax.faces.application.FacesMessage;
023: import javax.faces.component.UIComponent;
024: import javax.faces.component.UIMessage;
025: import javax.faces.component.UIViewRoot;
026: import javax.faces.component.html.HtmlMessage;
027: import javax.faces.context.FacesContext;
028: import javax.faces.context.ResponseWriter;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
033: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
034: import org.apache.myfaces.shared_impl.renderkit.html.HTML;
035:
036: /**
037: * @author Manfred Geiler (latest modification by $Author: dennisbyrne $)
038: * @version $Revision: 511714 $ $Date: 2007-02-26 05:02:54 +0100 (Mo, 26 Feb 2007) $
039: */
040: public abstract class HtmlMessageRendererBase extends HtmlRenderer {
041: private static final Log log = LogFactory
042: .getLog(HtmlMessageRendererBase.class);
043:
044: protected abstract String getSummary(FacesContext facesContext,
045: UIComponent message, FacesMessage facesMessage,
046: String msgClientId);
047:
048: protected abstract String getDetail(FacesContext facesContext,
049: UIComponent message, FacesMessage facesMessage,
050: String msgClientId);
051:
052: protected void renderMessage(FacesContext facesContext,
053: UIComponent message) throws IOException {
054: renderMessage(facesContext, message, false);
055: }
056:
057: /**
058: * @param facesContext
059: * @param message
060: * @param alwaysRenderSpan if true will render a span even if there is no message
061: */
062: protected void renderMessage(FacesContext facesContext,
063: UIComponent message, boolean alwaysRenderSpan)
064: throws IOException {
065: String forAttr = getFor(message);
066: if (forAttr == null) {
067: log.error("Attribute 'for' of UIMessage must not be null");
068: return;
069: }
070:
071: UIComponent forComponent = message.findComponent(forAttr);
072: if (forComponent == null) {
073: log
074: .error("Could not render Message. Unable to find component '"
075: + forAttr
076: + "' (calling findComponent on component '"
077: + message.getClientId(facesContext)
078: + "'). If the provided id was correct, wrap the message and its component into an h:panelGroup or h:panelGrid.");
079: return;
080: }
081:
082: String clientId = forComponent.getClientId(facesContext);
083:
084: Iterator messageIterator = facesContext.getMessages(clientId);
085: if (!messageIterator.hasNext()) {
086: // No associated message, nothing to render
087: if (alwaysRenderSpan) {
088: // show span anyways in case there's a client side update, ie: ajax
089: ResponseWriter writer = facesContext
090: .getResponseWriter();
091: writer.startElement(HTML.SPAN_ELEM, message);
092: writer.writeAttribute(HTML.ID_ATTR, message.getId(),
093: null);
094: writer.endElement(HTML.SPAN_ELEM);
095: }
096: return;
097: }
098:
099: // get first message
100: FacesMessage facesMessage = (FacesMessage) messageIterator
101: .next();
102:
103: // and render it
104: renderSingleFacesMessage(facesContext, message, facesMessage,
105: clientId);
106: }
107:
108: protected void renderSingleFacesMessage(FacesContext facesContext,
109: UIComponent message, FacesMessage facesMessage,
110: String messageClientId) throws IOException {
111: // determine style and style class
112: String[] styleAndClass = HtmlMessageRendererBase
113: .getStyleAndStyleClass(message, facesMessage
114: .getSeverity());
115: String style = styleAndClass[0];
116: String styleClass = styleAndClass[1];
117:
118: String summary = getSummary(facesContext, message,
119: facesMessage, messageClientId);
120: String detail = getDetail(facesContext, message, facesMessage,
121: messageClientId);
122:
123: String title = getTitle(message);
124: boolean tooltip = isTooltip(message);
125:
126: if (title == null && tooltip) {
127: title = summary;
128: }
129:
130: ResponseWriter writer = facesContext.getResponseWriter();
131:
132: boolean span = false;
133:
134: if (message.getId() != null
135: && !message.getId().startsWith(
136: UIViewRoot.UNIQUE_ID_PREFIX)) {
137: span = true;
138:
139: writer.startElement(HTML.SPAN_ELEM, message);
140:
141: HtmlRendererUtils.writeIdIfNecessary(writer, message,
142: facesContext);
143:
144: HtmlRendererUtils
145: .renderHTMLAttributes(
146: writer,
147: message,
148: HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
149: } else {
150: span = HtmlRendererUtils
151: .renderHTMLAttributesWithOptionalStartElement(
152: writer,
153: message,
154: HTML.SPAN_ELEM,
155: HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
156: }
157:
158: span |= HtmlRendererUtils
159: .renderHTMLAttributeWithOptionalStartElement(writer,
160: message, HTML.SPAN_ELEM, HTML.TITLE_ATTR,
161: title, span);
162: span |= HtmlRendererUtils
163: .renderHTMLAttributeWithOptionalStartElement(writer,
164: message, HTML.SPAN_ELEM, HTML.STYLE_ATTR,
165: style, span);
166: span |= HtmlRendererUtils
167: .renderHTMLAttributeWithOptionalStartElement(writer,
168: message, HTML.SPAN_ELEM, HTML.STYLE_CLASS_ATTR,
169: styleClass, span);
170:
171: boolean showSummary = isShowSummary(message)
172: && (summary != null);
173: boolean showDetail = isShowDetail(message) && (detail != null);
174:
175: if (showSummary && !(title == null && tooltip)) {
176: writer.writeText(summary, null);
177: if (showDetail) {
178: writer.writeText(" ", null);
179: }
180: }
181:
182: if (showDetail) {
183: writer.writeText(detail, null);
184: }
185:
186: if (span) {
187: writer
188: .endElement(org.apache.myfaces.shared_impl.renderkit.html.HTML.SPAN_ELEM);
189: }
190: }
191:
192: public static String[] getStyleAndStyleClass(UIComponent message,
193: FacesMessage.Severity severity) {
194: String style = null;
195: String styleClass = null;
196: if (message instanceof HtmlMessage) {
197: if (severity == FacesMessage.SEVERITY_INFO) {
198: style = ((HtmlMessage) message).getInfoStyle();
199: styleClass = ((HtmlMessage) message).getInfoClass();
200: } else if (severity == FacesMessage.SEVERITY_WARN) {
201: style = ((HtmlMessage) message).getWarnStyle();
202: styleClass = ((HtmlMessage) message).getWarnClass();
203: } else if (severity == FacesMessage.SEVERITY_ERROR) {
204: style = ((HtmlMessage) message).getErrorStyle();
205: styleClass = ((HtmlMessage) message).getErrorClass();
206: } else if (severity == FacesMessage.SEVERITY_FATAL) {
207: style = ((HtmlMessage) message).getFatalStyle();
208: styleClass = ((HtmlMessage) message).getFatalClass();
209: }
210:
211: if (style == null) {
212: style = ((HtmlMessage) message).getStyle();
213: }
214:
215: if (styleClass == null) {
216: styleClass = ((HtmlMessage) message).getStyleClass();
217: }
218: } else {
219: Map attr = message.getAttributes();
220: if (severity == FacesMessage.SEVERITY_INFO) {
221: style = (String) attr
222: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.INFO_STYLE_ATTR);
223: styleClass = (String) attr
224: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.INFO_CLASS_ATTR);
225: } else if (severity == FacesMessage.SEVERITY_WARN) {
226: style = (String) attr
227: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.WARN_STYLE_ATTR);
228: styleClass = (String) attr
229: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.WARN_CLASS_ATTR);
230: } else if (severity == FacesMessage.SEVERITY_ERROR) {
231: style = (String) attr
232: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.ERROR_STYLE_ATTR);
233: styleClass = (String) attr
234: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.ERROR_CLASS_ATTR);
235: } else if (severity == FacesMessage.SEVERITY_FATAL) {
236: style = (String) attr
237: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.FATAL_STYLE_ATTR);
238: styleClass = (String) attr
239: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.FATAL_CLASS_ATTR);
240: }
241:
242: if (style == null) {
243: style = (String) attr
244: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.STYLE_CLASS_ATTR);
245: }
246:
247: if (styleClass == null) {
248: styleClass = (String) attr
249: .get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.STYLE_CLASS_ATTR);
250: }
251: }
252:
253: return new String[] { style, styleClass };
254: }
255:
256: protected String getFor(UIComponent component) {
257: if (component instanceof UIMessage) {
258: return ((UIMessage) component).getFor();
259: }
260:
261: return (String) component.getAttributes().get(JSFAttr.FOR_ATTR);
262:
263: }
264:
265: protected String getTitle(UIComponent component) {
266: if (component instanceof HtmlMessage) {
267: return ((HtmlMessage) component).getTitle();
268: }
269:
270: return (String) component.getAttributes().get(
271: JSFAttr.TITLE_ATTR);
272:
273: }
274:
275: protected boolean isTooltip(UIComponent component) {
276: if (component instanceof HtmlMessage) {
277: return ((HtmlMessage) component).isTooltip();
278: }
279:
280: return org.apache.myfaces.shared_impl.renderkit.RendererUtils
281: .getBooleanAttribute(
282: component,
283: org.apache.myfaces.shared_impl.renderkit.JSFAttr.TOOLTIP_ATTR,
284: false);
285:
286: }
287:
288: protected boolean isShowSummary(UIComponent component) {
289: if (component instanceof UIMessage) {
290: return ((UIMessage) component).isShowSummary();
291: }
292:
293: return org.apache.myfaces.shared_impl.renderkit.RendererUtils
294: .getBooleanAttribute(
295: component,
296: org.apache.myfaces.shared_impl.renderkit.JSFAttr.SHOW_SUMMARY_ATTR,
297: false);
298:
299: }
300:
301: protected boolean isShowDetail(UIComponent component) {
302: if (component instanceof UIMessage) {
303: return ((UIMessage) component).isShowDetail();
304: }
305:
306: return RendererUtils
307: .getBooleanAttribute(
308: component,
309: org.apache.myfaces.shared_impl.renderkit.JSFAttr.SHOW_DETAIL_ATTR,
310: false);
311:
312: }
313:
314: }
|