KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlMessageRendererBase


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html;
17
18 import org.apache.myfaces.renderkit.JSFAttr;
19 import org.apache.myfaces.renderkit.RendererUtils;
20
21 import javax.faces.FacesException;
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIMessage;
25 import javax.faces.component.UIViewRoot;
26 import javax.faces.component.html.HtmlMessage;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import java.io.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
35  * @version $Revision: 1.12 $ $Date: 2005/01/26 13:27:16 $
36  * $Log: HtmlMessageRendererBase.java,v $
37  * Revision 1.12 2005/01/26 13:27:16 mmarinschek
38  * The x:message tags are now extended to use the column-name as a label for all inputs in an x:dataTable, without having to specify additional information.
39  *
40  * Revision 1.11 2005/01/22 19:47:44 mmarinschek
41  * Message rendering updated - if a validation exception needs to be rendered, the id of the component is replaced with a label.
42  *
43  * Revision 1.10 2004/12/23 13:03:09 mmarinschek
44  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
45  *
46  * Revision 1.9 2004/10/13 11:51:01 matze
47  * renamed packages to org.apache
48  *
49  * Revision 1.8 2004/09/02 17:23:25 tinytoony
50  * fix for the span-element for other than the output-text
51  *
52  * Revision 1.7 2004/08/09 08:43:29 manolito
53  * bug #1004867 - h:message has duplicate attributes
54  *
55  * Revision 1.6 2004/07/01 22:00:57 mwessendorf
56  * ASF switch
57  *
58  * Revision 1.5 2004/04/05 09:11:03 manolito
59  * extended exception messages
60  *
61  * Revision 1.4 2004/04/01 12:43:18 manolito
62  * html nesting bug fixed
63  *
64  * Revision 1.3 2004/03/31 14:51:47 manolito
65  * summaryFormat and detailFormat support
66  *
67  * Revision 1.2 2004/03/31 13:25:24 manolito
68  * locale variable renamed
69  *
70  * Revision 1.1 2004/03/30 17:47:36 manolito
71  * Message and Messages refactored
72  *
73  * Revision 1.1 2004/03/30 13:24:58 manolito
74  * refactoring: HtmlComponentTag moved to share and renamed to HtmlComponentTagBase
75  *
76  */

