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.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Set;
27  import java.util.TreeMap;
28  
29  import javax.faces.component.UISelectItems;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  import javax.faces.model.SelectItem;
33  
34  import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu;
35  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
36  
37  /**
38   * A localized list of countries choose box. The value binds to the 
39   * country ISO 3166 code. This is the same code as for 
40   * java.util.Locale.getCountry(). The official codes list is 
41   * available here : 
42   * 
43   * http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html 
44   * 
45   * Unless otherwise specified, all attributes accept static values or EL expressions.
46   * 
47   * @JSFComponent
48   *   name = "t:selectOneCountry"
49   *   class = "org.apache.myfaces.custom.selectOneCountry.SelectOneCountry"
50   *   tagClass = "org.apache.myfaces.custom.selectOneCountry.SelectOneCountryTag"
51   * @since 1.1.7
52   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
53   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
54   */
55  public abstract class AbstractSelectOneCountry extends HtmlSelectOneMenu {
56      public static final String COMPONENT_TYPE = "org.apache.myfaces.SelectOneCountry";
57      private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SelectOneCountryRenderer";
58  
59      public AbstractSelectOneCountry() {
60          setRendererType(DEFAULT_RENDERER_TYPE);
61      }
62  
63      /**
64       * Integer equals to the maximum number of characters in the country name.
65       * 
66       * @JSFProperty
67       */
68      public abstract Integer getMaxLength();
69      
70      /**
71       * Integer equals to the maximum number of characters in the country name.
72       * 
73       * @JSFProperty
74       */
75      public abstract String getEmptySelection();
76  
77      private Set getFilterSet(){
78          List selectItems = RendererUtils.getSelectItemList( this );
79          Set set = new HashSet( selectItems.size() );
80  
81          for (Iterator i = selectItems.iterator(); i.hasNext(); )
82              set.add( ((SelectItem)i.next()).getValue().toString().toUpperCase() );
83  
84          return set;
85      }
86  
87      protected List getCountriesChoicesAsSelectItemList(){
88          //return RendererUtils.getSelectItemList(component);
89  
90          Set filterSet = getFilterSet();
91  
92          String[] availableCountries = Locale.getISOCountries();
93  
94          Locale currentLocale;
95  
96          FacesContext facesContext = FacesContext.getCurrentInstance();
97          UIViewRoot viewRoot = facesContext.getViewRoot();
98          if( viewRoot != null )
99              currentLocale = viewRoot.getLocale();
100         else
101             currentLocale = facesContext.getApplication().getDefaultLocale();
102 
103 
104         TreeMap map = new TreeMap();
105         // TreeMap is sorted according to the keys' natural order
106 
107         for(int i=0; i<availableCountries.length; i++){
108             String countryCode = availableCountries[i];
109             if( ! filterSet.isEmpty() && ! filterSet.contains(countryCode))
110                 continue;
111             Locale tmp = new Locale(countryCode, countryCode);
112             map.put(tmp.getDisplayCountry(currentLocale), countryCode);
113         }
114 
115         List countriesSelectItems = new ArrayList( map.size() );
116         if(getEmptySelection() != null)
117             countriesSelectItems.add(new SelectItem("", getEmptySelection()));
118 
119         Integer maxLength = getMaxLength();
120         int maxDescriptionLength = maxLength==null ? Integer.MAX_VALUE : maxLength.intValue();
121         if( maxDescriptionLength < 5 )
122             maxDescriptionLength = 5;
123 
124         for(Iterator i = map.keySet().iterator(); i.hasNext(); ){
125             String countryName = (String) i.next();
126             String countryCode = (String) map.get( countryName );
127             String label;
128             if( countryName.length() <= maxDescriptionLength )
129                 label = countryName;
130             else
131                 label = countryName.substring(0, maxDescriptionLength-3)+"...";
132 
133             countriesSelectItems.add( new SelectItem(countryCode, label) );
134         }
135 
136         return countriesSelectItems;
137     }
138 
139     protected void validateValue(FacesContext context, Object value) {
140         UISelectItems selectItems = new UISelectItems();
141         selectItems.setTransient(true);
142         selectItems.setValue(getCountriesChoicesAsSelectItemList());
143         getChildren().add(selectItems);
144         
145         super.validateValue(context,value);
146     }
147 }