KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Text JavaDoc;
39
40 import javax.faces.application.FacesMessage;
41 import javax.faces.component.UIComponent;
42 import javax.faces.component.UIMessages;
43 import javax.faces.context.FacesContext;
44 import java.io.IOException JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47
48 public class MessagesRenderer extends DomBasicRenderer {
49
50     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
51             throws IOException JavaDoc {
52         validateParameters(facesContext, uiComponent, UIMessages.class);
53     }
54
55     public void encodeChildren(FacesContext facesContext,
56                                UIComponent uiComponent) throws IOException JavaDoc {
57         validateParameters(facesContext, uiComponent, UIMessages.class);
58     }
59
60     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
61             throws IOException JavaDoc {
62         validateParameters(facesContext, uiComponent, UIMessages.class);
63
64         DOMContext domContext =
65                 DOMContext.attachDOMContext(facesContext, uiComponent);
66         if (domContext.isStreamWriting()) {
67             writeStream(facesContext, uiComponent);
68             return;
69         }
70
71         // retrieve the messages
72
Iterator JavaDoc messagesIterator = null;
73         boolean retrieveGlobalMessagesOnly =
74                 ((UIMessages) uiComponent).isGlobalOnly();
75         if (retrieveGlobalMessagesOnly) {
76             messagesIterator = facesContext.getMessages(null);
77         } else {
78             messagesIterator = facesContext.getMessages();
79         }
80
81         // layout
82
boolean tableLayout = false; // default layout is list
83
String JavaDoc layout = (String JavaDoc) uiComponent.getAttributes().get("layout");
84         if (layout != null && layout.toLowerCase().equals("table")) {
85             tableLayout = true;
86         }
87
88         // the target element to which messages are appended; either td or span
89
Element JavaDoc parentTarget = null;
90         if (tableLayout) {
91             if (!domContext.isInitialized()) {
92                 parentTarget = domContext.createElement("table");
93                 domContext.setRootNode(parentTarget);
94                 setRootElementId(facesContext, parentTarget, uiComponent);
95             } else {
96                 // remove previous messages
97
parentTarget = (Element JavaDoc) domContext.getRootNode();
98                 DOMContext.removeChildrenByTagName(parentTarget, "tr");
99             }
100         } else {
101             if (!domContext.isInitialized()) {
102                 Element JavaDoc list = domContext.createElement(HTML.UL_ELEM);
103                 domContext.setRootNode(list);
104                 parentTarget = list;
105                 setRootElementId(facesContext, list, uiComponent);
106             } else {
107                 // remove previous messages
108
parentTarget = (Element JavaDoc) domContext.getRootNode();
109                 DOMContext.removeChildrenByTagName(parentTarget, HTML.LI_ELEM);
110             }
111         }
112
113         FacesMessage nextFacesMessage = null;
114         while (messagesIterator.hasNext()) {
115
116             nextFacesMessage = (FacesMessage) messagesIterator.next();
117
118             String JavaDoc[] styleAndStyleClass =
119                     getStyleAndStyleClass(uiComponent, nextFacesMessage);
120             String JavaDoc style = styleAndStyleClass[0];
121             String JavaDoc styleClass = styleAndStyleClass[1];
122
123             String JavaDoc[] summaryAndDetail = getSummaryAndDetail(nextFacesMessage);
124             String JavaDoc summary = summaryAndDetail[0];
125             String JavaDoc detail = summaryAndDetail[1];
126
127             boolean showSummary = ((UIMessages) uiComponent).isShowSummary();
128             boolean showDetail = ((UIMessages) uiComponent).isShowDetail();
129
130             Element JavaDoc nextTableData = null;
131             if (tableLayout) {
132                 Element JavaDoc tr = domContext.createElement("tr");
133                 Element JavaDoc td = domContext.createElement("td");
134                 tr.appendChild(td);
135                 parentTarget.appendChild(tr);
136                 nextTableData = td;
137             }
138
139             Element JavaDoc nextMessageSpan = domContext.createElement(HTML.SPAN_ELEM);
140             if (tableLayout) {
141                 nextTableData.appendChild(nextMessageSpan);
142             } else {
143                 Element JavaDoc li = domContext.createElement(HTML.LI_ELEM);
144                 parentTarget.appendChild(li);
145                 li.appendChild(nextMessageSpan);
146             }
147
148             if (null != styleClass) {
149                 nextMessageSpan.setAttribute("class", styleClass);
150             }
151             if (style != null && style.length() > 0) {
152                 nextMessageSpan.setAttribute("style", style);
153             }
154             else {
155                 nextMessageSpan.removeAttribute("style");
156             }
157
158             boolean tooltip = getToolTipAttribute(uiComponent);
159
160             if (showSummary && showDetail && tooltip) {
161                 // show summary as tooltip, detail as child span
162
nextMessageSpan.setAttribute("title", summary);
163                 Text JavaDoc textNode = domContext.getDocument().createTextNode(detail);
164                 nextMessageSpan.appendChild(textNode);
165             } else {
166                 if (showSummary) {
167                     Text JavaDoc textNode =
168                             domContext.getDocument().createTextNode(summary);
169                     nextMessageSpan.appendChild(textNode);
170                 }
171                 if (showDetail) {
172                     Text JavaDoc textNode =
173                             domContext.getDocument().createTextNode(detail);
174                     nextMessageSpan.appendChild(textNode);
175                 }
176             }
177
178         }
179         domContext.stepOver();
180     }
181
182     private void writeStream(FacesContext facesContext, UIComponent uiComponent)
183             throws IOException JavaDoc {
184         DOMContext domContext =
185                 DOMContext.getDOMContext(facesContext, uiComponent);
186         Element JavaDoc root = domContext.createRootElement(HTML.SPAN_ELEM);
187         Text JavaDoc text = domContext.createTextNode("List of Messages");
188         Object JavaDoc style = uiComponent.getAttributes().get("style");
189         String JavaDoc sstyle = ( (style == null) ? null : style.toString() );
190         if (sstyle != null && sstyle.length() > 0) {
191             root.setAttribute(HTML.STYLE_ATTR, sstyle);
192         }
193         else {
194             root.removeAttribute(HTML.STYLE_ATTR);
195         }
196         root.appendChild(text);
197         domContext.streamWrite(facesContext, uiComponent);
198         domContext.stepOver();
199     }
200
201 }
202
203
Popular Tags