KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlRadioRendererBase


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;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.faces.FacesException;
23 import javax.faces.component.UIComponent;
24 import javax.faces.component.UIInput;
25 import javax.faces.component.UIOutput;
26 import javax.faces.component.UISelectOne;
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
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.myfaces.renderkit.JSFAttr;
37 import org.apache.myfaces.renderkit.RendererUtils;
38
39 /**
40  * @author Manfred Geiler (latest modification by $Author: svieujot $)
41  * @author Thomas Spiegl
42  * @version $Revision: 1.15 $ $Date: 2005/02/08 17:57:23 $
43  * $Log: HtmlRadioRendererBase.java,v $
44  * Revision 1.15 2005/02/08 17:57:23 svieujot
45  * Close MYFACES-82 (Use submitted values).
46  * Also, use more RendererUtil methods.
47  *
48  * Revision 1.14 2005/01/22 16:47:17 mmarinschek
49  * fixing bug with validation not called if the submitted value is empty; an empty string is submitted instead if the component is enabled.
50  *
51  * Revision 1.13 2005/01/19 13:34:12 mmarinschek
52  * handle case that converter is null
53  *
54  * Revision 1.12 2005/01/19 13:18:04 mmarinschek
55  * better logging of component information
56  *
57  * Revision 1.11 2005/01/18 22:43:05 svieujot
58  * Fix some bugs where converter wasn't used to determine selected values.
59  * This caused for examples the list, checkbox and radio based components to bug when the backing bean value type is a primitive.
60  *
61  * Revision 1.10 2005/01/18 20:49:48 svieujot
62  * Set default layout to lineDirection, according to Spec.
63  *
64  * Revision 1.9 2004/12/23 13:03:09 mmarinschek
65  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
66  *
67  * Revision 1.8 2004/10/13 11:51:01 matze
68  * renamed packages to org.apache
69  *
70  * Revision 1.7 2004/10/05 08:49:14 manolito
71  * #1038697 h:selectOneRadio generates malformed XHTML
72  *
73  * Revision 1.6 2004/07/01 22:00:57 mwessendorf
74  * ASF switch
75  *
76  * Revision 1.5 2004/06/04 00:26:16 o_rossmueller
77  * modified renderes to comply with JSF 1.1
78  *
79  * Revision 1.4 2004/05/26 11:10:12 o_rossmueller
80  * fix #959926: styleClass support for selectOneRadio, selectOneList, selectManyList
81  *
82  * Revision 1.3 2004/05/18 14:31:39 manolito
83  * user role support completely moved to components source tree
84  *
85  * Revision 1.2 2004/03/31 15:15:58 royalts
86  * no message
87  *
88  * Revision 1.1 2004/03/31 13:26:09 manolito
89  * extended radio renderer
90  *
91  */

