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:
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.
Letter Date or Time Part Examples yYear 2006;06MMonth in year 07dDay in month 10HHour in day (0-23) 0hHour in am/pm (1-12) 12mMinute in hour 30sSecond in minute 55SMillisecond 978aAm/pm marker PM
Display Patterns
Display patterns are associated to aSimpleDateFormat 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>cachedPatternsCache of patterns by locale ISO3 codesprotected java.util.CalendarcalendarCalendar containing the current valueprotected intfieldCountNumber of fields in edit patternprotected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[]fieldsFields descriptionsprotected org.eclipse.swt.events.FocusListenerflistenerFocus listener on the Text widgetprotected java.lang.StringBufferinputCacheCurrent edited valueprotected java.lang.StringBufferinputMaskInput maskprotected org.eclipse.swt.events.KeyListenerklistenerKey listener on the Text widgetprotected java.util.LocalelocaleThe Locale used by this formatterprotected org.eclipse.swt.widgets.ListenermodifyFilterFilter for modify eventsprotected java.text.SimpleDateFormatsdfDisplayDate formatter for displayprotected intyearStartYear limit for 2 digits year fieldFields 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 localeDateTimeFormatter(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 voiddetach()Called when the formatter is replaced by an other one in theFormattedTextcontrol.java.lang.StringgetDefaultEditPattern(java.util.Locale loc)Returns the default edit pattern for the givenLocale.java.lang.StringgetDisplayString()Returns the current value formatted for display.java.lang.StringgetEditString()Returns the current value formatted for input.java.util.LocalegetLocale()Returns the current Locale used by this formatter.java.lang.ObjectgetValue()Returns the current value of the text control if it is a validDate.
The date is valid if all the input fields are set.java.lang.Class<java.util.Date>getValueType()Returns the type of value thisITextFormatterhandles, i.e. returns ingetValue().
A DateTimeFormatter always returns a Date value.booleanisEmpty()Returnstrueif current edited value is empty, else returnsfalse.
For a datetime, the value is considered empty if each field composing the datetime pattern contains an empty string.booleanisValid()Returnstrueif current edited value is valid, else returnsfalse.protected voidisValidCharPattern(char c)Checks if a given char is valid for the edit pattern.voidsetLocale(java.util.Locale loc)Sets a newLocaleon this formatter.voidsetText(org.eclipse.swt.widgets.Text text)Sets theTextwidget that will be managed by this formatter.voidsetTimeZone(java.util.TimeZone zone)Sets the time zone with the given time zone value.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, setIgnore, updateText, updateText
-
Field Details
-
cachedPatterns
protected static java.util.Hashtable<java.lang.String,java.lang.String> cachedPatternsCache of patterns by locale ISO3 codes -
calendar
protected java.util.Calendar calendarCalendar containing the current value -
sdfDisplay
protected java.text.SimpleDateFormat sdfDisplayDate formatter for display -
inputMask
protected java.lang.StringBuffer inputMaskInput mask -
inputCache
protected java.lang.StringBuffer inputCacheCurrent edited value -
fields
protected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[] fieldsFields descriptions -
fieldCount
protected int fieldCountNumber of fields in edit pattern -
yearStart
protected int yearStartYear limit for 2 digits year field -
klistener
protected org.eclipse.swt.events.KeyListener klistenerKey listener on the Text widget -
flistener
protected org.eclipse.swt.events.FocusListener flistenerFocus listener on the Text widget -
modifyFilter
protected org.eclipse.swt.widgets.Listener modifyFilterFilter for modify events -
locale
protected java.util.Locale localeThe 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 maskloc- 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 maskdisplayPattern- 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 maskdisplayPattern- display maskloc- locale
-
-
Method Details
-
detach
public void detach()Called when the formatter is replaced by an other one in theFormattedTextcontrol. Allow to release resources like additional listeners.Removes the
KeyListeneron the text widget.- Specified by:
detachin interfaceITextFormatter- Overrides:
detachin classAbstractFormatter- See Also:
ITextFormatter.detach()
-
getDefaultEditPattern
public java.lang.String getDefaultEditPattern(java.util.Locale loc)Returns the default edit pattern for the givenLocale.A
DateFormatobject is instantiated with SHORT format for both date and time parts for the given locale. The corresponding pattern string is then retrieved by calling thetoPattern.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 byFormattedTextwhen theTextwidget looses focus. The displayed value is the result of formatting on thecalendarwith aSimpleDateFormatfor 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 byFormattedTextwhen theTextwidget 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 validDate.
The date is valid if all the input fields are set. If invalid, returnsnull.- Returns:
- current date value if valid,
nullelse - See Also:
ITextFormatter.getValue()
-
getValueType
public java.lang.Class<java.util.Date> getValueType()Returns the type of value thisITextFormatterhandles, i.e. returns ingetValue().
A DateTimeFormatter always returns a Date value.- Returns:
- The value type.
-
isEmpty
public boolean isEmpty()Returnstrueif current edited value is empty, else returnsfalse.
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()Returnstrueif current edited value is valid, else returnsfalse. 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 newLocaleon this formatter.- Parameters:
loc- locale
-
setText
public void setText(org.eclipse.swt.widgets.Text text)Sets theTextwidget that will be managed by this formatter.The ancestor is override to add a key listener on the text widget.
- Specified by:
setTextin interfaceITextFormatter- Overrides:
setTextin classAbstractFormatter- 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 theCalendarused as value cache, and theSimpleDateFormatused 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 aDate.- 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 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)
-