001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Text;
039:
040: import javax.faces.application.FacesMessage;
041: import javax.faces.component.UIComponent;
042: import javax.faces.component.UIMessages;
043: import javax.faces.context.FacesContext;
044: import java.io.IOException;
045: import java.util.Iterator;
046:
047: public class MessagesRenderer extends DomBasicRenderer {
048:
049: public void encodeBegin(FacesContext facesContext,
050: UIComponent uiComponent) throws IOException {
051: validateParameters(facesContext, uiComponent, UIMessages.class);
052: }
053:
054: public void encodeChildren(FacesContext facesContext,
055: UIComponent uiComponent) throws IOException {
056: validateParameters(facesContext, uiComponent, UIMessages.class);
057: }
058:
059: public void encodeEnd(FacesContext facesContext,
060: UIComponent uiComponent) throws IOException {
061: validateParameters(facesContext, uiComponent, UIMessages.class);
062:
063: DOMContext domContext = DOMContext.attachDOMContext(
064: facesContext, uiComponent);
065: if (domContext.isStreamWriting()) {
066: writeStream(facesContext, uiComponent);
067: return;
068: }
069:
070: // retrieve the messages
071: Iterator messagesIterator = null;
072: boolean retrieveGlobalMessagesOnly = ((UIMessages) uiComponent)
073: .isGlobalOnly();
074: if (retrieveGlobalMessagesOnly) {
075: messagesIterator = facesContext.getMessages(null);
076: } else {
077: messagesIterator = facesContext.getMessages();
078: }
079:
080: if (!messagesIterator.hasNext()) {
081: domContext.stepOver();
082: return;
083: }
084: // layout
085: boolean tableLayout = false; // default layout is list
086: String layout = (String) uiComponent.getAttributes().get(
087: "layout");
088: if (layout != null && layout.toLowerCase().equals("table")) {
089: tableLayout = true;
090: }
091:
092: // the target element to which messages are appended; either td or span
093: Element parentTarget = null;
094: if (tableLayout) {
095: if (!domContext.isInitialized()) {
096: parentTarget = domContext.createElement("table");
097: domContext.setRootNode(parentTarget);
098: setRootElementId(facesContext, parentTarget,
099: uiComponent);
100: } else {
101: // remove previous messages
102: parentTarget = (Element) domContext.getRootNode();
103: DOMContext.removeChildrenByTagName(parentTarget, "tr");
104: }
105: } else {
106: if (!domContext.isInitialized()) {
107: Element list = domContext.createElement(HTML.UL_ELEM);
108: domContext.setRootNode(list);
109: parentTarget = list;
110: setRootElementId(facesContext, list, uiComponent);
111: } else {
112: // remove previous messages
113: parentTarget = (Element) domContext.getRootNode();
114: DOMContext.removeChildrenByTagName(parentTarget,
115: HTML.LI_ELEM);
116: }
117: }
118:
119: FacesMessage nextFacesMessage = null;
120: while (messagesIterator.hasNext()) {
121:
122: nextFacesMessage = (FacesMessage) messagesIterator.next();
123:
124: String[] styleAndStyleClass = getStyleAndStyleClass(
125: uiComponent, nextFacesMessage);
126: String style = styleAndStyleClass[0];
127: String styleClass = styleAndStyleClass[1];
128:
129: String[] summaryAndDetail = getSummaryAndDetail(nextFacesMessage);
130: String summary = summaryAndDetail[0];
131: String detail = summaryAndDetail[1];
132:
133: boolean showSummary = ((UIMessages) uiComponent)
134: .isShowSummary();
135: boolean showDetail = ((UIMessages) uiComponent)
136: .isShowDetail();
137:
138: Element nextTableData = null;
139: if (tableLayout) {
140: Element tr = domContext.createElement("tr");
141: Element td = domContext.createElement("td");
142: tr.appendChild(td);
143: parentTarget.appendChild(tr);
144: nextTableData = td;
145: }
146:
147: Element nextMessageSpan = domContext
148: .createElement(HTML.SPAN_ELEM);
149: if (tableLayout) {
150: nextTableData.appendChild(nextMessageSpan);
151: } else {
152: Element li = domContext.createElement(HTML.LI_ELEM);
153: parentTarget.appendChild(li);
154: li.appendChild(nextMessageSpan);
155: }
156:
157: if (null != styleClass) {
158: nextMessageSpan.setAttribute("class", styleClass);
159: }
160: if (style != null && style.length() > 0) {
161: nextMessageSpan.setAttribute("style", style);
162: } else {
163: nextMessageSpan.removeAttribute("style");
164: }
165:
166: boolean tooltip = getToolTipAttribute(uiComponent);
167:
168: if (showSummary && showDetail && tooltip) {
169: // show summary as tooltip, detail as child span
170: nextMessageSpan.setAttribute("title", summary);
171: Text textNode = domContext.getDocument()
172: .createTextNode(detail);
173: nextMessageSpan.appendChild(textNode);
174: } else {
175: if (showSummary) {
176: Text textNode = domContext.getDocument()
177: .createTextNode(summary);
178: nextMessageSpan.appendChild(textNode);
179: }
180: if (showDetail) {
181: Text textNode = domContext.getDocument()
182: .createTextNode(detail);
183: nextMessageSpan.appendChild(textNode);
184: }
185: }
186:
187: }
188: domContext.stepOver();
189: }
190:
191: private void writeStream(FacesContext facesContext,
192: UIComponent uiComponent) throws IOException {
193: DOMContext domContext = DOMContext.getDOMContext(facesContext,
194: uiComponent);
195: Element root = domContext.createRootElement(HTML.SPAN_ELEM);
196: Text text = domContext.createTextNode("List of Messages");
197: Object style = uiComponent.getAttributes().get("style");
198: String sstyle = ((style == null) ? null : style.toString());
199: if (sstyle != null && sstyle.length() > 0) {
200: root.setAttribute(HTML.STYLE_ATTR, sstyle);
201: } else {
202: root.removeAttribute(HTML.STYLE_ATTR);
203: }
204: root.appendChild(text);
205: domContext.streamWrite(facesContext, uiComponent);
206: domContext.stepOver();
207: }
208:
209: }
|