001    package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    /*
021     * Created 07.02.2003 16:00:00.
022     * $Id: MessagesRenderer.java 644072 2008-04-02 21:14:53Z bommel $
023     */
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_GLOBAL_ONLY;
028    import org.apache.myfaces.tobago.util.ComponentUtil;
029    import org.apache.myfaces.tobago.component.UIMessages;
030    import org.apache.myfaces.tobago.renderkit.MessageRendererBase;
031    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
032    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
033    import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtil;
034    import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
035    import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
036    
037    import javax.faces.application.FacesMessage;
038    import javax.faces.component.UIComponent;
039    import javax.faces.context.FacesContext;
040    import java.io.IOException;
041    import java.util.ArrayList;
042    import java.util.Iterator;
043    
044    public class MessagesRenderer extends MessageRendererBase {
045    
046      private static final Log LOG = LogFactory.getLog(MessagesRenderer.class);
047    
048      public int getFixedHeight(FacesContext facesContext, UIComponent component) {
049        int count = 0;
050        for (Iterator i = facesContext.getMessages(); i.hasNext(); i.next()) {
051          count++;
052        }
053        if (LOG.isDebugEnabled()) {
054          LOG.debug("component = '" + component + "'");
055          LOG.debug("here are " + count + " messages");
056        }
057        return (count > 0)
058            ? count * getConfiguredValue(facesContext, component, "messageHeight")
059            : getConfiguredValue(facesContext, component, "fixedHeight");
060      }
061    
062      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
063    
064        UIMessages messages = (UIMessages) component;
065    
066        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
067    
068        if (LOG.isDebugEnabled()) {
069          LOG.debug("facesContext is " + facesContext.getClass().getName());
070        }
071        if (facesContext.getMessages().hasNext()) { // in ie empty span gets a height
072          writer.startElement(HtmlConstants.SPAN, messages);
073          writer.writeClassAttribute("tobago-validation-message");
074          writer.writeStyleAttribute();
075    
076          // with id
077          String focusId = null;
078          Iterator clientIds;
079          if (ComponentUtil.getBooleanAttribute(messages, ATTR_GLOBAL_ONLY)) {
080            ArrayList<String> list = new ArrayList<String>(1);
081            list.add(null);
082            clientIds = list.iterator();
083          } else {
084            clientIds = facesContext.getClientIdsWithMessages();
085          }
086    
087          for (UIMessages.Item item : messages.createMessageList(facesContext)) {
088            encodeMessage(writer, messages, item.getFacesMessage(), item.getClientId());
089          }
090    /*
091          while(clientIds.hasNext()) {
092            String clientId = (String) clientIds.next();
093            encodeMessagesForId(facesContext, writer, clientId, showSummary, showDetail);
094            if (focusId == null) {
095              focusId = clientId;
096            }
097          }
098      todo: don't forget: focus
099          if (focusId != null) {
100            ComponentUtil.findPage(facesContext, messages).setFocusId(focusId);
101          }
102    */
103          writer.endElement(HtmlConstants.SPAN);
104        }
105      }
106    
107      /*
108        private void encodeMessagesForId(FacesContext facesContext,
109            TobagoResponseWriter writer, String clientId, boolean showSummary, boolean showDetail) throws IOException {
110          Iterator iterator = facesContext.getMessages(clientId);
111          while (iterator.hasNext()) {
112            FacesMessage message = (FacesMessage) iterator.next();
113            if (LOG.isDebugEnabled()) {
114              LOG.debug("message = " + message.getSummary());
115            }
116            encodeMessage(writer, message, clientId, showSummary, showDetail);
117          }
118        }
119      */
120      private void encodeMessage(TobagoResponseWriter writer, UIMessages messages, FacesMessage message, String clientId)
121          throws IOException {
122    
123        String summary = message.getSummary();
124        String detail = message.getDetail();
125        writer.startElement(HtmlConstants.LABEL, null);
126        if (clientId != null) {
127          writer.writeAttribute(HtmlAttributes.FOR, clientId, false);
128        }
129        writer.writeAttribute(HtmlAttributes.TITLE, detail, true);
130        StyleClasses classes = new StyleClasses();
131        classes.addMarkupClass("messages", message.getSeverity().toString().toLowerCase());
132        writer.writeClassAttribute(classes);
133        boolean writeEmptyText = true;
134        if (summary != null && messages.isShowSummary()) {
135          writer.writeText(summary);
136          writeEmptyText = false;
137          if (detail != null && messages.isShowDetail()) {
138            writer.writeText(" ");
139          }
140        }
141        if (detail != null && messages.isShowDetail()) {
142          writeEmptyText = false;
143          writer.writeText(detail);
144        }
145        if (writeEmptyText) {
146          writer.writeText("");
147        }
148        writer.endElement(HtmlConstants.LABEL);
149        writer.startElement(HtmlConstants.BR, null);
150        writer.endElement(HtmlConstants.BR);
151      }
152    
153    }
154