001: package com.xoetrope.swing;
002:
003: import java.text.DecimalFormat;
004: import java.text.NumberFormat;
005: import java.util.Currency;
006: import java.util.Locale;
007: import javax.swing.text.DefaultFormatterFactory;
008: import javax.swing.text.NumberFormatter;
009: import net.xoetrope.swing.XEdit;
010:
011: import net.xoetrope.xui.XTextHolder;
012:
013: /**
014: * Handles input of monetary values, stripping out the thousand separators as needed.
015: * THIS CLASS IS INCOMPLETE
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: */
022: public class XMoneyEdit extends XEdit implements XTextHolder {
023: protected char thousandSeparator = ',';
024: protected NumberFormat format;
025: protected Currency currency;
026: protected Locale locale;
027: private boolean prefixed;
028: private boolean suffixed;
029:
030: /**
031: * Create a new XMoneyEdit
032: */
033: public XMoneyEdit() {
034: super ();
035:
036: locale = Locale.getDefault();
037: currency = Currency.getInstance(locale);
038: NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
039: setNumberFormat(nf);
040: }
041:
042: /**
043: * Gets the value of the control stripping out and thousand separators and
044: * spaces in the process
045: * @return the stripped value
046: */
047: public String getText() {
048: String text = super .getText().trim();
049: int pos = text.indexOf(thousandSeparator);
050: while (pos > 0) {
051: text = text.substring(0, pos) + text.substring(pos + 1);
052: pos = text.indexOf(thousandSeparator);
053: }
054:
055: pos = text.indexOf(' ');
056: while (pos > 0) {
057: text = text.substring(0, pos) + text.substring(pos + 1);
058: pos = text.indexOf(' ');
059: }
060: return text;
061: }
062:
063: /**
064: * Set the format of the currency field
065: * @param format the new currency format - the group separator
066: */
067: public void setFormat(String format) {
068: thousandSeparator = format.charAt(0);
069: }
070:
071: /**
072: * Set the format of the currency field
073: * @return - the group separator
074: */
075: public String getFormat() {
076: return ((thousandSeparator == ',') ? "," : "");
077: }
078:
079: /**
080: * Set the DecimalFormat of the currency field
081: * @param df the new DecimalFormat
082: */
083: public void setNumberFormat(NumberFormat df) {
084: format = df;
085: format.setCurrency(currency);
086:
087: if (df instanceof DecimalFormat)
088: thousandSeparator = ((DecimalFormat) df)
089: .getDecimalFormatSymbols().getGroupingSeparator();
090:
091: setFormatterFactory(new DefaultFormatterFactory(
092: new NumberFormatter(df)));
093: }
094:
095: /**
096: * Set the DecimalFormat of the currency field
097: * @return - the format
098: */
099: public NumberFormat getNumberFormat() {
100: return format;
101: }
102:
103: /**
104: * Set the currency field
105: * @param currency the new currency
106: */
107: public void setCurrency(String currencyCode) {
108: DecimalFormat df = (DecimalFormat) DecimalFormat
109: .getCurrencyInstance(locale);
110: currency = Currency.getInstance(currencyCode);
111: df.setCurrency(currency);
112: if (!prefixed) {
113: df.setPositivePrefix("");
114: df.setNegativePrefix("-");
115: }
116:
117: format = df;
118:
119: setFormatterFactory(new DefaultFormatterFactory(
120: new NumberFormatter(df)));
121: }
122:
123: /**
124: * Set the country code for the currency e.g 'EN', 'DA', 'FR'
125: * @param localeCode the language code of the new currency
126: */
127: public void setLocaleLanguage(String localeCode) {
128: if (localeCode.length() > 0) {
129: locale = new Locale(localeCode, getLocaleCountry());
130: setCurrency(getCurrency());
131: }
132: }
133:
134: /**
135: * Set the locale country code e.g 'IE', 'US', 'FR'
136: * @param countryCode country code of the new currency
137: */
138: public void setLocaleCountry(String countryCode) {
139: if (countryCode.length() > 0) {
140: locale = new Locale(getLocaleLanguage(), countryCode);
141: setCurrency(getCurrency());
142: }
143: }
144:
145: /**
146: * Get the locale language code
147: * @return the language code
148: */
149: public String getLocaleLanguage() {
150: return locale.getLanguage();
151: }
152:
153: /**
154: * Get the locale country code
155: * @return the language code
156: */
157: public String getLocaleCountry() {
158: return locale.getCountry();
159: }
160:
161: /**
162: * Set the format of the currency field
163: * @return - the group separator
164: */
165: public String getCurrency() {
166: return currency.getCurrencyCode();
167: }
168:
169: /**
170: * Set one or more attributes of the component.
171: * <OL>
172: * <LI>format, value=the currency format</LI>
173: * <LI>currency, value=the ISO 4217 currency code</LI>
174: * <LI>alignment, value=(Left|Right|Center|Leading|Trailing)</LI>
175: * <LI>border, value=0, to tun off the border</LI>
176: * <LI>margin, value=the size in pixels of the margin (the space between the border and the text)</LI>
177: * <LI>tooltip, value=the tooltip text</LI>
178: * <LI>format, value=integer|curreny|date|decimal or a mask for a mask format</LI>
179: * <LI>editable, value=(true|false) set the edit to being editable</LI>
180: * <LI>antialias, value=(true|false) antialias the text</LI>
181: * </OL>
182: * @param attribName the name of the attribute
183: * @param attribValue the value of the attribute
184: * @return 0 for success, non zero for failure or to require some further action
185: */
186: public int setAttribute(String attribName, Object attribValue) {
187: String attribValueStr = (String) attribValue;
188: if (attribName.equalsIgnoreCase("format"))
189: thousandSeparator = attribValueStr.charAt(0);
190: else if (attribName.equalsIgnoreCase("currency"))
191: setCurrency(attribValueStr);
192: else if (attribName.equalsIgnoreCase("language"))
193: setLocaleLanguage(attribValueStr);
194: else if (attribName.equalsIgnoreCase("country"))
195: setLocaleCountry(attribValueStr);
196: else if (attribName.equalsIgnoreCase("usePrefix"))
197: setPrefixed("true".equals(attribValueStr));
198: else if (attribName.equalsIgnoreCase("useSuffix"))
199: setSuffixed("true".equals(attribValueStr));
200: else
201: super .setAttribute(attribName, attribValue);
202:
203: return 0;
204: }
205:
206: /**
207: * Does the format to use a currency prefix
208: * @return true if a prefix is used
209: */
210: public boolean isPrefixed() {
211: return prefixed;
212: }
213:
214: /**
215: * Set the currency prefix flag
216: * @param state true if a prefix is to be used
217: */
218: public void setPrefixed(boolean state) {
219: prefixed = state;
220: }
221:
222: /**
223: * Does the format to use a currency suffix
224: * @return trueif a prefix is used
225: */
226: public boolean isSuffixed() {
227: return suffixed;
228: }
229:
230: /**
231: * Set the currency suffix flag
232: * @param state true if a suffix is to be used
233: */
234: public void setSuffixed(boolean state) {
235: suffixed = state;
236: }
237: }
|