KickJava   Java API By Example, From Geeks To Geeks.

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


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.UserRoleUtils;
19 import org.apache.myfaces.custom.radio.HtmlRadio;
20 import org.apache.myfaces.renderkit.RendererUtils;
21 import org.apache.myfaces.renderkit.html.HTML;
22 import org.apache.myfaces.renderkit.html.HtmlRadioRendererBase;
23
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26 import javax.faces.component.UISelectOne;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import javax.faces.convert.Converter;
30 import javax.faces.model.SelectItem;
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 /**
36  * @author Manfred Geiler (latest modification by $Author: matze $)
37  * @author Thomas Spiegl
38  * @version $Revision: 1.9 $ $Date: 2004/10/13 11:50:59 $
39  * $Log: HtmlRadioRenderer.java,v $
40  * Revision 1.9 2004/10/13 11:50:59 matze
41  * renamed packages to org.apache
42  *
43  * Revision 1.8 2004/08/13 15:47:08 manolito
44  * No decode for spread checkbox or radio
45  *
46  * Revision 1.7 2004/07/01 21:53:06 mwessendorf
47  * ASF switch
48  *
49  * Revision 1.6 2004/06/04 00:26:16 o_rossmueller
50  * modified renderes to comply with JSF 1.1
51  *
52  * Revision 1.5 2004/05/18 14:31:38 manolito
53  * user role support completely moved to components source tree
54  *
55  * Revision 1.4 2004/04/05 09:11:03 manolito
56  * extended exception messages
57  *
58  * Revision 1.3 2004/04/02 13:57:11 manolito
59  * extended HtmlSelectManyCheckbox with layout "spread" and custom Checkbox component
60  *
61  * Revision 1.2 2004/03/31 15:15:59 royalts
62  * no message
63  *
64  * Revision 1.1 2004/03/31 13:26:08 manolito
65  * extended radio renderer
66  *
67  */

68 public class HtmlRadioRenderer
69         extends HtmlRadioRendererBase
70 {
71     //private static final Log log = LogFactory.getLog(HtmlRadioRenderer.class);
72

73     private static final String JavaDoc LAYOUT_SPREAD = "spread";
74
75     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc
76     {
77         if (context == null) throw new NullPointerException JavaDoc("context");
78         if (component == null) throw new NullPointerException JavaDoc("component");
79
80         if (component instanceof HtmlRadio)
81         {
82             renderRadio(context, (HtmlRadio)component);
83         }
84         else if (component instanceof UISelectOne)
85         {
86             String JavaDoc layout = getLayout(component);
87             if (layout != null && layout.equals(LAYOUT_SPREAD))
88             {
89                 return; //radio inputs are rendered by spread radio components
90
}
91             else
92             {
93                 super.encodeEnd(context, component);
94             }
95         }
96         else
97         {
98             throw new IllegalArgumentException JavaDoc("Unsupported component class " + component.getClass().getName());
99         }
100     }
101
102
103     private void renderRadio(FacesContext facesContext, HtmlRadio radio) throws IOException JavaDoc
104     {
105         String JavaDoc forAttr = radio.getFor();
106         if (forAttr == null)
107         {
108             throw new IllegalStateException JavaDoc("mandatory attribute 'for'");
109         }
110         int index = radio.getIndex();
111         if (index < 0)
112         {
113             throw new IllegalStateException JavaDoc("positive index must be given");
114         }
115
116         UIComponent uiComponent = radio.findComponent(forAttr);
117         if (uiComponent == null)
118         {
119             throw new IllegalStateException JavaDoc("Could not find component '" + forAttr + "' (calling findComponent on component '" + radio.getClientId(facesContext) + "')");
120         }
121         if (!(uiComponent instanceof UISelectOne))
122         {
123             throw new IllegalStateException JavaDoc("UISelectOne expected");
124         }
125
126         UISelectOne uiSelectOne = (UISelectOne)uiComponent;
127         Converter converter;
128         List JavaDoc selectItemList = RendererUtils.getSelectItemList(uiSelectOne);
129         if (index >= selectItemList.size())
130         {
131             throw new IndexOutOfBoundsException JavaDoc("index " + index + " >= " + selectItemList.size());
132         }
133
134         try
135         {
136             converter = RendererUtils.findUIOutputConverter(facesContext, uiSelectOne);
137         }
138         catch (FacesException e)
139         {
140             converter = null;
141         }
142
143         Object JavaDoc currentValue = uiSelectOne.getValue();
144         SelectItem selectItem = (SelectItem)selectItemList.get(index);
145         Object JavaDoc itemValue = selectItem.getValue();
146         String JavaDoc itemStrValue;
147         if (converter == null)
148         {
149             itemStrValue = itemValue.toString();
150         }
151         else
152         {
153             itemStrValue = converter.getAsString(facesContext, uiSelectOne, itemValue);
154         }
155         ResponseWriter writer = facesContext.getResponseWriter();
156
157         writer.startElement(HTML.LABEL_ELEM, uiSelectOne);
158
159         renderRadio(facesContext,
160                     uiSelectOne,
161                     itemStrValue,
162                     selectItem.getLabel(),
163                     currentValue == null && itemValue == null ||
164                     currentValue != null && currentValue.equals(itemValue), false);
165         writer.endElement(HTML.LABEL_ELEM);
166     }
167
168
169     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
170     {
171         if (!UserRoleUtils.isEnabledOnUserRole(uiComponent))
172         {
173             return false;
174         }
175         else
176         {
177             return super.isDisabled(facesContext, uiComponent);
178         }
179     }
180
181
182     public void decode(FacesContext facesContext, UIComponent uiComponent)
183     {
184         if (uiComponent instanceof HtmlRadio)
185         {
186             //nothing to decode
187
}
188         else
189         {
190             super.decode(facesContext, uiComponent);
191         }
192     }
193
194 }
195
Popular Tags