KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.myfaces.util.NullIterator;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.faces.application.FacesMessage;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UIMessages;
27 import javax.faces.component.html.HtmlMessages;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
36  * @version $Revision: 1.5 $ $Date: 2005/01/26 13:27:16 $
37  * $Log: HtmlMessagesRendererBase.java,v $
38  * Revision 1.5 2005/01/26 13:27:16 mmarinschek
39  * 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.
40  *
41  * Revision 1.4 2004/12/23 13:03:09 mmarinschek
42  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
43  *
44  * Revision 1.3 2004/10/13 11:51:01 matze
45  * renamed packages to org.apache
46  *
47  * Revision 1.2 2004/07/01 22:00:56 mwessendorf
48  * ASF switch
49  *
50  * Revision 1.1 2004/03/30 17:47:36 manolito
51  * Message and Messages refactored
52  *
53  */

54 public abstract class HtmlMessagesRendererBase
55         extends HtmlMessageRendererBase
56 {
57     private static final Log log = LogFactory.getLog(HtmlMessagesRendererBase.class);
58
59     protected static final String JavaDoc LAYOUT_LIST = "list";
60     protected static final String JavaDoc LAYOUT_TABLE = "table";
61
62
63     protected void renderMessages(FacesContext facesContext,
64                                   UIComponent messages)
65             throws IOException JavaDoc
66     {
67         MessagesIterator messagesIterator = new MessagesIterator(facesContext,
68                                                                  isGlobalOnly(messages));
69
70         if (messagesIterator.hasNext())
71         {
72             String JavaDoc layout = getLayout(messages);
73             if (layout == null)
74             {
75                 if (log.isDebugEnabled())
76                 {
77                     log.debug("No messages layout given, using default layout 'list'.");
78                 }
79                 renderList(facesContext, messages, messagesIterator);
80             }
81             else if (layout.equalsIgnoreCase(LAYOUT_TABLE))
82             {
83                 renderTable(facesContext, messages, messagesIterator);
84             }
85             else
86             {
87                 if (log.isWarnEnabled() && !layout.equalsIgnoreCase(LAYOUT_LIST))
88                 {
89                     log.warn("Unsupported messages layout '" + layout + "' - using default layout 'list'.");
90                 }
91                 renderList(facesContext, messages, messagesIterator);
92             }
93         }
94     }
95
96
97     private void renderList(FacesContext facesContext,
98                             UIComponent messages,
99                             MessagesIterator messagesIterator)
100             throws IOException JavaDoc
101     {
102         ResponseWriter writer = facesContext.getResponseWriter();
103
104         writer.startElement(HTML.UL_ELEM, messages);
105         HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext);
106
107         while(messagesIterator.hasNext())
108         {
109             writer.startElement(HTML.LI_ELEM, messages);
110             renderSingleFacesMessage(facesContext,
111                                      messages,
112                                      (FacesMessage)messagesIterator.next(),
113                                      messagesIterator.getClientId());
114             writer.endElement(HTML.LI_ELEM);
115         }
116
117         writer.endElement(HTML.UL_ELEM);
118     }
119
120
121     private void renderTable(FacesContext facesContext,
122                              UIComponent messages,
123                              MessagesIterator messagesIterator)
124             throws IOException JavaDoc
125     {
126         ResponseWriter writer = facesContext.getResponseWriter();
127
128         writer.startElement(HTML.TABLE_ELEM, messages);
129         HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext);
130
131         while(messagesIterator.hasNext())
132         {
133             writer.startElement(HTML.TR_ELEM, messages);
134             writer.startElement(HTML.TD_ELEM, messages);
135             renderSingleFacesMessage(facesContext,
136                                      messages,
137                                      (FacesMessage)messagesIterator.next(),
138                                         messagesIterator.getClientId());
139
140             writer.endElement(HTML.TD_ELEM);
141             writer.endElement(HTML.TR_ELEM);
142         }
143
144         writer.endElement(HTML.TABLE_ELEM);
145     }
146
147
148     protected String JavaDoc[] getStyleAndStyleClass(UIComponent messages,
149                                              FacesMessage.Severity severity)
150     {
151         String JavaDoc style = null;
152         String JavaDoc styleClass = null;
153         if (messages instanceof HtmlMessages)
154         {
155             if (severity == FacesMessage.SEVERITY_INFO)
156             {
157                 style = ((HtmlMessages)messages).getInfoStyle();
158                 styleClass = ((HtmlMessages)messages).getInfoClass();
159             }
160             else if (severity == FacesMessage.SEVERITY_WARN)
161             {
162                 style = ((HtmlMessages)messages).getWarnStyle();
163                 styleClass = ((HtmlMessages)messages).getWarnClass();
164             }
165             else if (severity == FacesMessage.SEVERITY_ERROR)
166             {
167                 style = ((HtmlMessages)messages).getErrorStyle();
168                 styleClass = ((HtmlMessages)messages).getErrorClass();
169             }
170             else if (severity == FacesMessage.SEVERITY_FATAL)
171             {
172                 style = ((HtmlMessages)messages).getFatalStyle();
173                 styleClass = ((HtmlMessages)messages).getFatalClass();
174             }
175
176             if (style == null)
177             {
178                 style = ((HtmlMessages)messages).getStyle();
179             }
180
181             if (styleClass == null)
182             {
183                 styleClass = ((HtmlMessages)messages).getStyleClass();
184             }
185         }
186         else
187         {
188             Map JavaDoc attr = messages.getAttributes();
189             if (severity == FacesMessage.SEVERITY_INFO)
190             {
191                 style = (String JavaDoc)attr.get(JSFAttr.INFO_STYLE_ATTR);
192                 styleClass = (String JavaDoc)attr.get(JSFAttr.INFO_CLASS_ATTR);
193             }
194             else if (severity == FacesMessage.SEVERITY_WARN)
195             {
196                 style = (String JavaDoc)attr.get(JSFAttr.WARN_STYLE_ATTR);
197                 styleClass = (String JavaDoc)attr.get(JSFAttr.WARN_CLASS_ATTR);
198             }
199             else if (severity == FacesMessage.SEVERITY_ERROR)
200             {
201                 style = (String JavaDoc)attr.get(JSFAttr.ERROR_STYLE_ATTR);
202                 styleClass = (String JavaDoc)attr.get(JSFAttr.ERROR_CLASS_ATTR);
203             }
204             else if (severity == FacesMessage.SEVERITY_FATAL)
205             {
206                 style = (String JavaDoc)attr.get(JSFAttr.FATAL_STYLE_ATTR);
207                 styleClass = (String JavaDoc)attr.get(JSFAttr.FATAL_CLASS_ATTR);
208             }
209
210             if (style == null)
211             {
212                 style = (String JavaDoc)attr.get(JSFAttr.STYLE_CLASS_ATTR);
213             }
214
215             if (styleClass == null)
216             {
217                 styleClass = (String JavaDoc)attr.get(JSFAttr.STYLE_CLASS_ATTR);
218             }
219         }
220
221         return new String JavaDoc[] {style, styleClass};
222     }
223
224
225     protected String JavaDoc getTitle(UIComponent component)
226     {
227         if (component instanceof HtmlMessages)
228         {
229             return ((HtmlMessages)component).getTitle();
230         }
231         else
232         {
233             return (String JavaDoc)component.getAttributes().get(JSFAttr.TITLE_ATTR);
234         }
235     }
236
237     protected boolean isTooltip(UIComponent component)
238     {
239         if (component instanceof HtmlMessages)
240         {
241             return ((HtmlMessages)component).isTooltip();
242         }
243         else
244         {
245             return RendererUtils.getBooleanAttribute(component, JSFAttr.TOOLTIP_ATTR, false);
246         }
247     }
248
249     protected boolean isShowSummary(UIComponent component)
250     {
251         if (component instanceof UIMessages)
252         {
253             return ((UIMessages)component).isShowSummary();
254         }
255         else
256         {
257             return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false);
258         }
259     }
260
261     protected boolean isShowDetail(UIComponent component)
262     {
263         if (component instanceof UIMessages)
264         {
265             return ((UIMessages)component).isShowDetail();
266         }
267         else
268         {
269             return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false);
270         }
271     }
272
273     protected boolean isGlobalOnly(UIComponent component)
274     {
275         if (component instanceof UIMessages)
276         {
277             return ((UIMessages)component).isGlobalOnly();
278         }
279         else
280         {
281             return RendererUtils.getBooleanAttribute(component, JSFAttr.GLOBAL_ONLY_ATTR, false);
282         }
283     }
284
285     protected String JavaDoc getLayout(UIComponent component)
286     {
287         if (component instanceof HtmlMessages)
288         {
289             return ((HtmlMessages)component).getLayout();
290         }
291         else
292         {
293             return (String JavaDoc)component.getAttributes().get(JSFAttr.LAYOUT_ATTR);
294         }
295     }
296
297
298
299     private static class MessagesIterator implements Iterator
300     {
301         private FacesContext _facesContext;
302         private Iterator _globalMessagesIterator;
303         private Iterator _clientIdsWithMessagesIterator;
304         private Iterator _componentMessagesIterator = null;
305         private String JavaDoc _clientId = null;
306
307         public MessagesIterator(FacesContext facesContext, boolean globalOnly)
308         {
309             _facesContext = facesContext;
310             _globalMessagesIterator = facesContext.getMessages(null);
311             if (globalOnly)
312             {
313                 _clientIdsWithMessagesIterator = NullIterator.instance();
314             }
315             else
316             {
317                 _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages();
318             }
319             _componentMessagesIterator = null;
320             _clientId = null;
321         }
322
323         public boolean hasNext()
324         {
325             return _globalMessagesIterator.hasNext() ||
326                    _clientIdsWithMessagesIterator.hasNext() ||
327                    (_componentMessagesIterator != null && _componentMessagesIterator.hasNext());
328         }
329
330         public Object JavaDoc next()
331         {
332             if (_globalMessagesIterator.hasNext())
333             {
334                 return _globalMessagesIterator.next();
335             }
336             else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext())
337             {
338                 return _componentMessagesIterator.next();
339             }
340             else
341             {
342                 _clientId = (String JavaDoc)_clientIdsWithMessagesIterator.next();
343                 _componentMessagesIterator = _facesContext.getMessages(_clientId);
344                 return _componentMessagesIterator.next();
345             }
346         }
347
348         public void remove()
349         {
350             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
351         }
352
353         public String JavaDoc getClientId()
354         {
355             return _clientId;
356         }
357     }
358
359 }
360
Popular Tags