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