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 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)

    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 locale
    NumberFormatter​(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 valid Number.
    java.lang.Class<?> getValueType()
    Returns the type of value this ITextFormatter handles, i.e. returns in getValue().
    A NumberFormatter always returns a Number value.
    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.

    Methods inherited from class org.eclipse.nebula.widgets.formattedtext.AbstractFormatter

    beep, detach, setIgnore, setText, updateText, updateText

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • cachedPatterns

      protected static java.util.Hashtable<java.util.Locale,​java.lang.String> cachedPatterns
      Cache of number patterns by locales
    • nfDisplay

      protected java.text.DecimalFormat nfDisplay
      Number formatter for display
    • nfEdit

      protected java.text.DecimalFormat nfEdit
      Number formatter for display edit
    • editValue

      protected java.lang.StringBuffer editValue
      Buffer for the edit value
    • value

      protected java.lang.Number value
      Number containing the current value
    • editPattern

      protected java.lang.String editPattern
      Edit pattern
    • locale

      protected java.util.Locale locale
      The locale used
    • groupLen

      protected int groupLen
      Length of groups (0 if no group separator)
    • intCount

      protected int intCount
      Current number of digits of the int part
    • intLen

      protected int intLen
      Maximum number of digits of the int part
    • decimalLen

      protected int decimalLen
      Maximum number of digits of the decimal part
    • zeroIntLen

      protected int zeroIntLen
      Minimum number of digits of the int part (complement by 0)
    • zeroDecimalLen

      protected int zeroDecimalLen
      Minimum number of digits of the decimal part (complement by 0)
    • minus

      protected boolean minus
      Flag for display of the negative sign
    • negative

      protected boolean negative
      Flag indicating that the current value is negative
    • symbols

      protected java.text.DecimalFormatSymbols symbols
      Symbols used to format numbers
    • nbspSeparator

      protected boolean nbspSeparator
      Flag indicating the use of the 0xAO (no-break space) grouping separator
    • alwaysShowDec

      protected boolean alwaysShowDec
      Flag for display of the decimal separator
    • modified

      protected boolean modified
      Flag indicating that the buffer is modified and the Number value must be computed
    • fixedInt

      protected boolean fixedInt
      Flag indicating that the int part has a fixed length
    • fixedDec

      protected boolean fixedDec
      Flag indicating that the decimal part has a fixed length
    • prefixLen

      protected int prefixLen
      Length of the prefix part in the cache
    • suffixLen

      protected int suffixLen
      Length 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 mask
      loc - 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 mask
      displayPattern - 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 mask
      displayPattern - display mask
      loc - 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 index
      len - 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 by 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.
      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 by FormattedText when the Text widget gains focus. The value returned is the content of the StringBuilder editValue 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 valid 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.
      Returns:
      current number value if valid, null else
      See Also:
      ITextFormatter.getValue()
    • getValueType

      public java.lang.Class<?> getValueType()
      Returns the type of value this ITextFormatter handles, i.e. returns in getValue().
      A NumberFormatter always returns a Number value.
      Returns:
      The value type.
    • isEmpty

      public boolean isEmpty()
      Returns true if current edited value is empty, else returns false.
      Returns:
      true if empty, else false
    • isValid

      public boolean isValid()
      Returns true if current edited value is valid, else returns false.
      A NumberFormatter is valid if the cached value is not null.
      Returns:
      true if valid, else false
      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 part
      fixedDec - 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 pattern
      display - display pattern
      loc - 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 the setPrefix method with a null parameter.
      Parameters:
      prefix - prefix to display, or null to clear
    • setSuffix

      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.
      Parameters:
      suffix - suffix to display, or null to clear
    • setValue

      public void setValue​(java.lang.Object value)
      Sets the value to edit. The value provided must be a Number.
      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 a VerifyEvent 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)