1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.renderkit.html; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
|
23 | |
import javax.faces.component.UIComponent; |
24 | |
import javax.faces.component.ValueHolder; |
25 | |
import javax.faces.component.html.HtmlOutputLabel; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.context.ResponseWriter; |
28 | |
|
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer; |
32 | |
import org.apache.myfaces.shared_impl.renderkit.JSFAttr; |
33 | |
import org.apache.myfaces.shared_impl.renderkit.RendererUtils; |
34 | |
import org.apache.myfaces.shared_impl.renderkit.html.HTML; |
35 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer; |
36 | |
import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@JSFRenderer( |
47 | |
renderKitId="HTML_BASIC", |
48 | |
family="javax.faces.Output", |
49 | |
type="javax.faces.Label") |
50 | 0 | public class HtmlLabelRenderer |
51 | |
extends HtmlRenderer |
52 | |
{ |
53 | 0 | private static final Log log = LogFactory.getLog(HtmlLabelRenderer.class); |
54 | |
|
55 | |
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) |
56 | |
throws IOException |
57 | |
{ |
58 | 0 | super.encodeBegin(facesContext, uiComponent); |
59 | |
|
60 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
61 | |
|
62 | 0 | encodeBefore(facesContext, writer, uiComponent); |
63 | |
|
64 | 0 | writer.startElement(HTML.LABEL_ELEM, uiComponent); |
65 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); |
66 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.LABEL_PASSTHROUGH_ATTRIBUTES); |
67 | |
|
68 | 0 | String forAttr = getFor(uiComponent); |
69 | |
|
70 | 0 | if (forAttr != null) |
71 | |
{ |
72 | 0 | writer.writeAttribute(HTML.FOR_ATTR, |
73 | |
getClientId(facesContext, uiComponent, forAttr), JSFAttr.FOR_ATTR); |
74 | |
} |
75 | |
else |
76 | |
{ |
77 | 0 | if (log.isWarnEnabled()) { |
78 | 0 | log.warn("Attribute 'for' of label component with id " + uiComponent.getClientId(facesContext)+" is not defined"); |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | 0 | if (uiComponent instanceof ValueHolder) |
86 | |
{ |
87 | 0 | String text = RendererUtils.getStringValue(facesContext, uiComponent); |
88 | 0 | if(text != null) |
89 | |
{ |
90 | 0 | writer.writeText(text, "value"); |
91 | |
} |
92 | |
} |
93 | |
|
94 | 0 | writer.flush(); |
95 | |
|
96 | 0 | encodeAfterStart(facesContext,writer,uiComponent); |
97 | 0 | } |
98 | |
|
99 | |
protected void encodeAfterStart(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) |
100 | |
throws IOException |
101 | |
{ |
102 | 0 | } |
103 | |
|
104 | |
protected void encodeBefore(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) |
105 | |
throws IOException |
106 | |
{ |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
protected String getFor(UIComponent component) |
111 | |
{ |
112 | 0 | if (component instanceof HtmlOutputLabel) |
113 | |
{ |
114 | 0 | return ((HtmlOutputLabel)component).getFor(); |
115 | |
} |
116 | |
|
117 | 0 | return (String) component.getAttributes().get(JSFAttr.FOR_ATTR); |
118 | |
|
119 | |
} |
120 | |
|
121 | |
protected String getClientId(FacesContext facesContext, |
122 | |
UIComponent uiComponent, String forAttr) |
123 | |
{ |
124 | 0 | return RendererUtils.getClientId(facesContext, uiComponent, forAttr); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) |
129 | |
throws IOException |
130 | |
{ |
131 | 0 | super.encodeEnd(facesContext, uiComponent); |
132 | |
|
133 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
134 | |
|
135 | 0 | encodeBeforeEnd(facesContext, writer, uiComponent); |
136 | |
|
137 | 0 | writer.endElement(HTML.LABEL_ELEM); |
138 | |
|
139 | 0 | encodeAfter(facesContext, writer, uiComponent); |
140 | 0 | } |
141 | |
|
142 | |
protected void encodeBeforeEnd(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) |
143 | |
throws IOException |
144 | |
{ |
145 | 0 | } |
146 | |
|
147 | |
protected void encodeAfter(FacesContext facesContext, ResponseWriter writer, UIComponent uiComponent) |
148 | |
throws IOException |
149 | |
{ |
150 | 0 | } |
151 | |
} |