View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.apache.myfaces.shared.renderkit.html;
20  
21  import org.apache.myfaces.shared.renderkit.JSFAttr;
22  import org.apache.myfaces.shared.renderkit.RendererUtils;
23  
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UISelectMany;
26  import javax.faces.component.UISelectOne;
27  import javax.faces.component.html.HtmlSelectManyListbox;
28  import javax.faces.component.html.HtmlSelectOneListbox;
29  import javax.faces.context.FacesContext;
30  import javax.faces.convert.ConverterException;
31  import java.io.IOException;
32  
33  
34  /***
35   * @author Thomas Spiegl (latest modification by $Author: matzew $)
36   * @author Anton Koinov
37   * @version $Revision: 597729 $ $Date: 2007-11-23 14:25:55 -0500 (Fri, 23 Nov 2007) $
38   */
39  public class HtmlListboxRendererBase
40          extends HtmlRenderer
41  {
42      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
43              throws IOException
44      {
45          org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
46  
47          int size = (Integer)uiComponent.getAttributes().get(JSFAttr.SIZE_ATTR);
48  
49          if (uiComponent instanceof UISelectMany)
50          {
51              HtmlRendererUtils.renderListbox(facesContext,
52                                              (UISelectMany)uiComponent,
53                                              isDisabled(facesContext, uiComponent),
54                                              size);
55          }
56          else if (uiComponent instanceof HtmlSelectOneListbox)
57          {
58              HtmlRendererUtils.renderListbox(facesContext,
59                                              (UISelectOne)uiComponent,
60                                              isDisabled(facesContext, uiComponent),
61                                              size);
62          }
63          else
64          {
65              throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
66          }
67      }
68  
69      protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
70      {
71          //TODO: overwrite in extended HtmlListboxRenderer and check for enabledOnUserRole
72          if (uiComponent instanceof HtmlSelectManyListbox)
73          {
74              return ((HtmlSelectManyListbox)uiComponent).isDisabled();
75          }
76          else if (uiComponent instanceof HtmlSelectOneListbox)
77          {
78              return ((HtmlSelectOneListbox)uiComponent).isDisabled();
79          }
80          else
81          {
82              return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(uiComponent, org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, false);
83          }
84      }
85  
86  
87      public void decode(FacesContext facesContext, UIComponent uiComponent)
88      {
89          RendererUtils.checkParamValidity(facesContext, uiComponent, null);
90  
91          if (uiComponent instanceof UISelectMany)
92          {
93              HtmlRendererUtils.decodeUISelectMany(facesContext, uiComponent);
94          }
95          else if (uiComponent instanceof UISelectOne)
96          {
97              HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent);
98          }
99          else
100         {
101             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
102         }
103     }
104 
105     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException
106     {
107         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
108 
109         if (uiComponent instanceof UISelectMany)
110         {
111             return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectManyValue(facesContext,
112                                                                (UISelectMany)uiComponent,
113                                                                submittedValue);
114         }
115         else if (uiComponent instanceof UISelectOne)
116         {
117             return RendererUtils.getConvertedUISelectOneValue(facesContext,
118                                                            (UISelectOne)uiComponent,
119                                                            submittedValue);
120         }
121         else
122         {
123             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
124         }
125     }
126 
127 }