1 19 20 21 package org.netbeans.modules.i18n; 22 23 24 import java.awt.Component ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.beans.PropertyEditorSupport ; 28 import java.util.Locale ; 29 30 import org.netbeans.modules.properties.LocalePanel; 31 32 33 38 public class LocalePropertyEditor extends PropertyEditorSupport { 39 40 41 private Locale locale; 42 43 44 45 public LocalePropertyEditor() { 46 } 47 48 49 50 public void setValue(Object value) { 51 if(!(value instanceof Locale )) 52 throw new IllegalArgumentException ("I18N module: Bad class type of value:"+value.getClass().getName()); 54 if(locale != null && locale.equals(value)) 55 return; 56 57 locale = (Locale )value; 58 firePropertyChange(); 59 } 60 61 62 public Object getValue() { 63 return locale; 64 } 65 66 67 public String getAsText() { 68 if(locale == null) 69 return super.getAsText(); 71 return locale.toString(); 72 } 73 74 75 public void setAsText(String text) throws IllegalArgumentException { 76 setValue(createLocaleFromText(text)); 77 } 78 79 80 public String getJavaInitializationString() { 81 if(locale == null) 82 return super.getJavaInitializationString(); 83 84 StringBuffer localeInit = new StringBuffer ("new Locale("); 86 String language = locale.getLanguage(); 87 String country = locale.getCountry(); 88 String variant = locale.getVariant(); 89 90 if(language == null) 91 localeInit.append("\"\""); else 93 localeInit.append("\""+language+"\""); 95 if(country == null) 96 localeInit.append(",\"\""); else 98 localeInit.append(",\""+country+"\""); 100 if(variant == null) 101 localeInit.append(")"); else 103 localeInit.append(",\""+variant+"\")"); 105 return localeInit.toString(); 106 } 107 108 110 public boolean supportsCustomEditor() { 111 return true; 112 } 113 114 115 public Component getCustomEditor() { 116 final LocalePanel localePanel = new LocalePanel(locale); 117 118 localePanel.addPropertyChangeListener(new PropertyChangeListener () { 119 public void propertyChange(PropertyChangeEvent evt) { 120 if(LocalePanel.PROP_CUSTOMIZED_LOCALE.equals(evt.getPropertyName())) 121 setValue(localePanel.getLocale()); 122 } 123 }); 124 125 return localePanel; 126 } 127 128 129 private static Locale createLocaleFromText(String text) { 130 if(text == null || "".equals(text)) return new Locale ("", ""); 133 int underscore = text.indexOf('_'); 135 136 String language; 137 138 if(underscore == -1) 139 return new Locale (text, ""); else if(underscore == 0) 141 language = ""; else 143 language = text.substring(0, underscore); 144 145 if(text.length() <= underscore + 1) 146 return new Locale (language, ""); 148 text = text.substring(underscore + 1); 149 150 underscore = text.indexOf('_'); 152 153 String country; 154 155 if(underscore == -1) 156 return new Locale (language, text); 157 else if(underscore == 0) 158 country = ""; else 160 country = text.substring(0, underscore); 161 162 if(text.length() <= underscore + 1) 164 return new Locale (language, country); 166 String variant = text.substring(underscore + 1); 167 168 return new Locale (language, country, variant); 169 } 170 } 171 | Popular Tags |