001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/java/org/sakaiproject/tool/section/jsf/DivMessageRendererBase.java $
003: * $Id: DivMessageRendererBase.java 9245 2006-05-10 17:48:20Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.section.jsf;
021:
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.faces.application.FacesMessage;
027: import javax.faces.component.UIComponent;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ResponseWriter;
030: import javax.faces.render.Renderer;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * Base class for custom message renderers.
037: *
038: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
039: *
040: */
041: public abstract class DivMessageRendererBase extends Renderer {
042: private static final Log logger = LogFactory
043: .getLog(DivMessageRendererBase.class);
044:
045: public static String INNER_TAG = "div";
046:
047: public static Map severityToStyleAttr, severityToClassAttr;
048: {
049: severityToStyleAttr = new HashMap();
050: severityToStyleAttr
051: .put(FacesMessage.SEVERITY_INFO, "infoStyle");
052: severityToStyleAttr
053: .put(FacesMessage.SEVERITY_WARN, "warnStyle");
054: severityToStyleAttr.put(FacesMessage.SEVERITY_ERROR,
055: "errorStyle");
056: severityToStyleAttr.put(FacesMessage.SEVERITY_FATAL,
057: "fatalStyle");
058: severityToClassAttr = new HashMap();
059: severityToClassAttr
060: .put(FacesMessage.SEVERITY_INFO, "infoClass");
061: severityToClassAttr
062: .put(FacesMessage.SEVERITY_WARN, "warnClass");
063: severityToClassAttr.put(FacesMessage.SEVERITY_ERROR,
064: "errorClass");
065: severityToClassAttr.put(FacesMessage.SEVERITY_FATAL,
066: "fatalClass");
067: }
068:
069: public String getMessageStyle(UIComponent component,
070: FacesMessage message) {
071: String messageStyle = (String) component.getAttributes().get(
072: "style");
073: FacesMessage.Severity severity = message.getSeverity();
074: if (severity != null) {
075: String severitySpecific = (String) component
076: .getAttributes().get(
077: (String) severityToStyleAttr.get(severity));
078: if ((severitySpecific != null)
079: && (severitySpecific.length() > 0)) {
080: messageStyle = severitySpecific;
081: }
082: }
083: return messageStyle;
084: }
085:
086: public String getMessageClass(UIComponent component,
087: FacesMessage message) {
088: String messageClass = (String) component.getAttributes().get(
089: "styleClass");
090: FacesMessage.Severity severity = message.getSeverity();
091: if (severity != null) {
092: String severitySpecific = (String) component
093: .getAttributes().get(
094: (String) severityToClassAttr.get(severity));
095: if ((severitySpecific != null)
096: && (severitySpecific.length() > 0)) {
097: messageClass = severitySpecific;
098: }
099: }
100: return messageClass;
101: }
102:
103: public void renderMessage(FacesContext context,
104: UIComponent component, FacesMessage message)
105: throws IOException {
106: ResponseWriter out = context.getResponseWriter();
107: out.startElement(INNER_TAG, component);
108: String msgClass = getMessageClass(component, message);
109: if (msgClass != null) {
110: out.writeAttribute("class", msgClass, "styleClass");
111: }
112: String msgStyle = getMessageStyle(component, message);
113: if (msgStyle != null) {
114: out.writeAttribute("style", msgStyle, "style");
115: }
116:
117: // In this first implementation, all we write is the summary.
118: out.writeText(message.getSummary(), null);
119:
120: out.endElement(INNER_TAG);
121: }
122: }
|