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