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.*;
035: import javax.faces.application.*;
036: import javax.faces.component.*;
037: import javax.faces.component.html.*;
038: import javax.faces.context.*;
039: import javax.faces.render.*;
040:
041: /**
042: * The HTML messages renderer
043: */
044: class HtmlMessagesRenderer extends Renderer {
045: public static final Renderer RENDERER = new HtmlMessagesRenderer();
046:
047: /**
048: * True if the renderer is responsible for rendering the children.
049: */
050: @Override
051: public boolean getRendersChildren() {
052: return true;
053: }
054:
055: /**
056: * Renders the open tag for the text.
057: */
058: @Override
059: public void encodeBegin(FacesContext context, UIComponent component)
060: throws IOException {
061: UIMessages uiMessage = (UIMessages) component;
062:
063: Iterator<FacesMessage> iter = context.getMessages();
064:
065: ResponseWriter out = context.getResponseWriter();
066:
067: String id = component.getId();
068:
069: String dir;
070: String lang;
071: String layout;
072: String errorClass;
073: String errorStyle;
074: String fatalClass;
075: String fatalStyle;
076: String infoClass;
077: String infoStyle;
078: String style;
079: String styleClass;
080: String title;
081: boolean tooltip;
082: String warnClass;
083: String warnStyle;
084: boolean isShowSummary;
085: boolean isShowDetail;
086:
087: if (component instanceof HtmlMessages) {
088: HtmlMessages htmlComp = (HtmlMessages) component;
089:
090: isShowSummary = htmlComp.isShowSummary();
091: isShowDetail = htmlComp.isShowDetail();
092:
093: errorClass = htmlComp.getErrorClass();
094: errorStyle = htmlComp.getErrorStyle();
095:
096: fatalClass = htmlComp.getFatalClass();
097: fatalStyle = htmlComp.getFatalStyle();
098:
099: dir = htmlComp.getDir();
100:
101: infoClass = htmlComp.getInfoClass();
102: infoStyle = htmlComp.getInfoStyle();
103:
104: lang = htmlComp.getLang();
105: layout = htmlComp.getLayout();
106:
107: style = htmlComp.getStyle();
108: styleClass = htmlComp.getStyleClass();
109: title = htmlComp.getTitle();
110: tooltip = htmlComp.isTooltip();
111:
112: warnClass = htmlComp.getWarnClass();
113: warnStyle = htmlComp.getWarnStyle();
114: } else {
115: Map<String, Object> attrMap = component.getAttributes();
116:
117: isShowSummary = (Boolean) attrMap.get("showSummary");
118: isShowDetail = (Boolean) attrMap.get("showDetail");
119:
120: dir = (String) attrMap.get("dir");
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: layout = (String) attrMap.get("layout");
133: style = (String) attrMap.get("style");
134: styleClass = (String) attrMap.get("styleClass");
135: title = (String) attrMap.get("title");
136: tooltip = (Boolean) attrMap.get("tooltip");
137: }
138:
139: boolean isFirst = true;
140:
141: while (iter.hasNext()) {
142: FacesMessage msg = iter.next();
143:
144: if (isFirst) {
145: if ("table".equals(layout))
146: out.startElement("table", component);
147: else
148: out.startElement("ul", component);
149:
150: if (dir != null)
151: out.writeAttribute("dir", dir, "dir");
152:
153: if (lang != null)
154: out.writeAttribute("lang", lang, "lang");
155:
156: if (style != null)
157: out.writeAttribute("style", style, "style");
158:
159: if (styleClass != null)
160: out.writeAttribute("class", styleClass,
161: "styleClass");
162:
163: if (title != null)
164: out.writeAttribute("title", title, "title");
165: }
166: isFirst = false;
167:
168: if ("table".equals(layout)) {
169: out.startElement("tr", component);
170: out.startElement("td", component);
171: } else
172: out.startElement("li", component);
173:
174: if (FacesMessage.SEVERITY_ERROR.equals(msg.getSeverity())) {
175: if (errorClass != null)
176: out.writeAttribute("class", errorClass,
177: "errorClass");
178:
179: if (errorStyle != null)
180: out.writeAttribute("style", errorStyle,
181: "errorStyle");
182: } else if (FacesMessage.SEVERITY_FATAL.equals(msg
183: .getSeverity())) {
184: if (fatalClass != null)
185: out.writeAttribute("class", fatalClass,
186: "fatalClass");
187:
188: if (fatalStyle != null)
189: out.writeAttribute("style", fatalStyle,
190: "fatalStyle");
191: } else if (FacesMessage.SEVERITY_INFO.equals(msg
192: .getSeverity())) {
193: if (infoClass != null)
194: out.writeAttribute("class", infoClass, "infoClass");
195:
196: if (infoStyle != null)
197: out.writeAttribute("style", infoStyle, "infoStyle");
198: } else if (FacesMessage.SEVERITY_WARN.equals(msg
199: .getSeverity())) {
200: if (warnClass != null)
201: out.writeAttribute("class", warnClass, "warnClass");
202:
203: if (warnStyle != null)
204: out.writeAttribute("style", warnStyle, "warnStyle");
205: }
206:
207: if (isShowSummary)
208: out.writeText(msg.getSummary(), "summary");
209:
210: if (isShowDetail)
211: out.writeText(msg.getDetail(), "detail");
212:
213: if ("table".equals(layout)) {
214: out.endElement("td");
215: out.endElement("tr");
216: } else
217: out.endElement("li");
218: }
219:
220: if (!isFirst) {
221: if ("table".equals(layout))
222: out.endElement("table");
223: else
224: out.endElement("ul");
225: }
226: }
227:
228: /**
229: * Renders the content for the component.
230: */
231: @Override
232: public void encodeChildren(FacesContext context,
233: UIComponent component) throws IOException {
234: }
235:
236: /**
237: * Renders the closing tag for the component.
238: */
239: @Override
240: public void encodeEnd(FacesContext context, UIComponent component)
241: throws IOException {
242: }
243:
244: public String toString() {
245: return "HtmlMessagesRenderer[]";
246: }
247: }
|