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 | |
|
26 | |
import javax.faces.component.*; |
27 | |
import javax.faces.component.html.HtmlSelectOneRadio; |
28 | |
import javax.faces.context.FacesContext; |
29 | |
import javax.faces.context.ResponseWriter; |
30 | |
import javax.faces.convert.Converter; |
31 | |
import javax.faces.convert.ConverterException; |
32 | |
import javax.faces.model.SelectItem; |
33 | |
import javax.faces.model.SelectItemGroup; |
34 | |
import java.io.IOException; |
35 | |
import java.util.Iterator; |
36 | |
import java.util.List; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class HtmlRadioRendererBase |
44 | |
extends HtmlRenderer |
45 | |
{ |
46 | 0 | private static final Log log = LogFactory.getLog(HtmlRadioRendererBase.class); |
47 | |
|
48 | |
private static final String PAGE_DIRECTION = "pageDirection"; |
49 | |
private static final String LINE_DIRECTION = "lineDirection"; |
50 | |
|
51 | |
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException |
52 | |
{ |
53 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, UISelectOne.class); |
54 | |
|
55 | 0 | UISelectOne selectOne = (UISelectOne)uiComponent; |
56 | |
|
57 | 0 | String layout = getLayout(selectOne); |
58 | |
|
59 | 0 | boolean pageDirectionLayout = false; |
60 | 0 | if (layout != null) |
61 | |
{ |
62 | 0 | if (layout.equals(PAGE_DIRECTION)) |
63 | |
{ |
64 | 0 | pageDirectionLayout = true; |
65 | |
} |
66 | 0 | else if (layout.equals(LINE_DIRECTION)) |
67 | |
{ |
68 | 0 | pageDirectionLayout = false; |
69 | |
} |
70 | |
else |
71 | |
{ |
72 | 0 | log.error("Wrong layout attribute for component " + selectOne.getClientId(facesContext) + ": " + layout); |
73 | |
} |
74 | |
} |
75 | |
|
76 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
77 | |
|
78 | 0 | writer.startElement(HTML.TABLE_ELEM, selectOne); |
79 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, selectOne, |
80 | |
HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES); |
81 | 0 | HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext); |
82 | |
|
83 | 0 | if (!pageDirectionLayout) writer.startElement(HTML.TR_ELEM, selectOne); |
84 | |
|
85 | |
Converter converter; |
86 | 0 | List selectItemList = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getSelectItemList(selectOne); |
87 | 0 | converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOne); |
88 | |
|
89 | 0 | Object currentValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getObjectValue(selectOne); |
90 | |
|
91 | 0 | currentValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(facesContext, selectOne, converter, currentValue); |
92 | |
|
93 | 0 | int itemNum = 0; |
94 | |
|
95 | 0 | for (Iterator it = selectItemList.iterator(); it.hasNext(); ) |
96 | |
{ |
97 | 0 | SelectItem selectItem = (SelectItem)it.next(); |
98 | |
|
99 | 0 | renderGroupOrItemRadio(facesContext, selectOne, |
100 | |
selectItem, currentValue, |
101 | |
converter, pageDirectionLayout, new Integer(itemNum)); |
102 | |
|
103 | 0 | itemNum++; |
104 | 0 | } |
105 | |
|
106 | 0 | if (!pageDirectionLayout) writer.endElement(HTML.TR_ELEM); |
107 | 0 | writer.endElement(HTML.TABLE_ELEM); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
protected String getLayout(UIComponent selectOne) |
112 | |
{ |
113 | 0 | if (selectOne instanceof HtmlSelectOneRadio) |
114 | |
{ |
115 | 0 | return ((HtmlSelectOneRadio)selectOne).getLayout(); |
116 | |
} |
117 | |
|
118 | 0 | return (String)selectOne.getAttributes().get(JSFAttr.LAYOUT_ATTR); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
protected String getStyleClass(UISelectOne selectOne) |
123 | |
{ |
124 | 0 | if (selectOne instanceof HtmlSelectOneRadio) |
125 | |
{ |
126 | 0 | return ((HtmlSelectOneRadio)selectOne).getStyleClass(); |
127 | |
} |
128 | |
|
129 | 0 | return (String)selectOne.getAttributes().get(JSFAttr.STYLE_CLASS_ATTR); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
protected void renderGroupOrItemRadio(FacesContext facesContext, |
134 | |
UIComponent uiComponent, SelectItem selectItem, |
135 | |
Object currentValue, |
136 | |
Converter converter, boolean pageDirectionLayout, |
137 | |
Integer itemNum) throws IOException{ |
138 | |
|
139 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
140 | |
|
141 | 0 | boolean isSelectItemGroup = (selectItem instanceof SelectItemGroup); |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 0 | UISelectOne selectOne = (UISelectOne)uiComponent; |
148 | |
|
149 | 0 | if (isSelectItemGroup) { |
150 | 0 | if (pageDirectionLayout) |
151 | 0 | writer.startElement(HTML.TR_ELEM, selectOne); |
152 | |
|
153 | 0 | writer.startElement(HTML.TD_ELEM, selectOne); |
154 | 0 | writer.write(selectItem.getLabel()); |
155 | 0 | writer.endElement(HTML.TD_ELEM); |
156 | |
|
157 | 0 | if (pageDirectionLayout) { |
158 | 0 | writer.endElement(HTML.TR_ELEM); |
159 | 0 | writer.startElement(HTML.TR_ELEM, selectOne); |
160 | |
} |
161 | 0 | writer.startElement(HTML.TD_ELEM, selectOne); |
162 | |
|
163 | 0 | writer.startElement(HTML.TABLE_ELEM, selectOne); |
164 | 0 | writer.writeAttribute(HTML.BORDER_ATTR, "0", null); |
165 | |
|
166 | 0 | SelectItemGroup group = (SelectItemGroup) selectItem; |
167 | 0 | SelectItem[] selectItems = group.getSelectItems(); |
168 | |
|
169 | 0 | for (int i=0; i<selectItems.length; i++) { |
170 | 0 | renderGroupOrItemRadio(facesContext, selectOne, selectItems[i], currentValue, converter, |
171 | |
pageDirectionLayout, new Integer(i)); |
172 | |
} |
173 | |
|
174 | 0 | writer.endElement(HTML.TD_ELEM); |
175 | 0 | writer.endElement(HTML.TR_ELEM); |
176 | 0 | writer.endElement(HTML.TABLE_ELEM); |
177 | 0 | writer.endElement(HTML.TD_ELEM); |
178 | |
|
179 | 0 | if (pageDirectionLayout) |
180 | 0 | writer.endElement(HTML.TR_ELEM); |
181 | |
|
182 | 0 | } else { |
183 | |
|
184 | 0 | writer.write("\t\t"); |
185 | 0 | if (pageDirectionLayout) |
186 | 0 | writer.startElement(HTML.TR_ELEM, selectOne); |
187 | 0 | writer.startElement(HTML.TD_ELEM, selectOne); |
188 | |
|
189 | 0 | String itemStrValue = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(facesContext, selectOne, converter, selectItem.getValue()); |
190 | 0 | boolean itemDisabled = selectItem.isDisabled(); |
191 | |
|
192 | 0 | boolean itemChecked = itemStrValue.equals(currentValue); |
193 | |
|
194 | 0 | String itemId = renderRadio(facesContext, selectOne, itemStrValue, itemDisabled, itemChecked, false, itemNum); |
195 | |
|
196 | |
|
197 | 0 | boolean componentDisabled = isDisabled(facesContext, selectOne); |
198 | 0 | boolean disabled = (componentDisabled || itemDisabled); |
199 | |
|
200 | 0 | HtmlRendererUtils.renderLabel(writer, selectOne, itemId, selectItem, disabled); |
201 | |
|
202 | 0 | writer.endElement(HTML.TD_ELEM); |
203 | 0 | if (pageDirectionLayout) |
204 | 0 | writer.endElement(HTML.TR_ELEM); |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | |
@Deprecated |
209 | |
protected void renderRadio(FacesContext facesContext, |
210 | |
UIComponent uiComponent, |
211 | |
String value, |
212 | |
String label, |
213 | |
boolean disabled, |
214 | |
boolean checked, boolean renderId) |
215 | |
throws IOException |
216 | |
{ |
217 | 0 | renderRadio(facesContext, (UIInput) uiComponent, value, disabled, checked, renderId, 0); |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
protected String renderRadio(FacesContext facesContext, |
225 | |
UIInput uiComponent, |
226 | |
String value, |
227 | |
boolean disabled, |
228 | |
boolean checked, |
229 | |
boolean renderId, |
230 | |
Integer itemNum) |
231 | |
throws IOException |
232 | |
{ |
233 | 0 | String clientId = uiComponent.getClientId(facesContext); |
234 | |
|
235 | 0 | String itemId = (itemNum == null)? null : clientId + NamingContainer.SEPARATOR_CHAR + itemNum; |
236 | |
|
237 | 0 | ResponseWriter writer = facesContext.getResponseWriter(); |
238 | |
|
239 | 0 | writer.startElement(HTML.INPUT_ELEM, uiComponent); |
240 | |
|
241 | 0 | if (itemId != null) |
242 | |
{ |
243 | 0 | writer.writeAttribute(HTML.ID_ATTR, itemId, null); |
244 | |
} |
245 | 0 | writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null); |
246 | 0 | writer.writeAttribute(HTML.NAME_ATTR, clientId, null); |
247 | |
|
248 | 0 | if (disabled) { |
249 | 0 | writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null); |
250 | |
} |
251 | |
|
252 | 0 | if (renderId) { |
253 | 0 | writer.writeAttribute(HTML.ID_ATTR, clientId, null); |
254 | |
} |
255 | |
|
256 | 0 | if (checked) |
257 | |
{ |
258 | 0 | writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null); |
259 | |
} |
260 | |
|
261 | 0 | if (value != null) |
262 | |
{ |
263 | 0 | writer.writeAttribute(HTML.VALUE_ATTR, value, null); |
264 | |
} |
265 | |
|
266 | 0 | HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_STYLE); |
267 | 0 | if (isDisabled(facesContext, uiComponent)) |
268 | |
{ |
269 | 0 | writer.writeAttribute(org.apache.myfaces.shared_impl.renderkit.html.HTML.DISABLED_ATTR, Boolean.TRUE, null); |
270 | |
} |
271 | |
|
272 | 0 | writer.endElement(HTML.INPUT_ELEM); |
273 | |
|
274 | 0 | return itemId; |
275 | |
} |
276 | |
|
277 | |
|
278 | |
protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) |
279 | |
{ |
280 | |
|
281 | 0 | if (uiComponent instanceof HtmlSelectOneRadio) |
282 | |
{ |
283 | 0 | return ((HtmlSelectOneRadio)uiComponent).isDisabled(); |
284 | |
} |
285 | |
|
286 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
public void decode(FacesContext facesContext, UIComponent uiComponent) |
291 | |
{ |
292 | 0 | org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null); |
293 | 0 | if (uiComponent instanceof UIInput) |
294 | |
{ |
295 | 0 | HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent); |
296 | |
} |
297 | 0 | } |
298 | |
|
299 | |
|
300 | |
public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException |
301 | |
{ |
302 | 0 | RendererUtils.checkParamValidity(facesContext, uiComponent, UISelectOne.class); |
303 | 0 | return org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedUISelectOneValue(facesContext, |
304 | |
(UISelectOne)uiComponent, |
305 | |
submittedValue); |
306 | |
} |
307 | |
|
308 | |
} |