92 public class HtmlRadioRendererBase
93         extends HtmlRenderer
94 {
95     private static final Log log = LogFactory.getLog(HtmlRadioRendererBase.class);
96
97     private static final String JavaDoc PAGE_DIRECTION = "pageDirection";
98     private static final String JavaDoc LINE_DIRECTION = "lineDirection";
99
100     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException JavaDoc
101     {
102         RendererUtils.checkParamValidity(facesContext, uiComponent, UISelectOne.class);
103
104         UISelectOne selectOne = (UISelectOne)uiComponent;
105
106         String JavaDoc layout = getLayout(selectOne);
107
108         boolean pageDirectionLayout = false; // Defaults to LINE_DIRECTION
109
if (layout != null)
110         {
111             if (layout.equals(PAGE_DIRECTION))
112             {
113                 pageDirectionLayout = true;
114             }
115             else if (layout.equals(LINE_DIRECTION))
116             {
117                 pageDirectionLayout = false;
118             }
119             else
120             {
121                 log.error("Wrong layout attribute for component " + selectOne.getClientId(facesContext) + ": " + layout);
122             }
123         }
124
125         ResponseWriter writer = facesContext.getResponseWriter();
126
127         writer.startElement(HTML.TABLE_ELEM, selectOne);
128         HtmlRendererUtils.renderHTMLAttributes(writer, selectOne,
129                                                HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES);
130         HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
131
132         if (!pageDirectionLayout) writer.startElement(HTML.TR_ELEM, selectOne);
133
134         Converter converter;
135         List JavaDoc selectItemList = RendererUtils.getSelectItemList(selectOne);
136         try
137         {
138             converter = RendererUtils.findUIOutputConverter(facesContext, selectOne);
139         }
140         catch (FacesException e)
141         {
142             log.error("Error finding Converter for component with id " + uiComponent.getClientId(facesContext));
143             converter = null;
144         }
145
146         String JavaDoc currentValueStr = RendererUtils.getStringValue(facesContext, selectOne);
147
148         for (Iterator JavaDoc it = selectItemList.iterator(); it.hasNext(); )
149         {
150             SelectItem selectItem = (SelectItem)it.next();
151             String JavaDoc itemStrValue = RendererUtils.getConvertedStringValue(facesContext, selectOne, converter, selectItem.getValue());
152
153             writer.write("\t\t");
154             if (pageDirectionLayout) writer.startElement(HTML.TR_ELEM, selectOne);
155             writer.startElement(HTML.TD_ELEM, selectOne);
156             writer.startElement(HTML.LABEL_ELEM, selectOne);
157             renderRadio(facesContext,
158                         selectOne,
159                         itemStrValue,
160                         selectItem.getLabel(),
161                         currentValueStr.equals(itemStrValue),
162                         false);
163             writer.endElement(HTML.LABEL_ELEM);
164             writer.endElement(HTML.TD_ELEM);
165             if (pageDirectionLayout) writer.endElement(HTML.TR_ELEM);
166         }
167
168         if (!pageDirectionLayout) writer.endElement(HTML.TR_ELEM);
169         writer.endElement(HTML.TABLE_ELEM);
170     }
171
172
173     protected String JavaDoc getLayout(UIComponent selectOne)
174     {
175         if (selectOne instanceof HtmlSelectOneRadio)
176         {
177             return ((HtmlSelectOneRadio)selectOne).getLayout();
178         }
179
180         return (String JavaDoc)selectOne.getAttributes().get(JSFAttr.LAYOUT_ATTR);
181     }
182
183
184     protected String JavaDoc getStyleClass(UISelectOne selectOne)
185      {
186          if (selectOne instanceof HtmlSelectOneRadio)
187          {
188              return ((HtmlSelectOneRadio)selectOne).getStyleClass();
189          }
190
191          return (String JavaDoc)selectOne.getAttributes().get(JSFAttr.STYLE_CLASS_ATTR);
192      }
193
194
195     protected void renderRadio(FacesContext facesContext,
196                                UIInput uiComponent,
197                                String JavaDoc value,
198                                String JavaDoc label,
199                                boolean checked, boolean renderId)
200             throws IOException JavaDoc
201     {
202         String JavaDoc clientId = uiComponent.getClientId(facesContext);
203
204         ResponseWriter writer = facesContext.getResponseWriter();
205
206         writer.startElement(HTML.INPUT_ELEM, uiComponent);
207         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null);
208         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
209
210         if (renderId) {
211             writer.writeAttribute(HTML.ID_ATTR, clientId, null);
212         }
213
214         if (checked)
215         {
216             writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null);
217         }
218
219         if ((value != null) && (value.length() > 0))
220         {
221             writer.writeAttribute(HTML.VALUE_ATTR, value, null);
222         }
223
224         HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
225         if (isDisabled(facesContext, uiComponent))
226         {
227             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
228         }
229
230         writer.endElement(HTML.INPUT_ELEM);
231
232         if ((label != null) && (label.length() > 0))
233         {
234             writer.write(HTML.NBSP_ENTITY);
235             writer.writeText(label, null);
236         }
237     }
238
239
240     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
241     {
242         //TODO: overwrite in extended HtmlRadioRenderer and check for enabledOnUserRole
243
if (uiComponent instanceof HtmlSelectOneRadio)
244         {
245             return ((HtmlSelectOneRadio)uiComponent).isDisabled();
246         }
247
248         return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
249     }
250
251
252     public void decode(FacesContext facesContext, UIComponent uiComponent)
253     {
254         RendererUtils.checkParamValidity(facesContext, uiComponent, null);
255         if (uiComponent instanceof UIInput)
256         {
257             HtmlRendererUtils.decodeUIInput(facesContext, uiComponent);
258         }
259     }
260
261
262     public Object JavaDoc getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc submittedValue) throws ConverterException
263     {
264         RendererUtils.checkParamValidity(facesContext, uiComponent, UIOutput.class);
265         return RendererUtils.getConvertedUIOutputValue(facesContext,
266                                                        (UIOutput)uiComponent,
267                                                        submittedValue);
268     }
269
270 }
271
Popular Tags