Class NumberFormatter
java.lang.Object
org.eclipse.nebula.widgets.formattedtext.AbstractFormatter
org.eclipse.nebula.widgets.formattedtext.NumberFormatter
- All Implemented Interfaces:
java.util.EventListener,ITextFormatter,org.eclipse.swt.events.VerifyListener,org.eclipse.swt.internal.SWTEventListener
- Direct Known Subclasses:
BigDecimalFormatter,BigIntegerFormatter,DoubleFormatter,FloatFormatter,IntegerFormatter,LongFormatter,PercentFormatter,ShortFormatter
public class NumberFormatter extends AbstractFormatter
This class provides formatting of
Number values in a
FormattedText.
Formatter is composed of an edit pattern and a display pattern.
Display pattern uses the same syntax than DecimalFormat, and
uses it to compute the value to display.
Edit pattern is more limited and composed of two part, the int part and
the decimal part. Formatting characters allow to specify number of digits,
minimal length, decimal position, grouping and negative sign.
Patterns Characters
| Symbol | Meaning |
|---|---|
0 |
Digit |
# |
Digit, zero shows as absent |
. |
Decimal separator |
- |
Minus sign |
, |
Grouping separator |
Examples
new NumberFormatter("#,##0.00")- 1234.5 will edit and display as "1,234.50".
-
Field Summary
Fields Modifier and Type Field Description protected booleanalwaysShowDecFlag for display of the decimal separatorprotected static java.util.Hashtable<java.util.Locale,java.lang.String>cachedPatternsCache of number patterns by localesprotected intdecimalLenMaximum number of digits of the decimal partprotected java.lang.StringeditPatternEdit patternprotected java.lang.StringBuffereditValueBuffer for the edit valueprotected booleanfixedDecFlag indicating that the decimal part has a fixed lengthprotected booleanfixedIntFlag indicating that the int part has a fixed lengthprotected intgroupLenLength of groups (0 if no group separator)protected intintCountCurrent number of digits of the int partprotected intintLenMaximum number of digits of the int partprotected java.util.LocalelocaleThe locale usedprotected booleanminusFlag for display of the negative signprotected booleanmodifiedFlag indicating that the buffer is modified and the Number value must be computedprotected booleannbspSeparatorFlag indicating the use of the 0xAO (no-break space) grouping separatorprotected booleannegativeFlag indicating that the current value is negativeprotected java.text.DecimalFormatnfDisplayNumber formatter for displayprotected java.text.DecimalFormatnfEditNumber formatter for display editprotected intprefixLenLength of the prefix part in the cacheprotected intsuffixLenLength of the suffix part in the cacheprotected java.text.DecimalFormatSymbolssymbolsSymbols used to format numbersprotected java.lang.NumbervalueNumber containing the current valueprotected intzeroDecimalLenMinimum number of digits of the decimal part (complement by 0)protected intzeroIntLenMinimum number of digits of the int part (complement by 0)Fields inherited from class org.eclipse.nebula.widgets.formattedtext.AbstractFormatter
EMPTY, ignore, SPACE, text -
Constructor Summary
Constructors Constructor Description NumberFormatter()Constructs a new instance with all defaults : edit mask from NumberPatterns for the default locale display mask identical to the edit mask default localeNumberFormatter(java.lang.String editPattern)Constructs a new instance with the given edit mask.NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern)Constructs a new instance with the given edit and display masks.NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern, java.util.Locale loc)Constructs a new instance with the given masks and locale.NumberFormatter(java.lang.String editPattern, java.util.Locale loc)Constructs a new instance with the given edit mask and locale.NumberFormatter(java.util.Locale loc)Constructs a new instance with default edit and display masks for the given locale. -
Method Summary
Modifier and Type Method Description protected voidclearText(int start, int len)Clears a part of the edition cache.protected intformat(int curseur)Formats the edit buffer.protected java.lang.StringgetDefaultPattern(java.util.Locale loc)Returns the default edit pattern for a given locale.java.lang.StringgetDisplayString()Returns the current value formatted for display.java.lang.StringgetEditString()Returns the current value formatted for editing.java.lang.ObjectgetValue()Returns the current value of the text control if it is a validNumber.java.lang.Class<?>getValueType()Returns the type of value thisITextFormatterhandles, i.e. returns ingetValue().
A NumberFormatter always returns a Number value.booleanisEmpty()Returnstrueif current edited value is empty, else returnsfalse.booleanisValid()Returnstrueif current edited value is valid, else returnsfalse.
A NumberFormatter is valid if the cached value is not null.voidsetDecimalSeparatorAlwaysShown(boolean show)Sets the flag to always display the decimal separator, even if the decimal part is empty.voidsetFixedLengths(boolean fixedInt, boolean fixedDec)Sets the fixed length flags.
By default, int and decimal part of the pattern have a fixed length.protected voidsetPatterns(java.lang.String edit, java.lang.String display, java.util.Locale loc)Sets the patterns and initializes the technical attributes used to manage the operations.protected voidsetPrefix(java.lang.String prefix)Sets a prefix to display before the value.
To clear the current prefix, call thesetPrefixmethod with anullparameter.protected voidsetSuffix(java.lang.String suffix)Sets a suffix to display after the value.
To clear the current suffix, call thesetSuffixmethod with anullparameter.voidsetValue(java.lang.Object value)Sets the value to edit.voidverifyText(org.eclipse.swt.events.VerifyEvent e)Handles aVerifyEventsent when the text is about to be modified.Methods inherited from class org.eclipse.nebula.widgets.formattedtext.AbstractFormatter
beep, detach, setIgnore, setText, updateText, updateText
-
Field Details
-
cachedPatterns
protected static java.util.Hashtable<java.util.Locale,java.lang.String> cachedPatternsCache of number patterns by locales -
nfDisplay
protected java.text.DecimalFormat nfDisplayNumber formatter for display -
nfEdit
protected java.text.DecimalFormat nfEditNumber formatter for display edit -
editValue
protected java.lang.StringBuffer editValueBuffer for the edit value -
value
protected java.lang.Number valueNumber containing the current value -
editPattern
protected java.lang.String editPatternEdit pattern -
locale
protected java.util.Locale localeThe locale used -
groupLen
protected int groupLenLength of groups (0 if no group separator) -
intCount
protected int intCountCurrent number of digits of the int part -
intLen
protected int intLenMaximum number of digits of the int part -
decimalLen
protected int decimalLenMaximum number of digits of the decimal part -
zeroIntLen
protected int zeroIntLenMinimum number of digits of the int part (complement by 0) -
zeroDecimalLen
protected int zeroDecimalLenMinimum number of digits of the decimal part (complement by 0) -
minus
protected boolean minusFlag for display of the negative sign -
negative
protected boolean negativeFlag indicating that the current value is negative -
symbols
protected java.text.DecimalFormatSymbols symbolsSymbols used to format numbers -
nbspSeparator
protected boolean nbspSeparatorFlag indicating the use of the 0xAO (no-break space) grouping separator -
alwaysShowDec
protected boolean alwaysShowDecFlag for display of the decimal separator -
modified
protected boolean modifiedFlag indicating that the buffer is modified and the Number value must be computed -
fixedInt
protected boolean fixedIntFlag indicating that the int part has a fixed length -
fixedDec
protected boolean fixedDecFlag indicating that the decimal part has a fixed length -
prefixLen
protected int prefixLenLength of the prefix part in the cache -
suffixLen
protected int suffixLenLength of the suffix part in the cache
-
-
Constructor Details
-
NumberFormatter
public NumberFormatter()Constructs a new instance with all defaults :- edit mask from NumberPatterns for the default locale
- display mask identical to the edit mask
- default locale
-
NumberFormatter
public NumberFormatter(java.util.Locale loc)Constructs a new instance with default edit and display masks for the given locale.- Parameters:
loc- locale
-
NumberFormatter
public NumberFormatter(java.lang.String editPattern)Constructs a new instance with the given edit mask. Display mask is identical to the edit mask, and locale is the default one.- Parameters:
editPattern- edit mask
-
NumberFormatter
public NumberFormatter(java.lang.String editPattern, java.util.Locale loc)Constructs a new instance with the given edit mask and locale. Display mask is identical to the edit mask.- Parameters:
editPattern- edit maskloc- locale
-
NumberFormatter
public NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern)Constructs a new instance with the given edit and display masks. Uses the default locale.- Parameters:
editPattern- edit maskdisplayPattern- display mask
-
NumberFormatter
public NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern, java.util.Locale loc)Constructs a new instance with the given masks and locale.- Parameters:
editPattern- edit maskdisplayPattern- display maskloc- locale
-
-
Method Details
-
clearText
protected void clearText(int start, int len)Clears a part of the edition cache. The start and len parameters are adjusted to avoid clearing in prefix and suffix parts of the cache.- Parameters:
start- beginning indexlen- length of portion to clear
-
format
protected int format(int curseur)Formats the edit buffer. Inserts group separators to the right places, deletes excess decimal digits and add 0 to complete to the minimal length of int and decimal parts. The position of the cursor is preserved.- Parameters:
curseur- Current position of the cursor- Returns:
- New position of the cursor
-
getDefaultPattern
protected java.lang.String getDefaultPattern(java.util.Locale loc)Returns the default edit pattern for a given locale.- Parameters:
loc- locale- Returns:
- Edit pattern
-
getDisplayString
public java.lang.String getDisplayString()Returns the current value formatted for display. This method is called byFormattedTextwhen theTextwidget looses focus. The displayed value is the result of formatting on theNumberwith aDecimalFormatfor the display pattern passed in constructor.- Returns:
- display string if valid, empty string else
- See Also:
ITextFormatter.getDisplayString()
-
getEditString
public java.lang.String getEditString()Returns the current value formatted for editing. This method is called byFormattedTextwhen theTextwidget gains focus. The value returned is the content of the StringBuildereditValueused as cache.- Returns:
- edit string
- See Also:
ITextFormatter.getEditString()
-
getValue
public java.lang.Object getValue()Returns the current value of the text control if it is a validNumber. If the buffer is flagged as modified, the value is recalculated by parsing with thenfEditinitialized with the edit pattern. If the number is not valid, returnsnull.- Returns:
- current number value if valid,
nullelse - See Also:
ITextFormatter.getValue()
-
getValueType
public java.lang.Class<?> getValueType()Returns the type of value thisITextFormatterhandles, i.e. returns ingetValue().
A NumberFormatter always returns a Number value.- Returns:
- The value type.
-
isEmpty
public boolean isEmpty()Returnstrueif current edited value is empty, else returnsfalse.- Returns:
trueif empty, elsefalse
-
isValid
public boolean isValid()Returnstrueif current edited value is valid, else returnsfalse.
A NumberFormatter is valid if the cached value is not null.- Returns:
trueif valid, elsefalse- See Also:
ITextFormatter.isValid()
-
setDecimalSeparatorAlwaysShown
public void setDecimalSeparatorAlwaysShown(boolean show)Sets the flag to always display the decimal separator, even if the decimal part is empty.- Parameters:
show- true / false
-
setFixedLengths
public void setFixedLengths(boolean fixedInt, boolean fixedDec)Sets the fixed length flags.
By default, int and decimal part of the pattern have a fixed length.- Parameters:
fixedInt- flag for int partfixedDec- flag for decimal part
-
setPatterns
protected void setPatterns(java.lang.String edit, java.lang.String display, java.util.Locale loc)Sets the patterns and initializes the technical attributes used to manage the operations.- Parameters:
edit- edit patterndisplay- display patternloc- Locale to use- Throws:
java.lang.IllegalArgumentException- if a pattern is invalid
-
setPrefix
protected void setPrefix(java.lang.String prefix)Sets a prefix to display before the value.
To clear the current prefix, call thesetPrefixmethod with anullparameter.- Parameters:
prefix- prefix to display, ornullto clear
-
setSuffix
protected void setSuffix(java.lang.String suffix)Sets a suffix to display after the value.
To clear the current suffix, call thesetSuffixmethod with anullparameter.- Parameters:
suffix- suffix to display, ornullto clear
-
setValue
public void setValue(java.lang.Object value)Sets the value to edit. The value provided must be aNumber.- Parameters:
value- number value- Throws:
java.lang.IllegalArgumentException- if not a number- See Also:
ITextFormatter.setValue(java.lang.Object)
-
verifyText
public void verifyText(org.eclipse.swt.events.VerifyEvent e)Handles aVerifyEventsent when the text is about to be modified. This method is the entry point of all operations of formatting.- See Also:
VerifyListener.verifyText(org.eclipse.swt.events.VerifyEvent)
-