KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > wml > MessagesRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.wap.renderkit.wml;
17
18 import java.util.Iterator JavaDoc;
19
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.context.ResponseWriter;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.myfaces.util.NullIterator;
28 import org.apache.myfaces.wap.component.Messages;
29 import org.apache.myfaces.wap.renderkit.Attributes;
30 import org.apache.myfaces.wap.renderkit.WmlRenderer;
31
32 /**
33  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
34  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
35  * $Log: MessagesRenderer.java,v $
36  * Revision 1.1 2004/12/30 09:37:26 matzew
37  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
38  *
39  */

40 public class MessagesRenderer extends WmlRenderer {
41     private static Log log = LogFactory.getLog(MessagesRenderer.class);
42     
43     /** Creates a new instance of TextRenderer */
44     public MessagesRenderer() {
45         super();
46         log.debug("created object " + this.getClass().getName());
47     }
48     
49     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
50         log.debug("encodeBegin(" + component.getId() + ")");
51         if (context == null || component == null) {
52             throw new NullPointerException JavaDoc();
53         }
54     }
55     
56     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
57         log.debug("encodeChildren(" + component.getId() + ")");
58         if (context == null || component == null) {
59             throw new NullPointerException JavaDoc();
60         }
61     }
62     
63     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
64         log.debug("encodeEnd(" + component.getId() + ")");
65         if (context == null || component == null) {
66             throw new NullPointerException JavaDoc();
67         }
68         if (!component.isRendered()) return;
69         
70         Messages comp = (Messages)component;
71         ResponseWriter writer = context.getResponseWriter();
72         
73         MessagesIterator messagesIterator = new MessagesIterator(context, comp.isGlobalOnly());
74         
75         if (messagesIterator.hasNext()) {
76             String JavaDoc layout = comp.getLayout();
77             
78             if (layout == null || layout.equalsIgnoreCase(Attributes.LIST)) {
79                 while(messagesIterator.hasNext()) {
80                     renderMessage(context, comp, (FacesMessage)messagesIterator.next());
81                     
82                     if (messagesIterator.hasNext())
83                         writer.startElement(Attributes.BR, comp);
84                 }
85             }
86             else { // renderers messages as a table
87
writer.startElement(Attributes.TABLE, comp);
88                 writer.writeAttribute(Attributes.COLUMNS, "1", null);
89                 
90                 while(messagesIterator.hasNext()) {
91                     writer.startElement(Attributes.TR, comp);
92                     writer.startElement(Attributes.TD, comp);
93                     renderMessage(context, comp, (FacesMessage)messagesIterator.next());
94                     
95                     if (messagesIterator.hasNext())
96                         writer.startElement(Attributes.BR, comp);
97     
98                     writer.endElement(Attributes.TD);
99                     writer.endElement(Attributes.TR);
100                 }
101                 writer.endElement(Attributes.TABLE);
102             }
103         }
104     }
105     
106     private void renderMessage(FacesContext context, Messages comp, FacesMessage message) throws java.io.IOException JavaDoc{
107         ResponseWriter writer = context.getResponseWriter();
108         
109         if (comp.isShowSummary())
110             writer.write(message.getSummary());
111         if (comp.isShowDetail())
112             writer.write(message.getDetail());
113     }
114     
115     public void decode(FacesContext context, UIComponent component) {
116         if (component == null ) throw new NullPointerException JavaDoc();
117     }
118         
119     private static class MessagesIterator implements Iterator JavaDoc {
120         private FacesContext _facesContext;
121         private Iterator JavaDoc _globalMessagesIterator;
122         private Iterator JavaDoc _clientIdsWithMessagesIterator;
123         private Iterator JavaDoc _componentMessagesIterator = null;
124         private String JavaDoc _clientId = null;
125         
126         public MessagesIterator(FacesContext facesContext, boolean globalOnly) {
127             _facesContext = facesContext;
128             _globalMessagesIterator = facesContext.getMessages(null);
129             if (globalOnly) {
130                 _clientIdsWithMessagesIterator = NullIterator.instance();
131             }
132             else {
133                 _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages();
134             }
135             _componentMessagesIterator = null;
136             _clientId = null;
137         }
138         
139         public boolean hasNext() {
140             return _globalMessagesIterator.hasNext() ||
141             _clientIdsWithMessagesIterator.hasNext() ||
142             (_componentMessagesIterator != null && _componentMessagesIterator.hasNext());
143         }
144         
145         public Object JavaDoc next() {
146             if (_globalMessagesIterator.hasNext()) {
147                 return _globalMessagesIterator.next();
148             }
149             else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()) {
150                 return _componentMessagesIterator.next();
151             }
152             else {
153                 _clientId = (String JavaDoc)_clientIdsWithMessagesIterator.next();
154                 _componentMessagesIterator = _facesContext.getMessages(_clientId);
155                 return _componentMessagesIterator.next();
156             }
157         }
158         
159         public void remove() {
160             throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
161         }
162         
163         public String JavaDoc getClientId() {
164             return _clientId;
165         }
166     }
167     
168 }
169
170
Popular Tags