public class DateTimeFormatter extends AbstractFormatter
Date
values in a
FormattedText
. Supports a subset of date and time patterns
defined in SimpleDateFormat
for input.
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
SimpleDateFormat
object.
So they have to be compatible with it.
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".Modifier and Type | Field and 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
|
EMPTY, ignore, SPACE, text
Constructor and 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.util.Locale loc)
Constructs a new instance with default edit and display masks for the given
locale.
|
DateTimeFormatter(java.lang.String editPattern)
Constructs a new instance with the given edit mask.
|
DateTimeFormatter(java.lang.String editPattern,
java.util.Locale loc)
Constructs a new instance with the given edit mask and locale.
|
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.
|
Modifier and Type | Method and 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. |
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. |
beep, setIgnore, updateText, updateText
protected static java.util.Hashtable<java.lang.String,java.lang.String> cachedPatterns
protected java.util.Calendar calendar
protected java.text.SimpleDateFormat sdfDisplay
protected java.lang.StringBuffer inputMask
protected java.lang.StringBuffer inputCache
protected org.eclipse.nebula.widgets.formattedtext.DateTimeFormatter.FieldDesc[] fields
protected int fieldCount
protected int yearStart
protected org.eclipse.swt.events.KeyListener klistener
protected org.eclipse.swt.events.FocusListener flistener
protected org.eclipse.swt.widgets.Listener modifyFilter
protected java.util.Locale locale
public DateTimeFormatter()
public DateTimeFormatter(java.util.Locale loc)
loc
- localepublic DateTimeFormatter(java.lang.String editPattern)
editPattern
- edit maskpublic DateTimeFormatter(java.lang.String editPattern, java.util.Locale loc)
editPattern
- edit maskloc
- localepublic DateTimeFormatter(java.lang.String editPattern, java.lang.String displayPattern)
editPattern
- edit maskdisplayPattern
- display maskpublic DateTimeFormatter(java.lang.String editPattern, java.lang.String displayPattern, java.util.Locale loc)
editPattern
- edit maskdisplayPattern
- display maskloc
- localepublic void detach()
FormattedText
control. Allow to release resources like additional listeners.
Removes the KeyListener
on the text widget.
detach
in interface ITextFormatter
detach
in class AbstractFormatter
ITextFormatter.detach()
public java.lang.String getDefaultEditPattern(java.util.Locale loc)
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.
loc
- localepublic java.lang.String getDisplayString()
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.
ITextFormatter.getDisplayString()
public java.lang.String getEditString()
FormattedText
when the Text
widget gains focus.
The value returned is the content of the StringBuilder used as cache.ITextFormatter.getEditString()
public java.util.Locale getLocale()
public java.lang.Object getValue()
Date
.null
.null
elseITextFormatter.getValue()
public java.lang.Class<java.util.Date> getValueType()
ITextFormatter
handles,
i.e. returns in getValue()
.public boolean isEmpty()
true
if current edited value is empty, else returns
false
.public boolean isValid()
true
if current edited value is valid, else returns
false
. An empty value is considered as invalid.ITextFormatter.isValid()
protected void isValidCharPattern(char c)
c
- pattern charjava.lang.IllegalArgumentException
- if not validpublic void setLocale(java.util.Locale loc)
Locale
on this formatter.loc
- localepublic void setText(org.eclipse.swt.widgets.Text text)
Text
widget that will be managed by this formatter.The ancestor is override to add a key listener on the text widget.
setText
in interface ITextFormatter
setText
in class AbstractFormatter
text
- Text widgetITextFormatter.setText(Text)
public void setTimeZone(java.util.TimeZone zone)
Calendar
used as value cache, and the
SimpleDateFormat
used for display mask.zone
- Time zonepublic void setValue(java.lang.Object value)
Date
.value
- date valuejava.lang.IllegalArgumentException
- if not a dateITextFormatter.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)