KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > MessageRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.util.Debug;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41 import javax.faces.application.FacesMessage;
42 import javax.faces.component.UIComponent;
43 import javax.faces.component.UIMessage;
44 import javax.faces.context.FacesContext;
45 import java.io.IOException JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 public class MessageRenderer extends DomBasicRenderer {
49
50     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
51             throws IOException JavaDoc {
52
53         validateParameters(facesContext, uiComponent, UIMessage.class);
54
55         DOMContext domContext =
56                 DOMContext.attachDOMContext(facesContext, uiComponent);
57         if (domContext.isStreamWriting()) {
58             writeStream(facesContext, uiComponent);
59             return;
60         }
61
62         FacesMessage facesMessage =
63                 getSingleMessage(facesContext, (UIMessage) uiComponent);
64
65
66         if (!domContext.isInitialized()) {
67             Element JavaDoc span = domContext.createElement(HTML.SPAN_ELEM);
68             domContext.setRootNode(span);
69             setRootElementId(facesContext, span, uiComponent);
70         }
71         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
72
73
74         if (facesMessage == null) {
75             if (root != null) {
76                 DOMContext.removeChildren(root);
77                 domContext.stepOver();
78             }
79             return;
80         }
81
82         // Remove the previous message
83
DOMContext.removeChildren(root);
84
85         String JavaDoc[] styleAndStyleClass =
86                 getStyleAndStyleClass(uiComponent, facesMessage);
87         String JavaDoc style = styleAndStyleClass[0];
88         String JavaDoc styleClass = styleAndStyleClass[1];
89
90         if (styleClass != null) {
91             root.setAttribute("class", styleClass);
92         }
93         if (style != null && style.length() > 0) {
94             root.setAttribute("style", style);
95         }
96         else {
97             root.removeAttribute("style");
98         }
99
100         // tooltip
101
boolean tooltip = getToolTipAttribute(uiComponent);
102
103         String JavaDoc[] summaryAndDetail = getSummaryAndDetail(facesMessage);
104         String JavaDoc summary = summaryAndDetail[0];
105         String JavaDoc detail = summaryAndDetail[1];
106
107         // showSummary
108
boolean showSummary = ((UIMessage) uiComponent).isShowSummary();
109         boolean showDetail = ((UIMessage) uiComponent).isShowDetail();
110
111         if (tooltip && showSummary && showDetail) {
112             root.setAttribute("title", summary);
113             Text JavaDoc textNode = domContext.getDocument().createTextNode(detail);
114             root.appendChild(textNode);
115
116         } else {
117             if (showSummary) {
118                 Text JavaDoc textNode =
119                         domContext.getDocument().createTextNode(summary);
120                 root.appendChild(textNode);
121             }
122             if (showDetail) {
123                 Text JavaDoc textNode = domContext.getDocument().createTextNode(detail);
124                 root.appendChild(textNode);
125             }
126         }
127
128         domContext.stepOver();
129     }
130
131
132     private void writeStream(FacesContext facesContext, UIComponent uiComponent)
133             throws IOException JavaDoc {
134         DOMContext domContext =
135                 DOMContext.getDOMContext(facesContext, uiComponent);
136         Element JavaDoc root = domContext.createRootElement(HTML.SPAN_ELEM);
137         Text JavaDoc text = domContext.createTextNode("Message goes here");
138         Object JavaDoc style = uiComponent.getAttributes().get("style");
139         String JavaDoc sstyle = ( (style == null) ? null : style.toString() );
140         if (sstyle != null && sstyle.length() > 0) {
141             root.setAttribute(HTML.STYLE_ATTR, sstyle);
142         }
143         else {
144             root.removeAttribute(HTML.STYLE_ATTR);
145         }
146         root.appendChild(text);
147         domContext.streamWrite(facesContext, uiComponent);
148         domContext.stepOver();
149     }
150
151     /**
152      * @param facesContext
153      * @param uiComponent
154      * @param uiMessage
155      * @param domContext
156      * @return
157      */

158     private FacesMessage getSingleMessage(FacesContext facesContext,
159                                           UIMessage uiMessage) {
160         String JavaDoc forComponentId = uiMessage.getFor();
161         Debug.assertTrue(forComponentId != null,
162                          "For component must not be null");
163         Iterator JavaDoc messages = null;
164         if (forComponentId.length() == 0) {
165             // get the global messages
166
messages = facesContext.getMessages(null);
167         } else {
168             UIComponent forComponent =
169                     findForComponent(facesContext, uiMessage);
170             if (forComponent != null) {
171                 messages = facesContext
172                         .getMessages(forComponent.getClientId(facesContext));
173             }
174         }
175         if (messages == null || !messages.hasNext()) {
176             // there are no messages to render
177
return null;
178         }
179         FacesMessage firstMessage = (FacesMessage) messages.next();
180         return firstMessage;
181     }
182 }
183
184
Popular Tags