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.custom.selectOneCountry;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Set;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  import javax.faces.convert.Converter;
30  
31  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
33  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
34  import org.apache.myfaces.renderkit.html.ext.HtmlMenuRenderer;
35  
36  /**
37   * @JSFRenderer
38   *   renderKitId = "HTML_BASIC"
39   *   family = "javax.faces.SelectOne"
40   *   type = "org.apache.myfaces.SelectOneCountryRenderer"
41   * 
42   * @author Sylvain Vieujot (latest modification by $Author: skitching $)
43   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
44   */
45  public class SelectOneCountryRenderer extends HtmlMenuRenderer {
46      
47      public void encodeEnd(FacesContext facesContext, UIComponent component)
48      throws IOException
49      {
50          RendererUtils.checkParamValidity(facesContext, component, null);
51  
52          SelectOneCountry selectOneCountry = (SelectOneCountry) component;
53          ResponseWriter writer = facesContext.getResponseWriter();
54  
55          if(HtmlRendererUtils.isDisplayValueOnly(component))
56          {
57              //HtmlRendererUtils.renderDisplayValueOnlyForSelects(facesContext, component);
58              writer.startElement(HTML.SPAN_ELEM, selectOneCountry);
59              HtmlRendererUtils.writeIdIfNecessary(writer, selectOneCountry, facesContext);
60  
61              String[] supportedAttributes = {HTML.STYLE_CLASS_ATTR, HTML.STYLE_ATTR};
62              HtmlRendererUtils.renderHTMLAttributes(writer, selectOneCountry, supportedAttributes);
63  
64              String countryCode = selectOneCountry.getValue().toString();
65              String countryName = new Locale(countryCode, countryCode).getDisplayCountry( facesContext.getViewRoot().getLocale() );
66  
67              writer.write( countryName );
68  
69              writer.endElement(HTML.SPAN_ELEM);
70              return;
71          }
72  
73          writer.startElement(HTML.SELECT_ELEM, selectOneCountry);
74          HtmlRendererUtils.writeIdIfNecessary(writer, selectOneCountry, facesContext);
75          writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext), null);
76  
77          List selectItemList = selectOneCountry.getCountriesChoicesAsSelectItemList();
78          Converter converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOneCountry);
79  
80          writer.writeAttribute(HTML.SIZE_ATTR, "1", null);
81  
82          HtmlRendererUtils.renderHTMLAttributes(writer, selectOneCountry, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
83          if ( isDisabled(facesContext, selectOneCountry) ) {
84              writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
85          }
86  
87          Set lookupSet = HtmlRendererUtils.getSubmittedOrSelectedValuesAsSet(false, selectOneCountry, facesContext, converter);
88  
89          HtmlRendererUtils.renderSelectOptions(facesContext, selectOneCountry, converter, lookupSet, selectItemList);
90          // bug #970747: force separate end tag
91          writer.writeText("", null);
92          writer.endElement(HTML.SELECT_ELEM);
93      }
94  }