public class NumberFormatter extends AbstractFormatter
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.
Symbol | Meaning |
---|---|
0 |
Digit |
# |
Digit, zero shows as absent |
. |
Decimal separator |
- |
Minus sign |
, |
Grouping separator |
new NumberFormatter("#,##0.00")
- 1234.5 will edit and
display as "1,234.50".Modifier and Type | Field and Description |
---|---|
protected boolean |
alwaysShowDec
Flag for display of the decimal separator
|
protected static java.util.Hashtable<java.util.Locale,java.lang.String> |
cachedPatterns
Cache of number patterns by locales
|
protected int |
decimalLen
Maximum number of digits of the decimal part
|
protected java.lang.String |
editPattern
Edit pattern
|
protected java.lang.StringBuffer |
editValue
Buffer for the edit value
|
protected boolean |
fixedDec
Flag indicating that the decimal part has a fixed length
|
protected boolean |
fixedInt
Flag indicating that the int part has a fixed length
|
protected int |
groupLen
Length of groups (0 if no group separator)
|
protected int |
intCount
Current number of digits of the int part
|
protected int |
intLen
Maximum number of digits of the int part
|
protected java.util.Locale |
locale
The locale used
|
protected boolean |
minus
Flag for display of the negative sign
|
protected boolean |
modified
Flag indicating that the buffer is modified and the Number value must be computed
|
protected boolean |
nbspSeparator
Flag indicating the use of the 0xAO (no-break space) grouping separator
|
protected boolean |
negative
Flag indicating that the current value is negative
|
protected java.text.DecimalFormat |
nfDisplay
Number formatter for display
|
protected java.text.DecimalFormat |
nfEdit
Number formatter for display edit
|
protected int |
prefixLen
Length of the prefix part in the cache
|
protected int |
suffixLen
Length of the suffix part in the cache
|
protected java.text.DecimalFormatSymbols |
symbols
Symbols used to format numbers
|
protected java.lang.Number |
value
Number containing the current value
|
protected 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)
|
EMPTY, ignore, SPACE, text
Constructor and 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 locale
|
NumberFormatter(java.util.Locale loc)
Constructs a new instance with default edit and display masks for the given
locale.
|
NumberFormatter(java.lang.String editPattern)
Constructs a new instance with the given edit mask.
|
NumberFormatter(java.lang.String editPattern,
java.util.Locale loc)
Constructs a new instance with the given edit mask and locale.
|
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.
|
Modifier and Type | Method and 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 valid
Number . |
java.lang.Class<?> |
getValueType()
Returns the type of value this
ITextFormatter handles,
i.e. |
boolean |
isEmpty()
Returns
true if current edited value is empty, else returns
false . |
boolean |
isValid()
Returns
true if current edited value is valid, else returns
false .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 the setPrefix method with a
null parameter. |
protected void |
setSuffix(java.lang.String suffix)
Sets a suffix to display after the value.
To clear the current suffix, call the setSuffix method with a
null parameter. |
void |
setValue(java.lang.Object value)
Sets the value to edit.
|
void |
verifyText(org.eclipse.swt.events.VerifyEvent e)
Handles a
VerifyEvent sent when the text is about to be modified. |
beep, detach, setIgnore, setText, updateText, updateText
protected static java.util.Hashtable<java.util.Locale,java.lang.String> cachedPatterns
protected java.text.DecimalFormat nfDisplay
protected java.text.DecimalFormat nfEdit
protected java.lang.StringBuffer editValue
protected java.lang.Number value
protected java.lang.String editPattern
protected java.util.Locale locale
protected int groupLen
protected int intCount
protected int intLen
protected int decimalLen
protected int zeroIntLen
protected int zeroDecimalLen
protected boolean minus
protected boolean negative
protected java.text.DecimalFormatSymbols symbols
protected boolean nbspSeparator
protected boolean alwaysShowDec
protected boolean modified
protected boolean fixedInt
protected boolean fixedDec
protected int prefixLen
protected int suffixLen
public NumberFormatter()
public NumberFormatter(java.util.Locale loc)
loc
- localepublic NumberFormatter(java.lang.String editPattern)
editPattern
- edit maskpublic NumberFormatter(java.lang.String editPattern, java.util.Locale loc)
editPattern
- edit maskloc
- localepublic NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern)
editPattern
- edit maskdisplayPattern
- display maskpublic NumberFormatter(java.lang.String editPattern, java.lang.String displayPattern, java.util.Locale loc)
editPattern
- edit maskdisplayPattern
- display maskloc
- localeprotected void clearText(int start, int len)
start
- beginning indexlen
- length of portion to clearprotected int format(int curseur)
curseur
- Current position of the cursorprotected java.lang.String getDefaultPattern(java.util.Locale loc)
loc
- localepublic java.lang.String getDisplayString()
FormattedText
when the Text
widget looses focus.
The displayed value is the result of formatting on the Number
with a DecimalFormat for the display pattern passed in
constructor.
ITextFormatter.getDisplayString()
public java.lang.String getEditString()
FormattedText
when the Text
widget gains focus.
The value returned is the content of the StringBuilder editValue
used as cache.ITextFormatter.getEditString()
public java.lang.Object getValue()
Number
.
If the buffer is flagged as modified, the value is recalculated by parsing
with the nfEdit
initialized with the edit pattern. If the
number is not valid, returns null
.null
elseITextFormatter.getValue()
public java.lang.Class<?> getValueType()
ITextFormatter
handles,
i.e. returns in getValue()
.public boolean isEmpty()
true
if current edited value is empty, else returns
false
.true
if empty, else false
public boolean isValid()
true
if current edited value is valid, else returns
false
.true
if valid, else false
ITextFormatter.isValid()
public void setDecimalSeparatorAlwaysShown(boolean show)
show
- true / falsepublic void setFixedLengths(boolean fixedInt, boolean fixedDec)
fixedInt
- flag for int partfixedDec
- flag for decimal partprotected void setPatterns(java.lang.String edit, java.lang.String display, java.util.Locale loc)
edit
- edit patterndisplay
- display patternloc
- Locale to usejava.lang.IllegalArgumentException
- if a pattern is invalidprotected void setPrefix(java.lang.String prefix)
setPrefix
method with a
null
parameter.prefix
- prefix to display, or null
to clearprotected void setSuffix(java.lang.String suffix)
setSuffix
method with a
null
parameter.suffix
- suffix to display, or null
to clearpublic void setValue(java.lang.Object value)
Number
.value
- number valuejava.lang.IllegalArgumentException
- if not a numberITextFormatter.setValue(java.lang.Object)
public void verifyText(org.eclipse.swt.events.VerifyEvent e)
VerifyEvent
sent when the text is about to be modified.
This method is the entry point of all operations of formatting.VerifyListener.verifyText(org.eclipse.swt.events.VerifyEvent)