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 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
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>
cachedPatterns
Cache of patterns by locale ISO3 codesprotected java.util.Calendar
calendar
Calendar containing the current valueprotected int
fieldCount
Number of fields in edit patternprotected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[]
fields
Fields descriptionsprotected org.eclipse.swt.events.FocusListener
flistener
Focus listener on the Text widgetprotected java.lang.StringBuffer
inputCache
Current edited valueprotected java.lang.StringBuffer
inputMask
Input maskprotected org.eclipse.swt.events.KeyListener
klistener
Key listener on the Text widgetprotected java.util.Locale
locale
The Locale used by this formatterprotected org.eclipse.swt.widgets.Listener
modifyFilter
Filter for modify eventsprotected java.text.SimpleDateFormat
sdfDisplay
Date formatter for displayprotected int
yearStart
Year 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 void
detach()
Called when the formatter is replaced by an other one in theFormattedText
control.java.lang.String
getDefaultEditPattern(java.util.Locale loc)
Returns the default edit pattern for the givenLocale
.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 validDate
.
The date is valid if all the input fields are set.java.lang.Class<java.util.Date>
getValueType()
Returns the type of value thisITextFormatter
handles, i.e. returns ingetValue()
.
A DateTimeFormatter always returns a Date value.boolean
isEmpty()
Returnstrue
if 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.boolean
isValid()
Returnstrue
if current edited value is valid, else returnsfalse
.protected void
isValidCharPattern(char c)
Checks if a given char is valid for the edit pattern.void
setLocale(java.util.Locale loc)
Sets a newLocale
on this formatter.void
setText(org.eclipse.swt.widgets.Text text)
Sets theText
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 aVerifyEvent
sent 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 theFormattedText
control. Allow to release resources like additional listeners.Removes the
KeyListener
on the text widget.- Specified by:
detach
in interfaceITextFormatter
- Overrides:
detach
in classAbstractFormatter
- See Also:
ITextFormatter.detach()
-
getDefaultEditPattern
public java.lang.String getDefaultEditPattern(java.util.Locale loc)Returns the default edit pattern for the givenLocale
.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 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 byFormattedText
when theText
widget looses focus. The displayed value is the result of formatting on thecalendar
with aSimpleDateFormat
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 byFormattedText
when theText
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 validDate
.
The date is valid if all the input fields are set. If invalid, returnsnull
.- 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 thisITextFormatter
handles, i.e. returns ingetValue()
.
A DateTimeFormatter always returns a Date value.- Returns:
- The value type.
-
isEmpty
public boolean isEmpty()Returnstrue
if 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()Returnstrue
if 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 newLocale
on this formatter.- Parameters:
loc
- locale
-
setText
public void setText(org.eclipse.swt.widgets.Text text)Sets theText
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 interfaceITextFormatter
- Overrides:
setText
in 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 theCalendar
used as value cache, and theSimpleDateFormat
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 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 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)
-