KickJava   Java API By Example, From Geeks To Geeks.

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


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.myfaces.component.html.ext.HtmlMessage;
19 import org.apache.myfaces.component.html.ext.HtmlMessages;
20 import org.apache.myfaces.renderkit.html.HtmlMessagesRendererBase;
21
22 import javax.faces.application.FacesMessage;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27
28 /**
29  * @author Manfred Geiler (latest modification by $Author: mmarinschek $)
30  * @version $Revision: 1.6 $ $Date: 2005/01/22 19:47:44 $
31  * $Log: HtmlMessagesRenderer.java,v $
32  * Revision 1.6 2005/01/22 19:47:44 mmarinschek
33  * Message rendering updated - if a validation exception needs to be rendered, the id of the component is replaced with a label.
34  *
35  * Revision 1.5 2004/10/13 11:50:59 matze
36  * renamed packages to org.apache
37  *
38  * Revision 1.4 2004/07/01 21:53:06 mwessendorf
39  * ASF switch
40  *
41  * Revision 1.3 2004/04/01 14:34:22 manolito
42  * new globalSummaryFormat attribute
43  *
44  * Revision 1.2 2004/03/31 14:51:46 manolito
45  * summaryFormat and detailFormat support
46  *
47  * Revision 1.1 2004/03/30 17:47:32 manolito
48  * Message and Messages refactored
49  *
50  */

51 public class HtmlMessagesRenderer
52         extends HtmlMessagesRendererBase
53 {
54     //private static final Log log = LogFactory.getLog(HtmlMessagesRenderer.class);
55

56
57     public void encodeEnd(FacesContext facesContext, UIComponent component)
58             throws IOException JavaDoc
59     {
60         super.encodeEnd(facesContext, component); //check for NP
61
renderMessages(facesContext, component);
62     }
63
64     protected String JavaDoc getSummary(FacesContext facesContext,
65                                 UIComponent message,
66                                 FacesMessage facesMessage,
67                                 String JavaDoc msgClientId)
68     {
69         String JavaDoc msgSummary = facesMessage.getSummary();
70         if (msgSummary == null) return null;
71
72         String JavaDoc inputLabel = null;
73         if (msgClientId != null) inputLabel = HtmlMessageRenderer.findInputLabel(facesContext, msgClientId);
74         if (inputLabel == null) inputLabel = "";
75
76         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
77                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
78                 inputLabel.length()!=0)
79             msgSummary = msgSummary.replaceAll(HtmlMessageRenderer.findInputId(facesContext, msgClientId),inputLabel);
80
81
82         String JavaDoc summaryFormat;
83         if (msgClientId == null)
84         {
85             summaryFormat = getGlobalSummaryFormat(message);
86             if (summaryFormat == null)
87             {
88                 summaryFormat = getSummaryFormat(message);
89             }
90         }
91         else
92         {
93             summaryFormat = getSummaryFormat(message);
94         }
95         if (summaryFormat == null) return msgSummary;
96
97         MessageFormat JavaDoc format = new MessageFormat JavaDoc(summaryFormat, facesContext.getViewRoot().getLocale());
98         return format.format(new Object JavaDoc[] {msgSummary, inputLabel});
99     }
100
101
102     private String JavaDoc getSummaryFormat(UIComponent message)
103     {
104         if (message instanceof HtmlMessages)
105         {
106             return ((HtmlMessages)message).getSummaryFormat();
107         }
108         else
109         {
110             return (String JavaDoc)message.getAttributes().get("summaryFormat");
111         }
112     }
113
114     private String JavaDoc getGlobalSummaryFormat(UIComponent message)
115     {
116         if (message instanceof HtmlMessages)
117         {
118             return ((HtmlMessages)message).getGlobalSummaryFormat();
119         }
120         else
121         {
122             return (String JavaDoc)message.getAttributes().get("globalSummaryFormat");
123         }
124     }
125
126     protected String JavaDoc getDetail(FacesContext facesContext,
127                                UIComponent message,
128                                FacesMessage facesMessage,
129                                String JavaDoc msgClientId)
130     {
131         String JavaDoc msgDetail = facesMessage.getDetail();
132         if (msgDetail == null) return null;
133
134         String JavaDoc inputLabel = null;
135         if (msgClientId != null) inputLabel = HtmlMessageRenderer.findInputLabel(facesContext, msgClientId);
136         if (inputLabel == null) inputLabel = "";
137
138         if(((message instanceof HtmlMessages && ((HtmlMessages) message).isReplaceIdWithLabel()) ||
139                 (message instanceof HtmlMessage && ((HtmlMessage) message).isReplaceIdWithLabel()))&&
140                 inputLabel.length()!=0)
141             msgDetail = msgDetail.replaceAll(HtmlMessageRenderer.findInputId(facesContext, msgClientId),inputLabel);
142
143         String JavaDoc detailFormat;
144         if (message instanceof HtmlMessage)
145         {
146             detailFormat = ((HtmlMessage)message).getDetailFormat();
147         }
148         else
149         {
150             detailFormat = (String JavaDoc)message.getAttributes().get("detailFormat");
151         }
152
153         if (detailFormat == null) return msgDetail;
154
155         MessageFormat JavaDoc format = new MessageFormat JavaDoc(detailFormat, facesContext.getViewRoot().getLocale());
156         return format.format(new Object JavaDoc[] {msgDetail, inputLabel});
157     }
158
159
160 }
161
Popular Tags