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 boolean
alwaysShowDec
Flag for display of the decimal separatorprotected static java.util.Hashtable<java.util.Locale,java.lang.String>
cachedPatterns
Cache of number patterns by localesprotected int
decimalLen
Maximum number of digits of the decimal partprotected java.lang.String
editPattern
Edit patternprotected java.lang.StringBuffer
editValue
Buffer for the edit valueprotected boolean
fixedDec
Flag indicating that the decimal part has a fixed lengthprotected boolean
fixedInt
Flag indicating that the int part has a fixed lengthprotected int
groupLen
Length of groups (0 if no group separator)protected int
intCount
Current number of digits of the int partprotected int
intLen
Maximum number of digits of the int partprotected java.util.Locale
locale
The locale usedprotected boolean
minus
Flag for display of the negative signprotected boolean
modified
Flag indicating that the buffer is modified and the Number value must be computedprotected boolean
nbspSeparator
Flag indicating the use of the 0xAO (no-break space) grouping separatorprotected boolean
negative
Flag indicating that the current value is negativeprotected java.text.DecimalFormat
nfDisplay
Number formatter for displayprotected java.text.DecimalFormat
nfEdit
Number formatter for display editprotected int
prefixLen
Length of the prefix part in the cacheprotected int
suffixLen
Length of the suffix part in the cacheprotected java.text.DecimalFormatSymbols
symbols
Symbols used to format numbersprotected java.lang.Number
value
Number containing the current valueprotected int
zeroDecimalLen
Minimum number of digits of the decimal part (complement by 0)protected int
zeroIntLen
Minimum 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 void
clearText(int start, int len)
Clears a part of the edition cache.protected int
format(int curseur)
Formats the edit buffer.protected java.lang.String
getDefaultPattern(java.util.Locale loc)
Returns the default edit pattern for a given locale.java.lang.String
getDisplayString()
Returns the current value formatted for display.java.lang.String
getEditString()
Returns the current value formatted for editing.java.lang.Object
getValue()
Returns the current value of the text control if it is a validNumber
.java.lang.Class<?>
getValueType()
Returns the type of value thisITextFormatter
handles, i.e. returns ingetValue()
.
A NumberFormatter always returns a Number value.boolean
isEmpty()
Returnstrue
if current edited value is empty, else returnsfalse
.boolean
isValid()
Returnstrue
if current edited value is valid, else returnsfalse
.
A NumberFormatter is valid if the cached value is not null.void
setDecimalSeparatorAlwaysShown(boolean show)
Sets the flag to always display the decimal separator, even if the decimal part is empty.void
setFixedLengths(boolean fixedInt, boolean fixedDec)
Sets the fixed length flags.
By default, int and decimal part of the pattern have a fixed length.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.protected void
setPrefix(java.lang.String prefix)
Sets a prefix to display before the value.
To clear the current prefix, call thesetPrefix
method with anull
parameter.protected void
setSuffix(java.lang.String suffix)
Sets a suffix to display after the value.
To clear the current suffix, call thesetSuffix
method with anull
parameter.void
setValue(java.lang.Object value)
Sets the value to edit.void
verifyText(org.eclipse.swt.events.VerifyEvent e)
Handles aVerifyEvent
sent 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 byFormattedText
when theText
widget looses focus. The displayed value is the result of formatting on theNumber
with aDecimalFormat
for 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 byFormattedText
when theText
widget gains focus. The value returned is the content of the StringBuildereditValue
used 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 thenfEdit
initialized with the edit pattern. If the number is not valid, returnsnull
.- Returns:
- current number value if valid,
null
else - See Also:
ITextFormatter.getValue()
-
getValueType
public java.lang.Class<?> getValueType()Returns the type of value thisITextFormatter
handles, i.e. returns ingetValue()
.
A NumberFormatter always returns a Number value.- Returns:
- The value type.
-
isEmpty
public boolean isEmpty()Returnstrue
if current edited value is empty, else returnsfalse
.- Returns:
true
if empty, elsefalse
-
isValid
public boolean isValid()Returnstrue
if current edited value is valid, else returnsfalse
.
A NumberFormatter is valid if the cached value is not null.- Returns:
true
if 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 thesetPrefix
method with anull
parameter.- Parameters:
prefix
- prefix to display, ornull
to clear
-
setSuffix
protected void setSuffix(java.lang.String suffix)Sets a suffix to display after the value.
To clear the current suffix, call thesetSuffix
method with anull
parameter.- Parameters:
suffix
- suffix to display, ornull
to 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 aVerifyEvent
sent 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)
-