KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > l10n > DefaultNumberFormatCfMod


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Dec 23, 2004
33  */

34 package com.nightlabs.l10n;
35
36 import java.text.DecimalFormat JavaDoc;
37 import java.text.DecimalFormatSymbols JavaDoc;
38 import java.text.NumberFormat JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 import com.nightlabs.config.ConfigModule;
42 import com.nightlabs.config.InitException;
43
44 /**
45  * @author Marco Schulze - marco at nightlabs dot de
46  */

47 public class DefaultNumberFormatCfMod extends ConfigModule
48 {
49     private String JavaDoc positivePrefix;
50     private String JavaDoc positiveSuffix;
51     private String JavaDoc negativePrefix;
52     private String JavaDoc negativeSuffix;
53
54     private char decimalSeparator = 0;
55     private char groupingSeparator = 0;
56     private int groupingSize = -1;
57
58     private String JavaDoc currencySymbolPosition;
59
60     public static String JavaDoc CURRENCYSYMBOLPOSITION_BEGIN = "begin";
61     public static String JavaDoc CURRENCYSYMBOLPOSITION_END = "end";
62
63     
64     public DefaultNumberFormatCfMod()
65     {
66     }
67
68     /**
69      * @see com.nightlabs.config.ConfigModule#getIdentifier()
70      */

71     public String JavaDoc getIdentifier()
72     {
73         return super.getIdentifier();
74     }
75     /**
76      * @see com.nightlabs.config.ConfigModule#setIdentifier(java.lang.String)
77      */

78     public void setIdentifier(String JavaDoc _identifier)
79     {
80         super.setIdentifier(_identifier);
81     }
82
83
84     /**
85      * @see com.nightlabs.config.ConfigModule#init()
86      */

87     public void init() throws InitException
88     {
89         String JavaDoc identifier = getIdentifier();
90         if (identifier == null)
91             throw new IllegalStateException JavaDoc("identifier of this ConfigModule is null! It should be the language/country-code (e.g. en_US)!");
92
93         String JavaDoc[] sa = identifier.split("_");
94         if (sa.length < 1)
95             throw new IllegalStateException JavaDoc("identifier of this ConfigModule is invalid (empty?!)! It should be the language/country-code (e.g. en_US)!");
96
97         String JavaDoc language = sa[0];
98         String JavaDoc country = null;
99         if (sa.length > 1)
100             country = sa[1];
101
102         Locale JavaDoc locale = new Locale JavaDoc(language, country);
103         
104         DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc(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 JavaDoc nf = NumberFormat.getNumberInstance();
126             if (nf instanceof DecimalFormat JavaDoc)
127                 setGroupingSize(((DecimalFormat JavaDoc)nf).getGroupingSize());
128             else
129                 setGroupingSize(3);
130         }
131
132         if (currencySymbolPosition == null)
133             setCurrencySymbolPosition(CURRENCYSYMBOLPOSITION_END);
134     }
135
136     /**
137      * @return Returns the currencySymbolPosition.
138      */

139     public String JavaDoc getCurrencySymbolPosition()
140     {
141         return currencySymbolPosition;
142     }
143     /**
144      * @param currencySymbolPosition The currencySymbolPosition to set.
145      */

146     public void setCurrencySymbolPosition(String JavaDoc 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 JavaDoc(
154                     "currencySymbolPosition \""+currencySymbolPosition+"\" is invalid!" +
155                             " Must be CURRENCYSYMBOLPOSITION_BEGIN=\""+CURRENCYSYMBOLPOSITION_BEGIN+"\" or " +
156                                     "CURRENCYSYMBOLPOSITION_END=\""+CURRENCYSYMBOLPOSITION_END+"\"!");
157
158         setChanged();
159     }
160     /**
161      * @return Returns the decimalSeparator.
162      */

163     public char getDecimalSeparator()
164     {
165         return decimalSeparator;
166     }
167     /**
168      * @param decimalSeparator The decimalSeparator to set.
169      */

170     public void setDecimalSeparator(char decimalSeparator)
171     {
172         this.decimalSeparator = decimalSeparator;
173         setChanged();
174     }
175     /**
176      * @return Returns the groupingSeparator.
177      */

178     public char getGroupingSeparator()
179     {
180         return groupingSeparator;
181     }
182     /**
183      * @param groupingSeparator The groupingSeparator to set.
184      */

185     public void setGroupingSeparator(char groupingSeparator)
186     {
187         this.groupingSeparator = groupingSeparator;
188         setChanged();
189     }
190     /**
191      * @return Returns the groupingSize.
192      */

193     public int getGroupingSize()
194     {
195         return groupingSize;
196     }
197     /**
198      * @param groupingSize The groupingSize to set.
199      */

200     public void setGroupingSize(int groupingSize)
201     {
202         this.groupingSize = groupingSize;
203         setChanged();
204     }
205     /**
206      * @return Returns the negativePrefix.
207      */

208     public String JavaDoc getNegativePrefix()
209     {
210         return negativePrefix;
211     }
212     /**
213      * @param negativePrefix The negativePrefix to set.
214      */

215     public void setNegativePrefix(String JavaDoc negativePrefix)
216     {
217         this.negativePrefix = negativePrefix;
218         setChanged();
219     }
220     /**
221      * @return Returns the negativeSuffix.
222      */

223     public String JavaDoc getNegativeSuffix()
224     {
225         return negativeSuffix;
226     }
227     /**
228      * @param negativeSuffix The negativeSuffix to set.
229      */

230     public void setNegativeSuffix(String JavaDoc negativeSuffix)
231     {
232         this.negativeSuffix = negativeSuffix;
233         setChanged();
234     }
235     /**
236      * @return Returns the positivePrefix.
237      */

238     public String JavaDoc getPositivePrefix()
239     {
240         return positivePrefix;
241     }
242     /**
243      * @param positivePrefix The positivePrefix to set.
244      */

245     public void setPositivePrefix(String JavaDoc positivePrefix)
246     {
247         this.positivePrefix = positivePrefix;
248         setChanged();
249     }
250     /**
251      * @return Returns the positiveSuffix.
252      */

253     public String JavaDoc getPositiveSuffix()
254     {
255         return positiveSuffix;
256     }
257     /**
258      * @param positiveSuffix The positiveSuffix to set.
259      */

260     public void setPositiveSuffix(String JavaDoc positiveSuffix)
261     {
262         this.positiveSuffix = positiveSuffix;
263         setChanged();
264     }
265 }
266
Popular Tags