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 com.icesoft.faces.util.Debug;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Text;
040:
041: import javax.faces.application.FacesMessage;
042: import javax.faces.component.UIComponent;
043: import javax.faces.component.UIMessage;
044: import javax.faces.context.FacesContext;
045: import java.io.IOException;
046: import java.util.Iterator;
047:
048: public class MessageRenderer extends DomBasicRenderer {
049:
050: public void encodeBegin(FacesContext facesContext,
051: UIComponent uiComponent) throws IOException {
052:
053: validateParameters(facesContext, uiComponent, UIMessage.class);
054:
055: DOMContext domContext = DOMContext.attachDOMContext(
056: facesContext, uiComponent);
057: if (domContext.isStreamWriting()) {
058: writeStream(facesContext, uiComponent);
059: return;
060: }
061:
062: FacesMessage facesMessage = getSingleMessage(facesContext,
063: (UIMessage) uiComponent);
064:
065: if (facesMessage == null) {
066: domContext.stepOver();
067: return;
068: }
069:
070: if (!domContext.isInitialized()) {
071: Element span = domContext.createElement(HTML.SPAN_ELEM);
072: domContext.setRootNode(span);
073: setRootElementId(facesContext, span, uiComponent);
074: }
075: Element root = (Element) domContext.getRootNode();
076:
077: // Remove the previous message
078: DOMContext.removeChildren(root);
079:
080: String[] styleAndStyleClass = getStyleAndStyleClass(
081: uiComponent, facesMessage);
082: String style = styleAndStyleClass[0];
083: String styleClass = styleAndStyleClass[1];
084:
085: if (styleClass != null) {
086: root.setAttribute("class", styleClass);
087: }
088: if (style != null && style.length() > 0) {
089: root.setAttribute("style", style);
090: } else {
091: root.removeAttribute("style");
092: }
093:
094: // tooltip
095: boolean tooltip = getToolTipAttribute(uiComponent);
096:
097: String[] summaryAndDetail = getSummaryAndDetail(facesMessage);
098: String summary = summaryAndDetail[0];
099: String detail = summaryAndDetail[1];
100:
101: // showSummary
102: boolean showSummary = ((UIMessage) uiComponent).isShowSummary();
103: boolean showDetail = ((UIMessage) uiComponent).isShowDetail();
104:
105: if (tooltip && showSummary && showDetail) {
106: root.setAttribute("title", summary);
107: Text textNode = domContext.getDocument().createTextNode(
108: detail);
109: root.appendChild(textNode);
110:
111: } else {
112: if (showSummary) {
113: Text textNode = domContext.getDocument()
114: .createTextNode(summary);
115: root.appendChild(textNode);
116: }
117: if (showDetail) {
118: Text textNode = domContext.getDocument()
119: .createTextNode(detail);
120: root.appendChild(textNode);
121: }
122: }
123:
124: domContext.stepOver();
125: }
126:
127: private void writeStream(FacesContext facesContext,
128: UIComponent uiComponent) throws IOException {
129: DOMContext domContext = DOMContext.getDOMContext(facesContext,
130: uiComponent);
131: Element root = domContext.createRootElement(HTML.SPAN_ELEM);
132: Text text = domContext.createTextNode("Message goes here");
133: Object style = uiComponent.getAttributes().get("style");
134: String sstyle = ((style == null) ? null : style.toString());
135: if (sstyle != null && sstyle.length() > 0) {
136: root.setAttribute(HTML.STYLE_ATTR, sstyle);
137: } else {
138: root.removeAttribute(HTML.STYLE_ATTR);
139: }
140: root.appendChild(text);
141: domContext.streamWrite(facesContext, uiComponent);
142: domContext.stepOver();
143: }
144:
145: /**
146: * @param facesContext
147: * @param uiComponent
148: * @param uiMessage
149: * @param domContext
150: * @return
151: */
152: private FacesMessage getSingleMessage(FacesContext facesContext,
153: UIMessage uiMessage) {
154: String forComponentId = uiMessage.getFor();
155: Debug.assertTrue(forComponentId != null,
156: "For component must not be null");
157: Iterator messages = null;
158: if (forComponentId.length() == 0) {
159: // get the global messages
160: messages = facesContext.getMessages(null);
161: } else {
162: UIComponent forComponent = findForComponent(facesContext,
163: uiMessage);
164: if (forComponent != null) {
165: messages = facesContext.getMessages(forComponent
166: .getClientId(facesContext));
167: }
168: }
169: if (messages == null || !messages.hasNext()) {
170: // there are no messages to render
171: return null;
172: }
173: FacesMessage firstMessage = (FacesMessage) messages.next();
174: return firstMessage;
175: }
176: }
|