View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared_orchestra.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import javax.faces.application.FacesMessage;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIMessage;
28  import javax.faces.component.UIViewRoot;
29  import javax.faces.component.html.HtmlMessage;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.myfaces.shared_orchestra.renderkit.JSFAttr;
36  import org.apache.myfaces.shared_orchestra.renderkit.RendererUtils;
37  import org.apache.myfaces.shared_orchestra.renderkit.html.HTML;
38  
39  /**
40   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
41   * @version $Revision: 778857 $ $Date: 2009-05-26 14:43:52 -0500 (Tue, 26 May 2009) $
42   */
43  public abstract class HtmlMessageRendererBase
44          extends HtmlRenderer
45  {
46      private static final Log log = LogFactory.getLog(HtmlMessageRendererBase.class);
47  
48      protected abstract String getSummary(FacesContext facesContext,
49                                           UIComponent message,
50                                           FacesMessage facesMessage,
51                                           String msgClientId);
52  
53      protected abstract String getDetail(FacesContext facesContext,
54                                          UIComponent message,
55                                          FacesMessage facesMessage,
56                                          String msgClientId);
57  
58  
59      protected void renderMessage(FacesContext facesContext,
60                                   UIComponent message)
61              throws IOException
62      {
63          renderMessage(facesContext, message, false);
64      }
65  
66      /**
67       * @param facesContext
68       * @param message
69       * @param alwaysRenderSpan if true will render a span even if there is no message
70       */
71      protected void renderMessage(FacesContext facesContext, UIComponent message, boolean alwaysRenderSpan) throws IOException
72      {
73          String forAttr = getFor(message);
74          if (forAttr == null)
75          {
76              log.error("Attribute 'for' of UIMessage must not be null");
77              return;
78          }
79  
80          UIComponent forComponent = message.findComponent(forAttr);
81          if (forComponent == null)
82          {
83              log.error("Could not render Message. Unable to find component '" + forAttr + "' (calling findComponent on component '" + message.getClientId(facesContext) + "'). If the provided id was correct, wrap the message and its component into an h:panelGroup or h:panelGrid.");
84              return;
85          }
86  
87          String clientId = forComponent.getClientId(facesContext);
88  
89          Iterator messageIterator = facesContext.getMessages(clientId);
90          if (!messageIterator.hasNext())
91          {
92              // No associated message, nothing to render
93              if (alwaysRenderSpan)
94              {
95                  // show span anyways in case there's a client side update, ie: ajax
96                  ResponseWriter writer = facesContext.getResponseWriter();
97                  writer.startElement(HTML.SPAN_ELEM, message);
98                  writer.writeAttribute(HTML.ID_ATTR, message.getId(), null);
99                  writer.endElement(HTML.SPAN_ELEM);
100             }
101             return;
102         }
103 
104         // get first message
105         FacesMessage facesMessage = (FacesMessage) messageIterator.next();
106 
107         // and render it
108         renderSingleFacesMessage(facesContext, message, facesMessage, clientId);
109     }
110 
111 
112     protected void renderSingleFacesMessage(FacesContext facesContext,
113                                             UIComponent message,
114                                             FacesMessage facesMessage,
115                                             String messageClientId)
116             throws IOException
117     {
118         renderSingleFacesMessage(facesContext, message, facesMessage, messageClientId,true);
119     }
120     
121     protected void renderSingleFacesMessage(FacesContext facesContext,
122             UIComponent message,
123             FacesMessage facesMessage,
124             String messageClientId,
125             boolean renderId)
126     throws IOException
127     {
128         renderSingleFacesMessage(facesContext, message, facesMessage, messageClientId, renderId, true);
129     }
130 
131     protected void renderSingleFacesMessage(FacesContext facesContext,
132                                             UIComponent message,
133                                             FacesMessage facesMessage,
134                                             String messageClientId,
135                                             boolean renderId,
136                                             boolean renderStyleAndStyleClass)
137             throws IOException
138     {
139         // determine style and style class
140         String[] styleAndClass = HtmlMessageRendererBase.getStyleAndStyleClass(message, facesMessage.getSeverity());
141         String style = styleAndClass[0];
142         String styleClass = styleAndClass[1];
143 
144         String summary = getSummary(facesContext, message, facesMessage, messageClientId);
145         String detail = getDetail(facesContext, message, facesMessage, messageClientId);
146 
147         String title = getTitle(message);
148         boolean tooltip = isTooltip(message);
149 
150         if (title == null && tooltip)
151         {
152             title = summary;
153         }
154 
155         ResponseWriter writer = facesContext.getResponseWriter();
156 
157         boolean span = false;
158 
159 
160         if (message.getId() != null && !message.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
161         {
162             span = true;
163 
164             writer.startElement(HTML.SPAN_ELEM, message);
165 
166             if (renderId)
167             {
168                 HtmlRendererUtils.writeIdIfNecessary(writer, message, facesContext);
169             }
170 
171             HtmlRendererUtils.renderHTMLAttributes(writer, message, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
172         }
173         else
174         {
175             span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(
176                     writer, message, HTML.SPAN_ELEM, HTML.MESSAGE_PASSTHROUGH_ATTRIBUTES_WITHOUT_TITLE_STYLE_AND_STYLE_CLASS);
177         }
178 
179         span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.TITLE_ATTR, title, span);
180         if (renderStyleAndStyleClass)
181         {
182             span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_ATTR, style, span);
183             span |= HtmlRendererUtils.renderHTMLAttributeWithOptionalStartElement(writer, message, HTML.SPAN_ELEM, HTML.STYLE_CLASS_ATTR, styleClass, span);
184         }
185 
186 
187         boolean showSummary = isShowSummary(message) && (summary != null);
188         boolean showDetail = isShowDetail(message) && (detail != null);
189 
190         if (showSummary && !(title == null && tooltip))
191         {
192             writer.writeText(summary, null);
193             if (showDetail)
194             {
195                 writer.writeText(" ", null);
196             }
197         }
198 
199         if (showDetail)
200         {
201             writer.writeText(detail, null);
202         }
203 
204         if (span)
205         {
206             writer.endElement(org.apache.myfaces.shared_orchestra.renderkit.html.HTML.SPAN_ELEM);
207         }
208     }
209 
210 
211     public static String[] getStyleAndStyleClass(UIComponent message,
212                                                  FacesMessage.Severity severity)
213     {
214         String style = null;
215         String styleClass = null;
216         if (message instanceof HtmlMessage)
217         {
218             if (severity == FacesMessage.SEVERITY_INFO)
219             {
220                 style = ((HtmlMessage) message).getInfoStyle();
221                 styleClass = ((HtmlMessage) message).getInfoClass();
222             }
223             else if (severity == FacesMessage.SEVERITY_WARN)
224             {
225                 style = ((HtmlMessage) message).getWarnStyle();
226                 styleClass = ((HtmlMessage) message).getWarnClass();
227             }
228             else if (severity == FacesMessage.SEVERITY_ERROR)
229             {
230                 style = ((HtmlMessage) message).getErrorStyle();
231                 styleClass = ((HtmlMessage) message).getErrorClass();
232             }
233             else if (severity == FacesMessage.SEVERITY_FATAL)
234             {
235                 style = ((HtmlMessage) message).getFatalStyle();
236                 styleClass = ((HtmlMessage) message).getFatalClass();
237             }
238 
239             if (style == null)
240             {
241                 style = ((HtmlMessage) message).getStyle();
242             }
243 
244             if (styleClass == null)
245             {
246                 styleClass = ((HtmlMessage) message).getStyleClass();
247             }
248         }
249         else
250         {
251             Map attr = message.getAttributes();
252             if (severity == FacesMessage.SEVERITY_INFO)
253             {
254                 style = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.INFO_STYLE_ATTR);
255                 styleClass = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.INFO_CLASS_ATTR);
256             }
257             else if (severity == FacesMessage.SEVERITY_WARN)
258             {
259                 style = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.WARN_STYLE_ATTR);
260                 styleClass = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.WARN_CLASS_ATTR);
261             }
262             else if (severity == FacesMessage.SEVERITY_ERROR)
263             {
264                 style = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.ERROR_STYLE_ATTR);
265                 styleClass = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.ERROR_CLASS_ATTR);
266             }
267             else if (severity == FacesMessage.SEVERITY_FATAL)
268             {
269                 style = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.FATAL_STYLE_ATTR);
270                 styleClass = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.FATAL_CLASS_ATTR);
271             }
272 
273             if (style == null)
274             {
275                 style = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_ATTR);
276             }
277 
278             if (styleClass == null)
279             {
280                 styleClass = (String) attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_CLASS_ATTR);
281             }
282         }
283 
284         return new String[]{style, styleClass};
285     }
286 
287     protected String getFor(UIComponent component)
288     {
289         if (component instanceof UIMessage)
290         {
291             return ((UIMessage) component).getFor();
292         }
293         else
294         {
295             return (String) component.getAttributes().get(JSFAttr.FOR_ATTR);
296         }
297     }
298 
299     protected String getTitle(UIComponent component)
300     {
301         if (component instanceof HtmlMessage)
302         {
303             return ((HtmlMessage) component).getTitle();
304         }
305         else
306         {
307             return (String) component.getAttributes().get(JSFAttr.TITLE_ATTR);
308         }
309     }
310 
311     protected boolean isTooltip(UIComponent component)
312     {
313         if (component instanceof HtmlMessage)
314         {
315             return ((HtmlMessage) component).isTooltip();
316         }
317         else
318         {
319             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.TOOLTIP_ATTR, false);
320         }
321     }
322 
323     protected boolean isShowSummary(UIComponent component)
324     {
325         if (component instanceof UIMessage)
326         {
327             return ((UIMessage) component).isShowSummary();
328         }
329         else
330         {
331             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.SHOW_SUMMARY_ATTR, false);
332         }
333     }
334 
335     protected boolean isShowDetail(UIComponent component)
336     {
337         if (component instanceof UIMessage)
338         {
339             return ((UIMessage) component).isShowDetail();
340         }
341         else
342         {
343             return RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.SHOW_DETAIL_ATTR, false);
344         }
345     }
346 
347 
348 }