77 public abstract class HtmlMessageRendererBase
78         extends HtmlRenderer
79 {
80     //private static final Log log = LogFactory.getLog(HtmlMessageRendererBase.class);
81

82     protected abstract String JavaDoc getSummary(FacesContext facesContext,
83                                          UIComponent message,
84                                          FacesMessage facesMessage,
85                                          String JavaDoc msgClientId);
86
87     protected abstract String JavaDoc getDetail(FacesContext facesContext,
88                                         UIComponent message,
89                                         FacesMessage facesMessage,
90                                         String JavaDoc msgClientId);
91
92     protected void renderMessage(FacesContext facesContext,
93                                  UIComponent message)
94             throws IOException JavaDoc
95     {
96         String JavaDoc forAttr = getFor(message);
97         if (forAttr == null)
98         {
99             throw new FacesException("Attribute 'for' of UIMessage must not be null");
100         }
101
102         UIComponent forComponent = message.findComponent(forAttr);
103         if (forComponent == null)
104         {
105             throw new FacesException("Could not render Message. Unable to find component '" + forAttr + "' (calling findComponent on component '" + message.getClientId(facesContext) + "')");
106         }
107
108         String JavaDoc clientId = forComponent.getClientId(facesContext);
109
110         Iterator JavaDoc messageIterator = facesContext.getMessages(clientId);
111         if (!messageIterator.hasNext())
112         {
113             // No associated message, nothing to render
114
return;
115         }
116
117         // get first message
118
FacesMessage facesMessage = (FacesMessage)messageIterator.next();
119
120         // and render it
121
renderSingleFacesMessage(facesContext, message, facesMessage,clientId);
122     }
123
124
125
126     protected void renderSingleFacesMessage(FacesContext facesContext,
127                                             UIComponent message,
128                                             FacesMessage facesMessage,
129                                             String JavaDoc messageClientId)
130             throws IOException JavaDoc
131     {
132         // determine style and style class
133
String JavaDoc[] styleAndClass = getStyleAndStyleClass(message, facesMessage.getSeverity());
134         String JavaDoc style = styleAndClass[0];
135         String JavaDoc styleClass = styleAndClass[1];
136
137         String JavaDoc summary = getSummary(facesContext, message, facesMessage, messageClientId);
138         String JavaDoc detail = getDetail(facesContext, message, facesMessage, messageClientId);
139
140         String JavaDoc title = getTitle(message);
141         boolean tooltip = isTooltip(message);
142
143         if (title == null && tooltip)
144         {
145             title = summary;
146         }
147
148         ResponseWriter writer = facesContext.getResponseWriter();
149
150         boolean span = false;
151
152
153         if(message.getId()!=null && !message.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
154         {
155             span = true;
156
157             writer.startElement(HTML.SPAN_ELEM, message);
158
159             HtmlRendererUtils.writeIdIfNecessary(writer, message, facesContext);
160
161             HtmlRendererUtils.renderHTMLAttributes(writer, message, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
162         }
163         else
164         {
165             span=HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(
166                                 writer, message, HTML.SPAN_ELEM, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
167         }
168
169         span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.TITLE_ATTR, title, span);
170         span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_ATTR, style, span);
171         span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_CLASS_ATTR, styleClass, span);
172
173
174         boolean showSummary = isShowSummary(message) && (summary != null);
175         boolean showDetail = isShowDetail(message) && (detail != null);
176
177         if (showSummary && !(title == null && tooltip))
178         {
179             writer.writeText(summary, null);
180             if (showDetail)
181             {
182                 writer.writeText(" ", null);
183             }
184         }
185
186         if (showDetail)
187         {
188             writer.writeText(detail, null);
189         }
190
191         if (span)
192         {
193             writer.endElement(HTML.SPAN_ELEM);
194         }
195     }
196
197
198     protected String JavaDoc[] getStyleAndStyleClass(UIComponent message,
199                                              FacesMessage.Severity severity)
200     {
201         String JavaDoc style = null;
202         String JavaDoc styleClass = null;
203         if (message instanceof HtmlMessage)
204         {
205             if (severity == FacesMessage.SEVERITY_INFO)
206             {
207                 style = ((HtmlMessage)message).getInfoStyle();
208                 styleClass = ((HtmlMessage)message).getInfoClass();
209             }
210             else if (severity == FacesMessage.SEVERITY_WARN)
211             {
212                 style = ((HtmlMessage)message).getWarnStyle();
213                 styleClass = ((HtmlMessage)message).getWarnClass();
214             }
215             else if (severity == FacesMessage.SEVERITY_ERROR)
216             {
217                 style = ((HtmlMessage)message).getErrorStyle();
218                 styleClass = ((HtmlMessage)message).getErrorClass();
219             }
220             else if (severity == FacesMessage.SEVERITY_FATAL)
221             {
222                 style = ((HtmlMessage)message).getFatalStyle();
223                 styleClass = ((HtmlMessage)message).getFatalClass();
224             }
225
226             if (style == null)
227             {
228                 style = ((HtmlMessage)message).getStyle();
229             }
230
231             if (styleClass == null)
232             {
233                 styleClass = ((HtmlMessage)message).getStyleClass();
234             }
235         }
236         else
237         {
238             Map JavaDoc attr = message.getAttributes();
239             if (severity == FacesMessage.SEVERITY_INFO)
240             {
241                 style = (String JavaDoc)attr.get(JSFAttr.INFO_STYLE_ATTR);
242                 styleClass = (String JavaDoc)attr.get(JSFAttr.INFO_CLASS_ATTR);
243             }
244             else if (severity == FacesMessage.SEVERITY_WARN)
245             {
246                 style = (String JavaDoc)attr.get(JSFAttr.WARN_STYLE_ATTR);
247                 styleClass = (String JavaDoc)attr.get(JSFAttr.WARN_CLASS_ATTR);
248             }
249             else if (severity == FacesMessage.SEVERITY_ERROR)
250             {
251                 style = (String JavaDoc)attr.get(JSFAttr.ERROR_STYLE_ATTR);
252                 styleClass = (String JavaDoc)attr.get(JSFAttr.ERROR_CLASS_ATTR);
253             }
254             else if (severity == FacesMessage.SEVERITY_FATAL)
255             {
256                 style = (String JavaDoc)attr.get(JSFAttr.FATAL_STYLE_ATTR);
257                 styleClass = (String JavaDoc)attr.get(JSFAttr.FATAL_CLASS_ATTR);
258             }
259
260             if (style == null)
261             {
262                 style = (String JavaDoc)attr.get(JSFAttr.STYLE_CLASS_ATTR);
263             }
264
265             if (styleClass == null)
266             {
267                 styleClass = (String JavaDoc)attr.get(JSFAttr.STYLE_CLASS_ATTR);
268             }
269         }
270
271         return new String JavaDoc[] {style, styleClass};
272     }
273
274     protected String JavaDoc getFor(UIComponent component)
275     {
276         if (component instanceof UIMessage)
277         {
278             return ((UIMessage)component).getFor();
279         }
280         else
281         {
282             return (String JavaDoc)component.getAttributes().get(JSFAttr.FOR_ATTR);
283         }
284     }
285
286     protected String JavaDoc getTitle(UIComponent component)
287     {
288         if (component instanceof HtmlMessage)
289         {
290             return ((HtmlMessage)component).getTitle();
291         }
292         else
293         {
294             return (String JavaDoc)component.getAttributes().get(JSFAttr.TITLE_ATTR);
295         }
296     }
297
298     protected boolean isTooltip(UIComponent component)
299     {
300         if (component instanceof HtmlMessage)
301         {
302             return ((HtmlMessage)component).isTooltip();
303         }
304         else
305         {
306             return RendererUtils.getBooleanAttribute(component, JSFAttr.TOOLTIP_ATTR, false);
307         }
308     }
309
310     protected boolean isShowSummary(UIComponent component)
311     {
312         if (component instanceof UIMessage)
313         {
314             return ((UIMessage)component).isShowSummary();
315         }
316         else
317         {
318             return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false);
319         }
320     }
321
322     protected boolean isShowDetail(UIComponent component)
323     {
324         if (component instanceof UIMessage)
325         {
326             return ((UIMessage)component).isShowDetail();
327         }
328         else
329         {
330             return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false);
331         }
332     }
333
334
335
336
337
338
339
340
341
342
343
344
345 }
346
Popular Tags