1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared_impl.renderkit.html; |
20 | |
|
21 | |
import org.apache.commons.logging.Log; |
22 | |
import org.apache.commons.logging.LogFactory; |
23 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
24 | |
import org.apache.myfaces.shared_impl.renderkit.RendererUtils; |
25 | |
import org.apache.myfaces.shared_impl.util.NullIterator; |
26 | |
|
27 | |
import javax.faces.application.FacesMessage; |
28 | |
import javax.faces.component.UIComponent; |
29 | |
import javax.faces.component.UIMessages; |
30 | |
import javax.faces.component.html.HtmlMessages; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
import javax.faces.context.ResponseWriter; |
33 | |
import java.io.IOException; |
34 | |
import java.util.Iterator; |
35 | |
import java.util.Map; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public abstract class HtmlMessagesRendererBase |
42 | |
extends HtmlMessageRendererBase |
43 | |
{ |
44 | 0 | private static final Log log = LogFactory.getLog(HtmlMessagesRendererBase.class); |
45 | |
|
46 | |
protected static final String LAYOUT_LIST = "list"; |
47 | |
protected static final String LAYOUT_TABLE = "table"; |
48 | |
|
49 | |
|
50 | |
protected void renderMessages(FacesContext facesContext, |
51 | |
UIComponent messages) |
52 | |
throws IOException |
53 | |
{ |
54 | 0 | MessagesIterator messagesIterator = new MessagesIterator(facesContext, |
55 | |
isGlobalOnly(messages)); |
56 | |
|
57 | 0 | if (messagesIterator.hasNext()) |
58 | |
{ |
59 | 0 | String layout = getLayout(messages); |
60 | 0 | if (layout == null) |
61 | |
{ |
62 | 0 | if (log.isDebugEnabled()) |
63 | |
{ |
64 | 0 | log.debug("No messages layout given, using default layout 'list'."); |
65 | |
} |
66 | 0 | renderList(facesContext, messages, messagesIterator); |
67 | |
} |
68 | 0 | else if (layout.equalsIgnoreCase(LAYOUT_TABLE)) |
69 | |
{ |
70 | 0 | renderTable(facesContext, messages, messagesIterator); |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 0 | if (log.isWarnEnabled() && !layout.equalsIgnoreCase(LAYOUT_LIST)) |
75 | |
{ |
76 | 0 | log.warn("Unsupported messages layout '" + layout + "' - using default layout 'list'."); |
77 | |
} |
78 | 0 | renderList(facesContext, messages, messagesIterator); |
79 | |
} |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
private void renderList(FacesContext facesContext, |
85 | |
UIComponent messages, |
86 | |
MessagesIterator messagesIterator) |
87 | |
throws IOException |
88 | |
{ |
89 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
90 | |
|
91 | 0 | writer.startElement(HTML.UL_ELEM, messages); |
92 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext); |
93 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, messages, HTML.UNIVERSAL_ATTRIBUTES_WITHOUT_STYLE); |
94 | |
|
95 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, getComponentStyle(messages)); |
96 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, getComponentStyleClass(messages)); |
97 | |
|
98 | 0 | while(messagesIterator.hasNext()) |
99 | |
{ |
100 | 0 | writer.startElement(org.apache.myfaces.shared_impl.renderkit.html.HTML.LI_ELEM, messages); |
101 | |
|
102 | 0 | FacesMessage facesMessage = (FacesMessage)messagesIterator.next(); |
103 | |
|
104 | 0 | String[] styleAndClass = getStyleAndStyleClass(messages, facesMessage.getSeverity()); |
105 | 0 | String style = styleAndClass[0]; |
106 | 0 | String styleClass = styleAndClass[1]; |
107 | |
|
108 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style); |
109 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass); |
110 | |
|
111 | 0 | renderSingleFacesMessage(facesContext, |
112 | |
messages, |
113 | |
facesMessage, |
114 | |
messagesIterator.getClientId(),false,false); |
115 | 0 | writer.endElement(HTML.LI_ELEM); |
116 | 0 | } |
117 | |
|
118 | 0 | writer.endElement(HTML.UL_ELEM); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
private void renderTable(FacesContext facesContext, |
123 | |
UIComponent messages, |
124 | |
MessagesIterator messagesIterator) |
125 | |
throws IOException |
126 | |
{ |
127 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
128 | |
|
129 | 0 | writer.startElement(HTML.TABLE_ELEM, messages); |
130 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, messages, facesContext); |
131 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, messages, HTML.UNIVERSAL_ATTRIBUTES_WITHOUT_STYLE); |
132 | |
|
133 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, getComponentStyle(messages)); |
134 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, getComponentStyleClass(messages)); |
135 | |
|
136 | 0 | while(messagesIterator.hasNext()) |
137 | |
{ |
138 | 0 | writer.startElement(HTML.TR_ELEM, messages); |
139 | 0 | writer.startElement(HTML.TD_ELEM, messages); |
140 | |
|
141 | 0 | FacesMessage facesMessage = (FacesMessage)messagesIterator.next(); |
142 | |
|
143 | 0 | String[] styleAndClass = getStyleAndStyleClass(messages, facesMessage.getSeverity()); |
144 | 0 | String style = styleAndClass[0]; |
145 | 0 | String styleClass = styleAndClass[1]; |
146 | |
|
147 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style); |
148 | 0 | HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass); |
149 | |
|
150 | 0 | renderSingleFacesMessage(facesContext, |
151 | |
messages, |
152 | |
facesMessage, |
153 | |
messagesIterator.getClientId(),false,false); |
154 | |
|
155 | 0 | writer.endElement(HTML.TD_ELEM); |
156 | 0 | writer.endElement(HTML.TR_ELEM); |
157 | 0 | } |
158 | |
|
159 | 0 | writer.endElement(HTML.TABLE_ELEM); |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
public static String[] getStyleAndStyleClass(UIComponent messages, |
164 | |
FacesMessage.Severity severity) |
165 | |
{ |
166 | 0 | String style = null; |
167 | 0 | String styleClass = null; |
168 | 0 | if (messages instanceof HtmlMessages) |
169 | |
{ |
170 | 0 | if (severity == FacesMessage.SEVERITY_INFO) |
171 | |
{ |
172 | 0 | style = ((HtmlMessages)messages).getInfoStyle(); |
173 | 0 | styleClass = ((HtmlMessages)messages).getInfoClass(); |
174 | |
} |
175 | 0 | else if (severity == FacesMessage.SEVERITY_WARN) |
176 | |
{ |
177 | 0 | style = ((HtmlMessages)messages).getWarnStyle(); |
178 | 0 | styleClass = ((HtmlMessages)messages).getWarnClass(); |
179 | |
} |
180 | 0 | else if (severity == FacesMessage.SEVERITY_ERROR) |
181 | |
{ |
182 | 0 | style = ((HtmlMessages)messages).getErrorStyle(); |
183 | 0 | styleClass = ((HtmlMessages)messages).getErrorClass(); |
184 | |
} |
185 | 0 | else if (severity == FacesMessage.SEVERITY_FATAL) |
186 | |
{ |
187 | 0 | style = ((HtmlMessages)messages).getFatalStyle(); |
188 | 0 | styleClass = ((HtmlMessages)messages).getFatalClass(); |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | Map attr = messages.getAttributes(); |
204 | 0 | if (severity == FacesMessage.SEVERITY_INFO) |
205 | |
{ |
206 | 0 | style = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.INFO_STYLE_ATTR); |
207 | 0 | styleClass = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.INFO_CLASS_ATTR); |
208 | |
} |
209 | 0 | else if (severity == FacesMessage.SEVERITY_WARN) |
210 | |
{ |
211 | 0 | style = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.WARN_STYLE_ATTR); |
212 | 0 | styleClass = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.WARN_CLASS_ATTR); |
213 | |
} |
214 | 0 | else if (severity == FacesMessage.SEVERITY_ERROR) |
215 | |
{ |
216 | 0 | style = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.ERROR_STYLE_ATTR); |
217 | 0 | styleClass = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.ERROR_CLASS_ATTR); |
218 | |
} |
219 | 0 | else if (severity == FacesMessage.SEVERITY_FATAL) |
220 | |
{ |
221 | 0 | style = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.FATAL_STYLE_ATTR); |
222 | 0 | styleClass = (String)attr.get(JSFAttr.FATAL_CLASS_ATTR); |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
} |
235 | |
|
236 | 0 | return new String[] {style, styleClass}; |
237 | |
} |
238 | |
|
239 | |
protected String getComponentStyleClass(UIComponent messages) |
240 | |
{ |
241 | 0 | String styleClass = null; |
242 | 0 | if (messages instanceof HtmlMessages) |
243 | |
{ |
244 | 0 | styleClass = ((HtmlMessages)messages).getStyleClass(); |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 0 | Map attr = messages.getAttributes(); |
249 | 0 | styleClass = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.STYLE_CLASS_ATTR); |
250 | |
} |
251 | 0 | return styleClass; |
252 | |
} |
253 | |
|
254 | |
protected String getComponentStyle(UIComponent messages) |
255 | |
{ |
256 | 0 | String style = null; |
257 | 0 | if (messages instanceof HtmlMessages) |
258 | |
{ |
259 | 0 | style = ((HtmlMessages)messages).getStyle(); |
260 | |
} |
261 | |
else |
262 | |
{ |
263 | 0 | Map attr = messages.getAttributes(); |
264 | 0 | style = (String)attr.get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.STYLE_ATTR); |
265 | |
} |
266 | 0 | return style; |
267 | |
} |
268 | |
|
269 | |
protected String getTitle(UIComponent component) |
270 | |
{ |
271 | 0 | if (component instanceof HtmlMessages) |
272 | |
{ |
273 | 0 | return ((HtmlMessages)component).getTitle(); |
274 | |
} |
275 | |
else |
276 | |
{ |
277 | 0 | return (String)component.getAttributes().get(org.apache.myfaces.shared_impl.renderkit.JSFAttr.TITLE_ATTR); |
278 | |
} |
279 | |
} |
280 | |
|
281 | |
protected boolean isTooltip(UIComponent component) |
282 | |
{ |
283 | 0 | if (component instanceof HtmlMessages) |
284 | |
{ |
285 | 0 | return ((HtmlMessages)component).isTooltip(); |
286 | |
} |
287 | |
else |
288 | |
{ |
289 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_impl.renderkit.JSFAttr.TOOLTIP_ATTR, false); |
290 | |
} |
291 | |
} |
292 | |
|
293 | |
protected boolean isShowSummary(UIComponent component) |
294 | |
{ |
295 | 0 | if (component instanceof UIMessages) |
296 | |
{ |
297 | 0 | return ((UIMessages)component).isShowSummary(); |
298 | |
} |
299 | |
else |
300 | |
{ |
301 | 0 | return RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_SUMMARY_ATTR, false); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
protected boolean isShowDetail(UIComponent component) |
306 | |
{ |
307 | 0 | if (component instanceof UIMessages) |
308 | |
{ |
309 | 0 | return ((UIMessages)component).isShowDetail(); |
310 | |
} |
311 | |
else |
312 | |
{ |
313 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(component, JSFAttr.SHOW_DETAIL_ATTR, false); |
314 | |
} |
315 | |
} |
316 | |
|
317 | |
protected boolean isGlobalOnly(UIComponent component) |
318 | |
{ |
319 | 0 | if (component instanceof UIMessages) |
320 | |
{ |
321 | 0 | return ((UIMessages)component).isGlobalOnly(); |
322 | |
} |
323 | |
else |
324 | |
{ |
325 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(component, JSFAttr.GLOBAL_ONLY_ATTR, false); |
326 | |
} |
327 | |
} |
328 | |
|
329 | |
protected String getLayout(UIComponent component) |
330 | |
{ |
331 | 0 | if (component instanceof HtmlMessages) |
332 | |
{ |
333 | 0 | return ((HtmlMessages)component).getLayout(); |
334 | |
} |
335 | |
else |
336 | |
{ |
337 | 0 | return (String)component.getAttributes().get(JSFAttr.LAYOUT_ATTR); |
338 | |
} |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | 0 | private static class MessagesIterator implements Iterator |
344 | |
{ |
345 | |
private FacesContext _facesContext; |
346 | |
private Iterator _globalMessagesIterator; |
347 | |
private Iterator _clientIdsWithMessagesIterator; |
348 | 0 | private Iterator _componentMessagesIterator = null; |
349 | 0 | private String _clientId = null; |
350 | |
|
351 | |
public MessagesIterator(FacesContext facesContext, boolean globalOnly) |
352 | 0 | { |
353 | 0 | _facesContext = facesContext; |
354 | 0 | if (globalOnly) |
355 | |
{ |
356 | 0 | _globalMessagesIterator = facesContext.getMessages(null); |
357 | 0 | _clientIdsWithMessagesIterator = NullIterator.instance(); |
358 | |
} |
359 | |
else |
360 | |
{ |
361 | 0 | _globalMessagesIterator = org.apache.myfaces.shared_impl.util.NullIterator.instance(); |
362 | 0 | _clientIdsWithMessagesIterator = facesContext.getClientIdsWithMessages(); |
363 | |
} |
364 | 0 | _componentMessagesIterator = null; |
365 | 0 | _clientId = null; |
366 | 0 | } |
367 | |
|
368 | |
public boolean hasNext() |
369 | |
{ |
370 | 0 | return _globalMessagesIterator.hasNext() || |
371 | |
_clientIdsWithMessagesIterator.hasNext() || |
372 | |
(_componentMessagesIterator != null && _componentMessagesIterator.hasNext()); |
373 | |
} |
374 | |
|
375 | |
public Object next() |
376 | |
{ |
377 | 0 | if (_globalMessagesIterator.hasNext()) |
378 | |
{ |
379 | 0 | return _globalMessagesIterator.next(); |
380 | |
} |
381 | 0 | else if (_componentMessagesIterator != null && _componentMessagesIterator.hasNext()) |
382 | |
{ |
383 | 0 | return _componentMessagesIterator.next(); |
384 | |
} |
385 | |
else |
386 | |
{ |
387 | 0 | _clientId = (String)_clientIdsWithMessagesIterator.next(); |
388 | 0 | _componentMessagesIterator = _facesContext.getMessages(_clientId); |
389 | 0 | return _componentMessagesIterator.next(); |
390 | |
} |
391 | |
} |
392 | |
|
393 | |
public void remove() |
394 | |
{ |
395 | 0 | throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException"); |
396 | |
} |
397 | |
|
398 | |
public String getClientId() |
399 | |
{ |
400 | 0 | return _clientId; |
401 | |
} |
402 | |
} |
403 | |
|
404 | |
} |