KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > ext > HtmlMessageRenderer


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.renderkit.html.ext;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.myfaces.component.html.ext.HtmlMessage;
21 import org.apache.myfaces.component.html.ext.HtmlMessages;
22 import org.apache.myfaces.renderkit.RendererUtils;
23 import org.apache.myfaces.renderkit.html.HtmlMessageRendererBase;
24
25 import javax.faces.application.FacesMessage;
26 import javax.faces.component.UIColumn;
27 import javax.faces.component.UIComponent;
28 import javax.faces.component.ValueHolder;
29 import javax.faces.component.html.HtmlOutputLabel;
30 import javax.faces.component.html.HtmlOutputText;
31 import javax.faces.context.FacesContext;
32 import java.io.IOException JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
41  * @version $Revision: 1.10 $ $Date: 2005/02/11 13:24:01 $
42  * $Log: HtmlMessageRenderer.java,v $
43  * Revision 1.10 2005/02/11 13:24:01 mmarinschek
44  * fix jira 60, showing fix in examples
45  *
46  * Revision 1.9 2005/01/26 17:48:22 mmarinschek
47  * small changes
48  *
49  * Revision 1.8 2005/01/26 13:27:16 mmarinschek
50  * The x:message tags are now extended to use the column-name as a label for all inputs in an x:dataTable, without having to specify additional information.
51  *
52  * Revision 1.7 2005/01/22 19:47:44 mmarinschek
53  * Message rendering updated - if a validation exception needs to be rendered, the id of the component is replaced with a label.
54  *
55  * Revision 1.6 2004/10/13 11:50:59 matze
56  * renamed packages to org.apache
57  *
58  * Revision 1.5 2004/07/01 21:53:06 mwessendorf
59  * ASF switch
60  *
61  * Revision 1.4 2004/04/05 15:02:46 manolito
62  * for-attribute issues
63  *
64  * Revision 1.3 2004/03/31 14:51:46 manolito
65  * summaryFormat and detailFormat support
66  *
67  * Revision 1.2 2004/03/30 17:47:32 manolito
68  * Message and Messages refactored
69  *
70  * Revision 1.1 2004/03/30 13:27:05 manolito
71  * extended Message component
72  *
73  */

74 public class HtmlMessageRenderer
75         extends HtmlMessageRendererBase
76 {
77     private static final Log log = LogFactory.getLog(HtmlMessageRenderer.class);
78
79     private static final String JavaDoc OUTPUT_LABEL_MAP = HtmlMessageRenderer.class.getName() + ".OUTPUT_LABEL_MAP";
80
81     public void encodeEnd(FacesContext facesContext, UIComponent component)
82             throws IOException JavaDoc
83     {
84         super.encodeEnd(facesContext, component); //check for NP
85
renderMessage(facesContext, component);
86     }
87
88     protected String JavaDoc getSummary(FacesContext facesContext,
89                                 UIComponent message,
90                                 FacesMessage facesMessage,
91                                 String JavaDoc msgClientId)
92     {
93         String JavaDoc msgSummary = facesMessage.getSummary();
94         if (msgSummary == null) return null;
95
96         String JavaDoc inputLabel = null;
97         if (msgClientId != null) inputLabel = findInputLabel(facesContext, msgClientId);
98         if (inputLabel == null) inputLabel = "";
99
100         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
101                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
102                 inputLabel.length()!=0)
103             msgSummary = msgSummary.replaceAll(findInputId(facesContext, msgClientId),inputLabel);
104
105
106         String JavaDoc summaryFormat;
107         if (message instanceof HtmlMessage)
108         {
109             summaryFormat = ((HtmlMessage)message).getSummaryFormat();
110         }
111         else
112         {
113             summaryFormat = (String JavaDoc)message.getAttributes().get("summaryFormat");
114         }
115
116         if (summaryFormat == null) return msgSummary;
117
118         MessageFormat JavaDoc format = new MessageFormat JavaDoc(summaryFormat, facesContext.getViewRoot().getLocale());
119
120         return format.format(new Object JavaDoc[] {msgSummary, inputLabel});
121     }
122
123     protected String JavaDoc getDetail(FacesContext facesContext,
124                                UIComponent message,
125                                FacesMessage facesMessage,
126                                String JavaDoc msgClientId)
127     {
128         String JavaDoc msgDetail = facesMessage.getDetail();
129         if (msgDetail == null) return null;
130
131         String JavaDoc inputLabel = null;
132         if (msgClientId != null) inputLabel = findInputLabel(facesContext, msgClientId);
133         if (inputLabel == null) inputLabel = "";
134
135         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
136                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
137                 inputLabel.length()!=0)
138             msgDetail = msgDetail.replaceAll(findInputId(facesContext, msgClientId),inputLabel);
139
140         String JavaDoc detailFormat;
141         if (message instanceof HtmlMessage)
142         {
143             detailFormat = ((HtmlMessage)message).getDetailFormat();
144         }
145         else
146         {
147             detailFormat = (String JavaDoc)message.getAttributes().get("detailFormat");
148         }
149
150         if (detailFormat == null) return msgDetail;
151
152         MessageFormat JavaDoc format = new MessageFormat JavaDoc(detailFormat, facesContext.getViewRoot().getLocale());
153         return format.format(new Object JavaDoc[] {msgDetail, inputLabel});
154     }
155
156
157     public static String JavaDoc findInputLabel(FacesContext facesContext, String JavaDoc inputClientId)
158     {
159         Map JavaDoc outputLabelMap = getOutputLabelMap(facesContext);
160         MessageLabelInfo info = ((MessageLabelInfo)outputLabelMap.get(inputClientId));
161
162         if(info == null)
163         {
164             UIComponent comp = facesContext.getViewRoot().findComponent(inputClientId);
165
166             UIComponent parent=comp;
167
168             while(parent != null && !((parent=parent.getParent())instanceof UIColumn));
169
170             if(parent != null)
171             {
172                 UIColumn column = (UIColumn) parent;
173
174                 if(column.getHeader()!=null)
175                 {
176                     UIComponent header = column.getHeader();
177
178                     return getComponentText(facesContext, header);
179                 }
180             }
181         }
182
183         return info==null?null:info.getText();
184     }
185
186     public static String JavaDoc findInputId(FacesContext facesContext, String JavaDoc inputClientId)
187     {
188         Map JavaDoc outputLabelMap = getOutputLabelMap(facesContext);
189         MessageLabelInfo info = ((MessageLabelInfo)outputLabelMap.get(inputClientId));
190
191         if(info == null)
192         {
193             UIComponent comp = facesContext.getViewRoot().findComponent(inputClientId);
194
195             if(comp!=null)
196             {
197                 return comp.getId();
198             }
199         }
200
201         return info==null?null:(info.getForComponent()==null?null:info.getForComponent().getId());
202     }
203
204     /**
205      * @param facesContext
206      * @return a Map that reversely maps clientIds of components to their
207      * corresponding OutputLabel component
208      */

