Class DateTimeFormatter

java.lang.Object
org.eclipse.nebula.widgets.formattedtext.AbstractFormatter
org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter
All Implemented Interfaces:
java.util.EventListener, ITextFormatter, org.eclipse.swt.events.VerifyListener, org.eclipse.swt.internal.SWTEventListener
Direct Known Subclasses:
DateFormatter, TimeFormatter

public class DateTimeFormatter
extends AbstractFormatter
This class provides formatting of Date values in a FormattedText. Supports a subset of date and time patterns defined in SimpleDateFormat for input.

Edit Patterns

Edit patterns are composed of letters defining the parts of the mask, and characters defining the separators.

The following pattern letters are defined:

Letter Date or Time Part Examples
y Year 2006; 06
M Month in year 07
d Day in month 10
H Hour in day (0-23) 0
h Hour in am/pm (1-12) 12
m Minute in hour 30
s Second in minute 55
S Millisecond 978
a Am/pm marker PM
Edit patterns are limited to numeric formats (except am/pm marker). Variable length fields and separators composed of more than one character are supported for input.

Display Patterns

Display patterns are associated to a SimpleDateFormat object. So they have to be compatible with it.

Examples

  • new DateTimeFormatter("MM/dd/yyyy") - 8 jul 2006 will edit and display as "07/08/2006".
  • new DateTimeFormatter("d/M/yyyy H:m, "dd MMM yyyy HH:mm")- 8 jul 2006, 15:05 will edit as "8/7/2006 15:5" and display as "08 Jul 2006 15:05".
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected static java.util.Hashtable<java.lang.String,​java.lang.String> cachedPatterns
    Cache of patterns by locale ISO3 codes
    protected java.util.Calendar calendar
    Calendar containing the current value
    protected int fieldCount
    Number of fields in edit pattern
    protected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[] fields
    Fields descriptions
    protected org.eclipse.swt.events.FocusListener flistener
    Focus listener on the Text widget
    protected java.lang.StringBuffer inputCache
    Current edited value
    protected java.lang.StringBuffer inputMask
    Input mask
    protected org.eclipse.swt.events.KeyListener klistener
    Key listener on the Text widget
    protected java.util.Locale locale
    The Locale used by this formatter
    protected org.eclipse.swt.widgets.Listener modifyFilter
    Filter for modify events
    protected java.text.SimpleDateFormat sdfDisplay
    Date formatter for display
    protected int yearStart
    Year limit for 2 digits year field

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

    EMPTY, ignore, SPACE, text
  • Constructor Summary

    Constructors 
    Constructor Description
    DateTimeFormatter()
    Constructs a new instance with all defaults : edit mask in SHORT format for both date and time parts for the default locale display mask identical to the edit mask default locale
    DateTimeFormatter​(java.lang.String editPattern)
    Constructs a new instance with the given edit mask.
    DateTimeFormatter​(java.lang.String editPattern, java.lang.String displayPattern)
    Constructs a new instance with the given edit and display masks.
    DateTimeFormatter​(java.lang.String editPattern, java.lang.String displayPattern, java.util.Locale loc)
    Constructs a new instance with the given masks and locale.
    DateTimeFormatter​(java.lang.String editPattern, java.util.Locale loc)
    Constructs a new instance with the given edit mask and locale.
    DateTimeFormatter​(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
    void detach()
    Called when the formatter is replaced by an other one in the FormattedText control.
    java.lang.String getDefaultEditPattern​(java.util.Locale loc)
    Returns the default edit pattern for the given Locale.
    java.lang.String getDisplayString()
    Returns the current value formatted for display.
    java.lang.String getEditString()
    Returns the current value formatted for input.
    java.util.Locale getLocale()
    Returns the current Locale used by this formatter.
    java.lang.Object getValue()
    Returns the current value of the text control if it is a valid Date.
    The date is valid if all the input fields are set.
    java.lang.Class<java.util.Date> getValueType()
    Returns the type of value this ITextFormatter handles, i.e. returns in getValue().
    A DateTimeFormatter always returns a Date value.
    boolean isEmpty()
    Returns true if current edited value is empty, else returns false.
    For a datetime, the value is considered empty if each field composing the datetime pattern contains an empty string.
    boolean isValid()
    Returns true if current edited value is valid, else returns false.
    protected void isValidCharPattern​(char c)
    Checks if a given char is valid for the edit pattern.
    void setLocale​(java.util.Locale loc)
    Sets a new Locale on this formatter.
    void setText​(org.eclipse.swt.widgets.Text text)
    Sets the Text widget that will be managed by this formatter.
    void setTimeZone​(java.util.TimeZone zone)
    Sets the time zone with the given time zone value.
    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, setIgnore, 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.lang.String,​java.lang.String> cachedPatterns
      Cache of patterns by locale ISO3 codes
    • calendar

      protected java.util.Calendar calendar
      Calendar containing the current value
    • sdfDisplay

      protected java.text.SimpleDateFormat sdfDisplay
      Date formatter for display
    • inputMask

      protected java.lang.StringBuffer inputMask
      Input mask
    • inputCache

      protected java.lang.StringBuffer inputCache
      Current edited value
    • fields

      protected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[] fields
      Fields descriptions
    • fieldCount

      protected int fieldCount
      Number of fields in edit pattern
    • yearStart

      protected int yearStart
      Year limit for 2 digits year field
    • klistener

      protected org.eclipse.swt.events.KeyListener klistener
      Key listener on the Text widget
    • flistener

      protected org.eclipse.swt.events.FocusListener flistener
      Focus listener on the Text widget
    • modifyFilter

      protected org.eclipse.swt.widgets.Listener modifyFilter
      Filter for modify events
    • locale

      protected java.util.Locale locale
      The Locale used by this formatter
  • Constructor Details

    • DateTimeFormatter

      public DateTimeFormatter()
      Constructs a new instance with all defaults :
      • edit mask in SHORT format for both date and time parts for the default locale
      • display mask identical to the edit mask
      • default locale
    • DateTimeFormatter

      public DateTimeFormatter​(java.util.Locale loc)
      Constructs a new instance with default edit and display masks for the given locale.
      Parameters:
      loc - locale
    • DateTimeFormatter

      public DateTimeFormatter​(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
    • DateTimeFormatter

      public DateTimeFormatter​(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
    • DateTimeFormatter

      public DateTimeFormatter​(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
    • DateTimeFormatter

      public DateTimeFormatter​(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

    • detach

      public void detach()
      Called when the formatter is replaced by an other one in the FormattedText control. Allow to release resources like additional listeners.

      Removes the KeyListener on the text widget.

      Specified by:
      detach in interface ITextFormatter
      Overrides:
      detach in class AbstractFormatter
      See Also:
      ITextFormatter.detach()
    • getDefaultEditPattern

      public java.lang.String getDefaultEditPattern​(java.util.Locale loc)
      Returns the default edit pattern for the given Locale.

      A DateFormat object is instantiated with SHORT format for both date and time parts for the given locale. The corresponding pattern string is then retrieved by calling the toPattern.

      Default patterns are stored in a cache with ISO3 language and country codes as key. So they are computed only once by locale.

      Parameters:
      loc - locale
      Returns:
      edit pattern for the locale
    • 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 calendar with a SimpleDateFormat for the display pattern passed in constructor. In case the input is invalid (eg. blanks fields), the edit string is returned in place of the display string.
      Returns:
      display string if valid, edit string else
      See Also:
      ITextFormatter.getDisplayString()
    • getEditString

      public java.lang.String getEditString()
      Returns the current value formatted for input. This method is called by FormattedText when the Text widget gains focus. The value returned is the content of the StringBuilder used as cache.
      Returns:
      edit string
      See Also:
      ITextFormatter.getEditString()
    • getLocale

      public java.util.Locale getLocale()
      Returns the current Locale used by this formatter.
      Returns:
      Current Locale used
    • getValue

      public java.lang.Object getValue()
      Returns the current value of the text control if it is a valid Date.
      The date is valid if all the input fields are set. If invalid, returns null.
      Returns:
      current date value if valid, null else
      See Also:
      ITextFormatter.getValue()
    • getValueType

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

      public boolean isEmpty()
      Returns true if current edited value is empty, else returns false.
      For a datetime, the value is considered empty if each field composing the datetime pattern contains an empty string.
      Returns:
      true if empty, else false
    • isValid

      public boolean isValid()
      Returns true if current edited value is valid, else returns false. An empty value is considered as invalid.
      Returns:
      true if valid, else false
      See Also:
      ITextFormatter.isValid()
    • isValidCharPattern

      protected void isValidCharPattern​(char c)
      Checks if a given char is valid for the edit pattern. This method must be override to restrict the edit pattern in subclasses.
      Parameters:
      c - pattern char
      Throws:
      java.lang.IllegalArgumentException - if not valid
    • setLocale

      public void setLocale​(java.util.Locale loc)
      Sets a new Locale on this formatter.
      Parameters:
      loc - locale
    • setText

      public void setText​(org.eclipse.swt.widgets.Text text)
      Sets the Text widget that will be managed by this formatter.

      The ancestor is override to add a key listener on the text widget.

      Specified by:
      setText in interface ITextFormatter
      Overrides:
      setText in class AbstractFormatter
      Parameters:
      text - Text widget
      See Also:
      ITextFormatter.setText(Text)
    • setTimeZone

      public void setTimeZone​(java.util.TimeZone zone)
      Sets the time zone with the given time zone value. The time zone is applied to both the Calendar used as value cache, and the SimpleDateFormat used for display mask.
      Parameters:
      zone - Time zone
    • setValue

      public void setValue​(java.lang.Object value)
      Sets the value to edit. The value provided must be a Date.
      Parameters:
      value - date value
      Throws:
      java.lang.IllegalArgumentException - if not a date
      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)