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  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectMany;
27  import javax.faces.component.UISelectOne;
28  import javax.faces.component.behavior.ClientBehavior;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.component.html.HtmlSelectManyListbox;
31  import javax.faces.component.html.HtmlSelectOneListbox;
32  import javax.faces.context.FacesContext;
33  import javax.faces.convert.Converter;
34  import javax.faces.convert.ConverterException;
35  import java.io.IOException;
36  import java.util.List;
37  import java.util.Map;
38  
39  
40  public class HtmlListboxRendererBase
41          extends HtmlSelectableRendererBase
42  {
43      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
44              throws IOException
45      {
46          org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
47          
48          Map<String, List<ClientBehavior>> behaviors = null;
49          if (uiComponent instanceof ClientBehaviorHolder)
50          {
51              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
52              if (!behaviors.isEmpty())
53              {
54                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(
55                          facesContext, facesContext.getResponseWriter());
56              }
57          }
58  
59          Integer size = (Integer)uiComponent.getAttributes().get(JSFAttr.SIZE_ATTR);
60          if (size == null)
61          {
62              size = Integer.MIN_VALUE;
63          }
64  
65          if (uiComponent instanceof UISelectMany)
66          {
67              renderListbox(facesContext,
68                                              (UISelectMany)uiComponent,
69                                              isDisabled(facesContext, uiComponent),
70                                              size, getConverter(facesContext, uiComponent));
71          }
72          else if (uiComponent instanceof HtmlSelectOneListbox)
73          {
74              renderListbox(facesContext,
75                                              (UISelectOne)uiComponent,
76                                              isDisabled(facesContext, uiComponent),
77                                              size, getConverter(facesContext, uiComponent));
78          }
79          else
80          {
81              throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
82          }
83      }
84      
85      protected void renderListbox(FacesContext facesContext,
86              UISelectOne selectOne, boolean disabled, int size,
87              Converter converter) throws IOException
88      {
89          internalRenderSelect(facesContext, selectOne, disabled, size, false,
90                  converter);
91      }
92  
93      protected void renderListbox(FacesContext facesContext,
94              UISelectMany selectMany, boolean disabled, int size,
95              Converter converter) throws IOException
96      {
97          internalRenderSelect(facesContext, selectMany, disabled, size, true,
98                  converter);
99      }
100 
101     protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent)
102     {
103         //TODO: overwrite in extended HtmlListboxRenderer and check for enabledOnUserRole
104         if (uiComponent instanceof HtmlSelectManyListbox)
105         {
106             return ((HtmlSelectManyListbox)uiComponent).isDisabled();
107         }
108         else if (uiComponent instanceof HtmlSelectOneListbox)
109         {
110             return ((HtmlSelectOneListbox)uiComponent).isDisabled();
111         }
112         else
113         {
114             return org.apache.myfaces.shared.renderkit.RendererUtils.getBooleanAttribute(uiComponent, 
115                     org.apache.myfaces.shared.renderkit.html.HTML.DISABLED_ATTR, false);
116         }
117     }
118 
119 
120     public void decode(FacesContext facesContext, UIComponent uiComponent)
121     {
122         RendererUtils.checkParamValidity(facesContext, uiComponent, null);
123 
124         if (uiComponent instanceof UISelectMany)
125         {
126             HtmlRendererUtils.decodeUISelectMany(facesContext, uiComponent);
127         }
128         else if (uiComponent instanceof UISelectOne)
129         {
130             HtmlRendererUtils.decodeUISelectOne(facesContext, uiComponent);
131         }
132         else
133         {
134             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
135         }
136         if (uiComponent instanceof ClientBehaviorHolder &&
137                 !HtmlRendererUtils.isDisabled(uiComponent))
138         {
139             HtmlRendererUtils.decodeClientBehaviors(facesContext, uiComponent);
140         }
141     }
142 
143     public Object getConvertedValue(FacesContext facesContext, UIComponent uiComponent, Object submittedValue)
144          throws ConverterException
145     {
146         org.apache.myfaces.shared.renderkit.RendererUtils.checkParamValidity(facesContext, uiComponent, null);
147 
148         if (uiComponent instanceof UISelectMany)
149         {
150             return org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectManyValue(facesContext,
151                                                                (UISelectMany)uiComponent,
152                                                                submittedValue);
153         }
154         else if (uiComponent instanceof UISelectOne)
155         {
156             return RendererUtils.getConvertedUISelectOneValue(facesContext,
157                                                            (UISelectOne)uiComponent,
158                                                            submittedValue);
159         }
160         else
161         {
162             throw new IllegalArgumentException("Unsupported component class " + uiComponent.getClass().getName());
163         }
164     }
165     
166     /**
167      * Gets the converter for the given component rendered by this renderer.
168      * @param facesContext
169      * @param component
170      * @return
171      */
172     protected Converter getConverter(FacesContext facesContext,
173             UIComponent component)
174     {
175         if (component instanceof UISelectMany)
176         {
177             return HtmlRendererUtils.findUISelectManyConverterFailsafe(facesContext, 
178                     (UISelectMany) component);
179         }
180         else if (component instanceof UISelectOne)
181         {
182             return HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, component);
183         }
184         return null;
185     }
186 
187 }