| 1 30 31 34 package com.nightlabs.l10n; 35 36 import java.text.DecimalFormat ; 37 import java.text.DecimalFormatSymbols ; 38 import java.text.NumberFormat ; 39 import java.util.Locale ; 40 41 import com.nightlabs.config.ConfigModule; 42 import com.nightlabs.config.InitException; 43 44 47 public class DefaultNumberFormatCfMod extends ConfigModule 48 { 49 private String positivePrefix; 50 private String positiveSuffix; 51 private String negativePrefix; 52 private String negativeSuffix; 53 54 private char decimalSeparator = 0; 55 private char groupingSeparator = 0; 56 private int groupingSize = -1; 57 58 private String currencySymbolPosition; 59 60 public static String CURRENCYSYMBOLPOSITION_BEGIN = "begin"; 61 public static String CURRENCYSYMBOLPOSITION_END = "end"; 62 63 64 public DefaultNumberFormatCfMod() 65 { 66 } 67 68 71 public String getIdentifier() 72 { 73 return super.getIdentifier(); 74 } 75 78 public void setIdentifier(String _identifier) 79 { 80 super.setIdentifier(_identifier); 81 } 82 83 84 87 public void init() throws InitException 88 { 89 String identifier = getIdentifier(); 90 if (identifier == null) 91 throw new IllegalStateException ("identifier of this ConfigModule is null! It should be the language/country-code (e.g. en_US)!"); 92 93 String [] sa = identifier.split("_"); 94 if (sa.length < 1) 95 throw new IllegalStateException ("identifier of this ConfigModule is invalid (empty?!)! It should be the language/country-code (e.g. en_US)!"); 96 97 String language = sa[0]; 98 String country = null; 99 if (sa.length > 1) 100 country = sa[1]; 101 102 Locale locale = new Locale (language, country); 103 104 DecimalFormatSymbols dfs = new DecimalFormatSymbols (locale); 105 106 if (positivePrefix == null) 107 setPositivePrefix(""); 108 109 if (positiveSuffix == null) 110 setPositiveSuffix(""); 111 112 if (negativePrefix == null) 113 setNegativePrefix(""+dfs.getMinusSign()); 114 115 if (negativeSuffix == null) 116 setNegativeSuffix(""); 117 118 if (decimalSeparator == 0) 119 setDecimalSeparator(dfs.getDecimalSeparator()); 120 121 if (groupingSeparator == 0) 122 setGroupingSeparator(dfs.getGroupingSeparator()); 123 124 if (groupingSize < 0) { 125 NumberFormat nf = NumberFormat.getNumberInstance(); 126 if (nf instanceof DecimalFormat ) 127 setGroupingSize(((DecimalFormat )nf).getGroupingSize()); 128 else 129 setGroupingSize(3); 130 } 131 132 if (currencySymbolPosition == null) 133 setCurrencySymbolPosition(CURRENCYSYMBOLPOSITION_END); 134 } 135 136 139 public String getCurrencySymbolPosition() 140 { 141 return currencySymbolPosition; 142 } 143 146 public void setCurrencySymbolPosition(String currencySymbolPosition) 147 { 148 if (CURRENCYSYMBOLPOSITION_BEGIN.equals(currencySymbolPosition)) 149 this.currencySymbolPosition = CURRENCYSYMBOLPOSITION_BEGIN; 150 else if (CURRENCYSYMBOLPOSITION_END.equals(currencySymbolPosition)) 151 this.currencySymbolPosition = CURRENCYSYMBOLPOSITION_END; 152 else 153 throw new IllegalArgumentException ( 154 "currencySymbolPosition \""+currencySymbolPosition+"\" is invalid!" + 155 " Must be CURRENCYSYMBOLPOSITION_BEGIN=\""+CURRENCYSYMBOLPOSITION_BEGIN+"\" or " + 156 "CURRENCYSYMBOLPOSITION_END=\""+CURRENCYSYMBOLPOSITION_END+"\"!"); 157 158 setChanged(); 159 } 160 163 public char getDecimalSeparator() 164 { 165 return decimalSeparator; 166 } 167 170 public void setDecimalSeparator(char decimalSeparator) 171 { 172 this.decimalSeparator = decimalSeparator; 173 setChanged(); 174 } 175 178 public char getGroupingSeparator() 179 { 180 return groupingSeparator; 181 } 182 185 public void setGroupingSeparator(char groupingSeparator) 186 { 187 this.groupingSeparator = groupingSeparator; 188 setChanged(); 189 } 190 193 public int getGroupingSize() 194 { 195 return groupingSize; 196 } 197 200 public void setGroupingSize(int groupingSize) 201 { 202 this.groupingSize = groupingSize; 203 setChanged(); 204 } 205 208 public String getNegativePrefix() 209 { 210 return negativePrefix; 211 } 212 215 public void setNegativePrefix(String negativePrefix) 216 { 217 this.negativePrefix = negativePrefix; 218 setChanged(); 219 } 220 223 public String getNegativeSuffix() 224 { 225 return negativeSuffix; 226 } 227 230 public void setNegativeSuffix(String negativeSuffix) 231 { 232 this.negativeSuffix = negativeSuffix; 233 setChanged(); 234 } 235 238 public String getPositivePrefix() 239 { 240 return positivePrefix; 241 } 242 245 public void setPositivePrefix(String positivePrefix) 246 { 247 this.positivePrefix = positivePrefix; 248 setChanged(); 249 } 250 253 public String getPositiveSuffix() 254 { 255 return positiveSuffix; 256 } 257 260 public void setPositiveSuffix(String positiveSuffix) 261 { 262 this.positiveSuffix = positiveSuffix; 263 setChanged(); 264 } 265 } 266 | Popular Tags |