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 org.apache.myfaces.shared_orchestra.renderkit.JSFAttr;
    22  import org.apache.myfaces.shared_orchestra.renderkit.RendererUtils;
    23  import org.apache.myfaces.shared_orchestra.renderkit.html.HTML;
    24  import org.apache.myfaces.shared_orchestra.renderkit.html.HtmlMessageRendererBase;
    25  import org.apache.myfaces.shared_orchestra.util.NullIterator;
    26  import org.apache.commons.logging.Log;
    27  import org.apache.commons.logging.LogFactory;
    28
    29  import javax.faces.application.FacesMessage;
    30  import javax.faces.component.UIComponent;
    31  import javax.faces.component.UIMessages;
    32  import javax.faces.component.html.HtmlMessages;
    33  import javax.faces.context.FacesContext;
    34  import javax.faces.context.ResponseWriter;
    35  import java.io.IOException;
    36  import java.util.Iterator;
    37  import java.util.Map;
    38
    39  /**
    40   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
    41   * @version $Revision: 779713 $ $Date: 2009-05-28 13:30:18 -0500 (Thu, 28 May 2009) $
    42   */
    43  public abstract class HtmlMessagesRendererBase
    44          extends HtmlMessageRendererBase
    45  {
    46      private static final Log log = LogFactory.getLog(HtmlMessagesRendererBase.class);
    47
    48      protected static final String LAYOUT_LIST  = "list";
    49      protected static final String LAYOUT_TABLE = "table";
    50
    51
    52      protected void renderMessages(FacesContext facesContext,
    53                                    UIComponent messages)
    54              throws IOException
    55      {
    56          MessagesIterator messagesIterator = new MessagesIterator(facesContext,
    57                  isGlobalOnly(messages));
    58
    59          if (messagesIterator.hasNext())
    60          {
    61              String layout = getLayout(messages);
    62              if (layout == null)
    63              {
    64                  if (log.isDebugEnabled())
    65                  {
    66                      log.debug("No messages layout given, using default layout 'list'.");
    67                  }
    68                  renderList(facesContext, messages, messagesIterator);
    69              }
    70              else if (layout.equalsIgnoreCase(LAYOUT_TABLE))
    71              {
    72                  renderTable(facesContext, messages, messagesIterator);
    73              }
    74              else
    75              {
    76                  if (log.isWarnEnabled() && !layout.equalsIgnoreCase(LAYOUT_LIST))
    77                  {
    78                      log.warn("Unsupported messages layout '" + layout + "' - using default layout 'list'.");
    79                  }
    80                  renderList(facesContext, messages, messagesIterator);
    81              }
    82          }
    83      }
    84
    85
    86      protected void renderList(FacesContext facesContext,
    87                              UIComponent messages,
    88                              MessagesIterator messagesIterator)
    89              throws IOException
    90      {
    91          ResponseWriter writer = facesContext.getResponseWriter();
    92
    93          writer.startElement(HTML.UL_ELEM, messages);
    94          HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext);
    95          HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, getComponentStyle(messages));
    96          HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, getComponentStyleClass(messages));
    97
    98          while(messagesIterator.hasNext())
    99          {
    100             writer.startElement(org.apache.myfaces.shared_orchestra.renderkit.html.HTML.LI_ELEM, messages);
    101
    102             FacesMessage facesMessage = (FacesMessage)messagesIterator.next();
    103             // determine style and style class
    104             String[] styleAndClass = getStyleAndStyleClass(messages, facesMessage.getSeverity());
    105             String style = styleAndClass[0];
    106             String styleClass = styleAndClass[1];
    107
    108             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style);
    109             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass);
    110
    111             renderSingleFacesMessage(facesContext,
    112                     messages,
    113                     facesMessage,
    114                     messagesIterator.getClientId(),false,false);
    115             writer.endElement(HTML.LI_ELEM);
    116         }
    117
    118         writer.endElement(HTML.UL_ELEM);
    119     }
    120
    121
    122     protected void renderTable(FacesContext facesContext,
    123                              UIComponent messages,
    124                              MessagesIterator messagesIterator)
    125             throws IOException
    126     {
    127         ResponseWriter writer = facesContext.getResponseWriter();
    128
    129         writer.startElement(HTML.TABLE_ELEM, messages);
    130         HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext);
    131
    132         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, getComponentStyle(messages));
    133         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, getComponentStyleClass(messages));
    134
    135         while(messagesIterator.hasNext())
    136         {
    137             writer.startElement(HTML.TR_ELEM, messages);
    138             writer.startElement(HTML.TD_ELEM, messages);
    139
    140             FacesMessage facesMessage = (FacesMessage)messagesIterator.next();
    141             // determine style and style class
    142             String[] styleAndClass = getStyleAndStyleClass(messages, facesMessage.getSeverity());
    143             String style = styleAndClass[0];
    144             String styleClass = styleAndClass[1];
    145
    146             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style);
    147             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass);
    148
    149             renderSingleFacesMessage(facesContext,
    150                     messages,
    151                     facesMessage,
    152                     messagesIterator.getClientId(),false,false);
    153
    154             writer.endElement(HTML.TD_ELEM);
    155             writer.endElement(HTML.TR_ELEM);
    156         }
    157
    158         writer.endElement(HTML.TABLE_ELEM);
    159     }
    160
    161
    162     public static String[] getStyleAndStyleClass(UIComponent messages,
    163                                              FacesMessage.Severity severity)
    164     {
    165         String style = null;
    166         String styleClass = null;
    167         if (messages instanceof HtmlMessages)
    168         {
    169             if (severity == FacesMessage.SEVERITY_INFO)
    170             {
    171                 style = ((HtmlMessages)messages).getInfoStyle();
    172                 styleClass = ((HtmlMessages)messages).getInfoClass();
    173             }
    174             else if (severity == FacesMessage.SEVERITY_WARN)
    175             {
    176                 style = ((HtmlMessages)messages).getWarnStyle();
    177                 styleClass = ((HtmlMessages)messages).getWarnClass();
    178             }
    179             else if (severity == FacesMessage.SEVERITY_ERROR)
    180             {
    181                 style = ((HtmlMessages)messages).getErrorStyle();
    182                 styleClass = ((HtmlMessages)messages).getErrorClass();
    183             }
    184             else if (severity == FacesMessage.SEVERITY_FATAL)
    185             {
    186                 style = ((HtmlMessages)messages).getFatalStyle();
    187                 styleClass = ((HtmlMessages)messages).getFatalClass();
    188             }
    189
    190             //if (style == null)
    191             //{
    192             //    style = ((HtmlMessages)messages).getStyle();
    193             //}
    194
    195             //if (styleClass == null)
    196             //{
    197             //    styleClass = ((HtmlMessages)messages).getStyleClass();
    198             //}
    199         }
    200         else
    201         {
    202             Map attr = messages.getAttributes();
    203             if (severity == FacesMessage.SEVERITY_INFO)
    204             {
    205                 style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.INFO_STYLE_ATTR);
    206                 styleClass = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.INFO_CLASS_ATTR);
    207             }
    208             else if (severity == FacesMessage.SEVERITY_WARN)
    209             {
    210                 style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.WARN_STYLE_ATTR);
    211                 styleClass = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.WARN_CLASS_ATTR);
    212             }
    213             else if (severity == FacesMessage.SEVERITY_ERROR)
    214             {
    215                 style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.ERROR_STYLE_ATTR);
    216                 styleClass = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.ERROR_CLASS_ATTR);
    217             }
    218             else if (severity == FacesMessage.SEVERITY_FATAL)
    219             {
    220                 style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.FATAL_STYLE_ATTR);
    221                 styleClass = (String)attr.get(JSFAttr.FATAL_CLASS_ATTR);
    222             }
    223
    224             //if (style == null)
    225             //{
    226             //    style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_ATTR);
    227             //}
    228
    229             //if (styleClass == null)
    230             //{
    231             //    styleClass = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_CLASS_ATTR);
    232             //}
    233         }
    234
    235         return new String[] {style, styleClass};
    236     }
    237
    238     protected String getComponentStyleClass(UIComponent messages)
    239     {
    240         String styleClass = null;
    241         if (messages instanceof HtmlMessages)
    242         {
    243             styleClass = ((HtmlMessages)messages).getStyleClass();
    244         }
    245         else
    246         {
    247             Map attr = messages.getAttributes();
    248             styleClass = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_CLASS_ATTR);
    249         }
    250         return styleClass;
    251     }
    252
    253     protected String getComponentStyle(UIComponent messages)
    254     {
    255         String style = null;
    256         if (messages instanceof HtmlMessages)
    257         {
    258             style = ((HtmlMessages)messages).getStyle();
    259         }
    260         else
    261         {
    262             Map attr = messages.getAttributes();
    263             style = (String)attr.get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.STYLE_ATTR);
    264         }
    265         return style;
    266     }
    267
    268     protected String getTitle(UIComponent component)
    269     {
    270         if (component instanceof HtmlMessages)
    271         {
    272             return ((HtmlMessages)component).getTitle();
    273         }
    274         else
    275         {
    276             return (String)component.getAttributes().get(org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.TITLE_ATTR);
    277         }
    278     }
    279
    280     protected boolean isTooltip(UIComponent component)
    281     {
    282         if (component instanceof HtmlMessages)
    283         {
    284             return ((HtmlMessages)component).isTooltip();
    285         }
    286         else
    287         {
    288             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.TOOLTIP_ATTR, false);
    289         }
    290     }
    291
    292     protected boolean isShowSummary(UIComponent component)
    293     {
    294         if (component instanceof UIMessages)
    295         {
    296             return ((UIMessages)component).isShowSummary();
    297         }
    298         else
    299         {
    300             return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false);
    301         }
    302     }
    303
    304     protected boolean isShowDetail(UIComponent component)
    305     {
    306         if (component instanceof UIMessages)
    307         {
    308             return ((UIMessages)component).isShowDetail();
    309         }
    310         else
    311         {
    312             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false);
    313         }
    314     }
    315
    316     protected boolean isGlobalOnly(UIComponent component)
    317     {
    318         if (component instanceof UIMessages)
    319         {
    320             return ((UIMessages)component).isGlobalOnly();
    321         }
    322         else
    323         {
    324             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, JSFAttr.GLOBAL_ONLY_ATTR, false);
    325         }
    326     }
    327
    328     protected String getLayout(UIComponent component)
    329     {
    330         if (component instanceof HtmlMessages)
    331         {
    332             return ((HtmlMessages)component).getLayout();
    333         }
    334         else
    335         {
    336             return (String)component.getAttributes().get(JSFAttr.LAYOUT_ATTR);
    337         }
    338     }
    339
    340
    341
    342     private static class MessagesIterator implements Iterator
    343     {
    344         private FacesContext _facesContext;
    345         private Iterator _globalMessagesIterator;
    346         private Iterator _clientIdsWithMessagesIterator;
    347         private Iterator _componentMessagesIterator = null;
    348         private String _clientId = null;
    349
    350         public MessagesIterator(FacesContext facesContext, boolean globalOnly)
    351         {
    352             _facesContext = facesContext;
    353             if (globalOnly)
    354             {
    355                 _globalMessagesIterator = facesContext.getMessages(null);
    356                 _clientIdsWithMessagesIterator = NullIterator.instance();
    357             }
    358             else
    359             {
    360                 _globalMessagesIterator = org.apache.myfaces.shared_orchestra.util.NullIterator.instance();
    361                 _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages();
    362             }
    363             _componentMessagesIterator = null;
    364             _clientId = null;
    365         }
    366
    367         public boolean hasNext()
    368         {
    369             return _globalMessagesIterator.hasNext() ||
    370                     _clientIdsWithMessagesIterator.hasNext() ||
    371                     (_componentMessagesIterator != null && _componentMessagesIterator.hasNext());
    372         }
    373
    374         public Object next()
    375         {
    376             if (_globalMessagesIterator.hasNext())
    377             {
    378                 return _globalMessagesIterator.next();
    379             }
    380             else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext())
    381             {
    382                 return _componentMessagesIterator.next();
    383             }
    384             else
    385             {
    386                 _clientId = (String)_clientIdsWithMessagesIterator.next();
    387                 _componentMessagesIterator = _facesContext.getMessages(_clientId);
    388                 return _componentMessagesIterator.next();
    389             }
    390         }
    391
    392         public void remove()
    393         {
    394             throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
    395         }
    396
    397         public String getClientId()
    398         {
    399             return _clientId;
    400         }
    401     }
    402
    403 }