1 16 package org.apache.myfaces.custom.selectOneCountry; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.TreeMap ; 23 24 import javax.faces.component.UISelectItem; 25 import javax.faces.component.UIViewRoot; 26 import javax.faces.context.FacesContext; 27 import javax.faces.el.ValueBinding; 28 import javax.faces.model.SelectItem; 29 30 import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu; 31 32 40 public class SelectOneCountry extends HtmlSelectOneMenu { 41 public static final String COMPONENT_TYPE = "org.apache.myfaces.SelectOneCountry"; 42 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SelectOneCountryRenderer"; 43 44 private Integer _maxLength = null; 45 46 public SelectOneCountry() { 47 setRendererType(DEFAULT_RENDERER_TYPE); 48 } 49 50 public Integer getMaxLength() { 51 if (_maxLength != null) return _maxLength; 52 ValueBinding vb = getValueBinding("length"); 53 return vb != null ? (Integer )vb.getValue(getFacesContext()) : null; 54 } 55 public void setMaxLength(Integer maxLength) { 56 _maxLength = maxLength; 57 } 58 59 public Object saveState(FacesContext context) { 60 Object values[] = new Object [2]; 61 values[0] = super.saveState(context); 62 values[1] = _maxLength; 63 return values; 64 } 65 66 public void restoreState(FacesContext context, Object state) { 67 Object values[] = (Object [])state; 68 super.restoreState(context, values[0]); 69 _maxLength = (Integer )values[1]; 70 } 71 72 74 public void encodeChildren(FacesContext context){ 75 } 77 78 public int getChildCount(){ 79 return Locale.getISOCountries().length; 80 } 81 82 public List getChildren(){ 83 String [] availableCountries = Locale.getISOCountries(); 84 85 Locale currentLocale; 86 87 FacesContext facesContext = FacesContext.getCurrentInstance(); 88 UIViewRoot viewRoot = facesContext.getViewRoot(); 89 if( viewRoot != null ) 90 currentLocale = viewRoot.getLocale(); 91 else 92 currentLocale = facesContext.getApplication().getDefaultLocale(); 93 94 95 TreeMap map = new TreeMap (); 96 98 for(int i=0; i<availableCountries.length; i++){ 99 String countryCode = availableCountries[i]; 100 Locale tmp = new Locale (countryCode, countryCode); 101 map.put(tmp.getDisplayCountry(currentLocale), countryCode); 102 } 103 104 List countriesSelectItems = new ArrayList (availableCountries.length); 105 106 int maxDescriptionLength = _maxLength==null ? Integer.MAX_VALUE : _maxLength.intValue(); 107 if( maxDescriptionLength < 5 ) 108 maxDescriptionLength = 5; 109 110 for(Iterator i = map.keySet().iterator(); i.hasNext(); ){ 111 String countryName = (String ) i.next(); 112 String countryCode = (String ) map.get( countryName ); 113 String label; 114 if( countryName.length() <= maxDescriptionLength ) 115 label = countryName; 116 else{ 117 label = countryName.substring(0, maxDescriptionLength-3)+"..."; 118 } 119 120 UISelectItem selectItem = new UISelectItem(); 121 selectItem.setValue( new SelectItem(countryCode, label) ); 122 123 countriesSelectItems.add( selectItem ); 124 } 125 126 return countriesSelectItems; 127 } 128 } | Popular Tags |