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