001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: import javax.faces.application.*;
035: import javax.faces.component.*;
036: import javax.faces.component.html.*;
037: import javax.faces.context.*;
038: import javax.faces.render.*;
039:
040: /**
041: * The HTML message renderer
042: */
043: class HtmlMessageRenderer extends Renderer {
044: public static final Renderer RENDERER = new HtmlMessageRenderer();
045:
046: /**
047: * True if the renderer is responsible for rendering the children.
048: */
049: @Override
050: public boolean getRendersChildren() {
051: return true;
052: }
053:
054: /**
055: * Renders the open tag for the text.
056: */
057: @Override
058: public void encodeBegin(FacesContext context, UIComponent component)
059: throws IOException {
060: UIMessage uiMessage = (UIMessage) component;
061:
062: ResponseWriter out = context.getResponseWriter();
063:
064: String id = component.getId();
065:
066: String dir;
067: String lang;
068: String errorClass;
069: String errorStyle;
070: String fatalClass;
071: String fatalStyle;
072: String forValue;
073: String infoClass;
074: String infoStyle;
075: String style;
076: String styleClass;
077: String title;
078: boolean tooltip;
079: String warnClass;
080: String warnStyle;
081: boolean isShowSummary;
082: boolean isShowDetail;
083:
084: if (component instanceof HtmlMessage) {
085: HtmlMessage htmlComp = (HtmlMessage) component;
086:
087: isShowSummary = htmlComp.isShowSummary();
088: isShowDetail = htmlComp.isShowDetail();
089:
090: errorClass = htmlComp.getErrorClass();
091: errorStyle = htmlComp.getErrorStyle();
092:
093: fatalClass = htmlComp.getFatalClass();
094: fatalStyle = htmlComp.getFatalStyle();
095:
096: dir = htmlComp.getDir();
097:
098: forValue = htmlComp.getFor();
099:
100: infoClass = htmlComp.getInfoClass();
101: infoStyle = htmlComp.getInfoStyle();
102:
103: lang = htmlComp.getLang();
104:
105: style = htmlComp.getStyle();
106: styleClass = htmlComp.getStyleClass();
107: title = htmlComp.getTitle();
108: tooltip = htmlComp.isTooltip();
109:
110: warnClass = htmlComp.getWarnClass();
111: warnStyle = htmlComp.getWarnStyle();
112: } else {
113: Map<String, Object> attrMap = component.getAttributes();
114:
115: isShowSummary = (Boolean) attrMap.get("showSummary");
116: isShowDetail = (Boolean) attrMap.get("showDetail");
117:
118: dir = (String) attrMap.get("dir");
119:
120: forValue = (String) attrMap.get("for");
121:
122: errorClass = (String) attrMap.get("errorClass");
123: errorStyle = (String) attrMap.get("errorStyle");
124: fatalClass = (String) attrMap.get("fatalClass");
125: fatalStyle = (String) attrMap.get("fatalStyle");
126: infoClass = (String) attrMap.get("infoClass");
127: infoStyle = (String) attrMap.get("infoStyle");
128: warnClass = (String) attrMap.get("warnClass");
129: warnStyle = (String) attrMap.get("warnStyle");
130:
131: lang = (String) attrMap.get("lang");
132: style = (String) attrMap.get("style");
133: styleClass = (String) attrMap.get("styleClass");
134: title = (String) attrMap.get("title");
135: tooltip = (Boolean) attrMap.get("tooltip");
136: }
137:
138: Iterator<FacesMessage> iter;
139:
140: UIComponent forComponent = component.findComponent(forValue);
141:
142: if (forComponent != null) {
143: iter = context.getMessages(forComponent
144: .getClientId(context));
145:
146: if (iter.hasNext()) {
147: FacesMessage msg = iter.next();
148:
149: if (FacesMessage.SEVERITY_ERROR.equals(msg
150: .getSeverity())) {
151: if (errorClass != null)
152: styleClass = errorClass;
153:
154: if (errorStyle != null)
155: style = errorStyle;
156: } else if (FacesMessage.SEVERITY_FATAL.equals(msg
157: .getSeverity())) {
158: if (fatalClass != null)
159: styleClass = fatalClass;
160:
161: if (fatalStyle != null)
162: style = fatalStyle;
163: } else if (FacesMessage.SEVERITY_INFO.equals(msg
164: .getSeverity())) {
165: if (infoClass != null)
166: styleClass = infoClass;
167:
168: if (errorStyle != null)
169: style = infoStyle;
170: } else if (FacesMessage.SEVERITY_WARN.equals(msg
171: .getSeverity())) {
172: if (warnClass != null)
173: styleClass = warnClass;
174:
175: if (warnStyle != null)
176: style = warnStyle;
177: }
178:
179: boolean hasSpan = (dir != null || lang != null
180: || style != null || styleClass != null
181: || title != null || tooltip);
182:
183: if (hasSpan)
184: out.startElement("span", component);
185:
186: if (dir != null)
187: out.writeAttribute("dir", dir, "dir");
188:
189: if (lang != null)
190: out.writeAttribute("lang", lang, "lang");
191:
192: if (style != null)
193: out.writeAttribute("style", style, "style");
194:
195: if (styleClass != null)
196: out.writeAttribute("class", styleClass,
197: "styleClass");
198:
199: if (isShowSummary) {
200: if (tooltip && isShowDetail) {
201: out.writeAttribute("title", msg.getSummary(),
202: "title");
203: } else if (title != null) {
204: out.writeAttribute("title", title, "title");
205: out.writeText(msg.getSummary(), "summary");
206: } else {
207: out.writeText(msg.getSummary(), "summary");
208: }
209: }
210:
211: if (isShowDetail)
212: out.writeText(msg.getDetail(), "detail");
213:
214: if (hasSpan)
215: out.endElement("span");
216: }
217: }
218: }
219:
220: /**
221: * Renders the content for the component.
222: */
223: @Override
224: public void encodeChildren(FacesContext context,
225: UIComponent component) throws IOException {
226: }
227:
228: /**
229: * Renders the closing tag for the component.
230: */
231: @Override
232: public void encodeEnd(FacesContext context, UIComponent component)
233: throws IOException {
234: }
235:
236: public String toString() {
237: return "HtmlMessageRenderer[]";
238: }
239: }
|