209     private static Map JavaDoc getOutputLabelMap(FacesContext facesContext)
210     {
211         Map JavaDoc map = (Map JavaDoc)facesContext.getExternalContext().getRequestMap().get(OUTPUT_LABEL_MAP);
212         if (map == null)
213         {
214             map = new HashMap JavaDoc();
215             createOutputLabelMap(facesContext, facesContext.getViewRoot(), map);
216             facesContext.getExternalContext().getRequestMap().put(OUTPUT_LABEL_MAP, map);
217         }
218         return map;
219     }
220
221     private static void createOutputLabelMap(FacesContext facesContext,
222                                              UIComponent root,
223                                              Map JavaDoc map)
224     {
225         for (Iterator JavaDoc it = root.getFacetsAndChildren(); it.hasNext(); )
226         {
227             UIComponent child = (UIComponent)it.next();
228             if (child instanceof HtmlOutputLabel)
229             {
230                 String JavaDoc forAttr = ((HtmlOutputLabel)child).getFor();
231                 UIComponent input = child.findComponent(forAttr);
232                 if (input == null)
233                 {
234                     log.warn("Unable to find component '" + forAttr + "' (calling findComponent on component '" + child.getClientId(facesContext) + "')");
235                 }
236                 else
237                 {
238                     map.put(input.getClientId(facesContext),
239                             new MessageLabelInfo(
240                                     input,getComponentText(facesContext, (HtmlOutputLabel)child)));
241                 }
242             }
243             else
244             {
245                 createOutputLabelMap(facesContext, child, map);
246             }
247         }
248     }
249
250     private static String JavaDoc getComponentText(FacesContext facesContext, UIComponent component)
251     {
252         String JavaDoc text = null;
253
254         if(component instanceof ValueHolder)
255         {
256             text=RendererUtils.getStringValue(facesContext, component);
257         }
258
259         if (text == null)
260         {
261             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
262             List JavaDoc li = component.getChildren();
263
264             for (int i = 0; i < li.size(); i++)
265             {
266                 UIComponent child = (UIComponent) li.get(i);
267
268                 if(child instanceof HtmlOutputText)
269                 {
270                     String JavaDoc str = RendererUtils.getStringValue(facesContext, child);
271
272                     if(str!=null)
273                         buf.append(str);
274                 }
275             }
276
277             text = buf.toString();
278         }
279         return text;
280     }
281
282     public static class MessageLabelInfo
283     {
284         private UIComponent _forComponent;
285         private String JavaDoc _text;
286
287         public MessageLabelInfo(UIComponent forComponent, String JavaDoc text)
288         {
289             _forComponent = forComponent;
290             _text = text;
291         }
292
293         public UIComponent getForComponent()
294         {
295             return _forComponent;
296         }
297
298         public void setForComponent(UIComponent forComponent)
299         {
300             _forComponent = forComponent;
301         }
302
303         public String JavaDoc getText()
304         {
305             return _text;
306         }
307
308         public void setText(String JavaDoc text)
309         {
310             _text = text;
311         }
312     }
313 }
314
Popular Tags