View Javadoc

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 org.apache.myfaces.renderkit.JSFAttr;
19  import org.apache.myfaces.renderkit.RendererUtils;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.component.UISelectMany;
23  import javax.faces.component.UISelectOne;
24  import javax.faces.component.html.HtmlSelectManyListbox;
25  import javax.faces.component.html.HtmlSelectOneListbox;
26  import javax.faces.context.FacesContext;
27  import javax.faces.convert.ConverterException;
28  import java.io.IOException;
29  
30  
31  /***
32   * @author Thomas Spiegl (latest modification by $Author: skitching $)
33   * @author Anton Koinov
34   * @version $Revision: 349370 $ $Date: 2005-11-28 05:01:41 +0000 (Mon, 28 Nov 2005) $
35   */
36  public class HtmlListboxRendererBase
37          extends HtmlRenderer
38  {
39      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
40              throws IOException
41      {
42          RendererUtils.checkParamValidity(facesContext, uiComponent, null);
43  
44          if (uiComponent instanceof UISelectMany)
45          {
46              int size;
47              if (uiComponent instanceof HtmlSelectManyListbox)
48              {
49                  size = ((HtmlSelectManyListbox)uiComponent).getSize();
50              }
51              else
52              {
53                  Integer i = (Integer)uiComponent.getAttributes().get(JSFAttr.SIZE_ATTR);
54                  size = i != null ? i.intValue() : 0;
55              }
56              HtmlRendererUtils.renderListbox(facesContext,
57                                              (UISelectMany)uiComponent,
58                                              isDisabled(facesContext, uiComponent),
59                                              size);
60          }
61          else if (uiComponent instanceof HtmlSelectOneListbox)
62          {
63              int size;
64              if (uiComponent instanceof HtmlSelectOneListbox)
65              {
66                  size = ((HtmlSelectOneListbox)uiComponent).getSize();
67              }
68              else
69              {
70                  Integer i = (Integer)uiComponent.getAttributes().get(JSFAttr.SIZE_ATTR);
71                  size = i != null ? i.intValue() : 0;
72              }
73              HtmlRendererUtils.renderListbox(facesContext,
74                                              (UISelectOne)uiComponent,
75                                              isDisabled(facesContext, uiComponent),
76                                              size);
77          }
78          else
79          {
80              throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
81          }
82      }
83  
84  
85      protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
86      {
87          //TODO: overwrite in extended HtmlListboxRenderer and check for enabledOnUserRole
88          if (uiComponent instanceof HtmlSelectManyListbox)
89          {
90              return ((HtmlSelectManyListbox)uiComponent).isDisabled();
91          }
92          else if (uiComponent instanceof HtmlSelectOneListbox)
93          {
94              return ((HtmlSelectOneListbox)uiComponent).isDisabled();
95          }
96          else
97          {
98              return RendererUtils.getBooleanAttribute(uiComponent, HTML.DISABLED_ATTR, false);
99          }
100     }
101 
102 
103     public void decode(FacesContext facesContext, UIComponent uiComponent)
104     {
105         RendererUtils.checkParamValidity(facesContext, uiComponent, null);
106 
107         if (uiComponent instanceof UISelectMany)
108         {
109             HtmlRendererUtils.decodeUISelectMany(facesContext, uiComponent);
110         }
111         else if (uiComponent instanceof UISelectOne)
112         {
113             HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent);
114         }
115         else
116         {
117             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
118         }
119     }
120 
121     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue) throws ConverterException
122     {
123         RendererUtils.checkParamValidity(facesContext, uiComponent, null);
124 
125         if (uiComponent instanceof UISelectMany)
126         {
127             return RendererUtils.getConvertedUISelectManyValue(facesContext,
128                                                                (UISelectMany)uiComponent,
129                                                                submittedValue);
130         }
131         else if (uiComponent instanceof UISelectOne)
132         {
133             return RendererUtils.getConvertedUIOutputValue(facesContext,
134                                                            (UISelectOne)uiComponent,
135                                                            submittedValue);
136         }
137         else
138         {
139             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
140         }
141     }
142 
143 }