|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.swing.JFormattedTextField.AbstractFormatter javax.swing.text.DefaultFormatter javax.swing.text.InternationalFormatter
InternationalFormatter
extends DefaultFormatter
,
using an instance of java.text.Format
to handle the
conversion to a String, and the conversion from a String.
If getAllowsInvalid()
is false, this will ask the
Format
to format the current text on every edit.
You can specify a minimum and maximum value by way of the
setMinimum
and setMaximum
methods. In order
for this to work the values returned from stringToValue
must be
comparable to the min/max values by way of the Comparable
interface.
Be careful how you configure the Format
and the
InternationalFormatter
, as it is possible to create a
situation where certain values can not be input. Consider the date
format 'M/d/yy', an InternationalFormatter
that is always
valid (setAllowsInvalid(false)
), is in overwrite mode
(setOverwriteMode(true)
) and the date 7/1/99. In this
case the user will not be able to enter a two digit month or day of
month. To avoid this, the format should be 'MM/dd/yy'.
If InternationalFormatter
is configured to only allow valid
values (setAllowsInvalid(false)
), every valid edit will result
in the text of the JFormattedTextField
being completely reset
from the Format
.
The cursor position will also be adjusted as literal characters are
added/removed from the resulting String.
InternationalFormatter
's behavior of
stringToValue
is slightly different than that of
DefaultTextFormatter
, it does the following:
parseObject
is invoked on the Format
specified by setFormat
setValueClass
),
supers implementation is invoked to convert the value returned
from parseObject
to the appropriate class.
ParseException
has not been thrown, and the value
is outside the min/max a ParseException
is thrown.
InternationalFormatter
implements stringToValue
in this manner so that you can specify an alternate Class than
Format
may return.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans
package.
Please see XMLEncoder
.
Format
,
Comparable
,
Serialized FormNested Class Summary | |
(package private) static class |
InternationalFormatter.ExtendedReplaceHolder
As InternationalFormatter replaces the complete text on every edit, ExtendedReplaceHolder keeps track of the offset and length passed into canReplace. |
private class |
InternationalFormatter.IncrementAction
IncrementAction is used to increment the value by a certain amount. |
Nested classes inherited from class javax.swing.text.DefaultFormatter |
DefaultFormatter.ReplaceHolder |
Field Summary | |
private static Format.Field[] |
EMPTY_FIELD_ARRAY
Used by getFields . |
private Format |
format
Object used to handle the conversion. |
private boolean |
ignoreDocumentMutate
If true, DocumentFilter methods are unconditionally allowed, and no checking is done on their values. |
private AttributedCharacterIterator |
iterator
Used to iterate over characters. |
private BitSet |
literalMask
A bit is set for every index identified in the AttributedCharacterIterator that is not considered decoration. |
private Comparable |
max
Can be used to impose a maximum value. |
private Comparable |
min
Can be used to impose a minimum value. |
private String |
string
Current value being displayed. |
private boolean |
validMask
True if the Format was able to convert the value to a String and back. |
Fields inherited from class javax.swing.text.DefaultFormatter |
replaceHolder |
Fields inherited from class javax.swing.JFormattedTextField.AbstractFormatter |
|
Constructor Summary | |
InternationalFormatter()
Creates an InternationalFormatter with no
Format specified. |
|
InternationalFormatter(Format format)
Creates an InternationalFormatter with the specified
Format instance. |
Method Summary | |
(package private) Object |
adjustValue(Object value,
Map attributes,
Object field,
int direction)
Subclasses supporting incrementing must override this to handle the actual incrementing. |
(package private) boolean |
canIncrement(Object field,
int cursorPosition)
Returns true if field is non-null.
|
(package private) boolean |
canReplace(DefaultFormatter.ReplaceHolder rh)
Overriden in an attempt to honor the literals. |
Object |
clone()
Creates a copy of the DefaultFormatter. |
protected Action[] |
getActions()
If getSupportsIncrement returns true, this returns
two Actions suitable for incrementing/decrementing the value. |
(package private) Object |
getAdjustField(int start,
Map attributes)
Returns the field that will be adjusted by adjustValue. |
(package private) Map |
getAttributes(int index)
Returns a Set of the attribute identifiers at index . |
(package private) int |
getAttributeStart(AttributedCharacterIterator.Attribute id)
Returns the start of the first run that contains the attribute id . |
(package private) char |
getBufferedChar(int index)
Returns the character from the mask that has been buffered at index . |
Format.Field[] |
getFields(int offset)
Returns the Format.Field constants associated with
the text at offset . |
private int |
getFieldTypeCountTo(Object f,
int start)
Returns the number of occurences of f before
the location start in the current
AttributedCharacterIterator . |
Format |
getFormat()
Returns the format that dictates the legal values that can be edited and displayed. |
(package private) AttributedCharacterIterator |
getIterator()
Returns the AttributedCharacterIterator used to
format the last value. |
(package private) char |
getLiteral(int index)
Returns the literal character at index. |
(package private) int |
getLiteralCountTo(int index)
Returns the number of literal characters before index . |
Comparable |
getMaximum()
Returns the maximum permissible value. |
Comparable |
getMinimum()
Returns the minimum permissible value. |
private int |
getNextNonliteralIndex(int index,
int direction)
Returns the index of the next non-literal character starting at index. |
(package private) DefaultFormatter.ReplaceHolder |
getReplaceHolder(DocumentFilter.FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs)
Overriden to return an instance of ExtendedReplaceHolder . |
(package private) boolean |
getSupportsIncrement()
Returns false, indicating InternationalFormatter does not allow incrementing of the value. |
void |
install(JFormattedTextField ftf)
Installs the DefaultFormatter onto a particular
JFormattedTextField .
|
(package private) boolean |
isLiteral(int index)
Returns true if the character at index is a literal, that is not editable. |
(package private) boolean |
isLiteral(Map attributes)
Returns true if attributes is null or empty. |
(package private) boolean |
isNavigatable(int offset)
Returns true if the character at offset is navigatable too. |
(package private) boolean |
isValidMask()
Returns true if the current mask is valid. |
(package private) boolean |
isValidValue(Object value,
boolean wantsCCE)
Returns true if value is between the min/max. |
private void |
readObject(ObjectInputStream s)
Subclassed to update the internal representation of the mask after the default read operation has completed. |
(package private) boolean |
replace(DefaultFormatter.ReplaceHolder rh)
When in ! |
(package private) void |
replace(DocumentFilter.FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs)
Overriden to unconditionally allow the replace if ignoreDocumentMutate is true. |
private void |
repositionCursor(int startLiteralCount,
int end,
int direction)
Repositions the cursor. |
(package private) void |
resetValue(Object value)
Resets the value of the JFormattedTextField to be value . |
(package private) void |
selectField(Object f,
int count)
Selects the fields identified by attributes . |
void |
setFormat(Format format)
Sets the format that dictates the legal values that can be edited and displayed. |
void |
setMaximum(Comparable max)
Sets the maximum permissible value. |
void |
setMinimum(Comparable minimum)
Sets the minimum permissible value. |
Object |
stringToValue(String text)
Returns the Object representation of the
String text . |
(package private) Object |
stringToValue(String text,
Format f)
Invokes parseObject on f , returning
its value. |
(package private) void |
updateMask()
Updates the AttributedCharacterIterator by invoking formatToCharacterIterator on the Format .
|
private void |
updateMask(AttributedCharacterIterator iterator)
Updates the interal bitset from iterator . |
(package private) void |
updateMaskIfNecessary()
Updates the AttributedCharacterIterator and bitset, if necessary. |
(package private) void |
updateValue(Object value)
Overriden to update the mask after invoking supers implementation. |
String |
valueToString(Object value)
Returns a String representation of the Object value .
|
Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter |
getFormattedTextField, invalidEdit, setEditValid, uninstall |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final Format.Field[] EMPTY_FIELD_ARRAY
getFields
.
private Format format
private Comparable max
private Comparable min
private transient BitSet literalMask
private transient AttributedCharacterIterator iterator
private transient boolean validMask
private transient String string
private transient boolean ignoreDocumentMutate
Constructor Detail |
public InternationalFormatter()
InternationalFormatter
with no
Format
specified.
public InternationalFormatter(Format format)
InternationalFormatter
with the specified
Format
instance.
format
- Format instance used for converting from/to StringsMethod Detail |
public void setFormat(Format format)
format
- Format
instance used for converting
from/to Stringspublic Format getFormat()
public void setMinimum(Comparable minimum)
valueClass
has
not been specified, and minimum
is non null, the
valueClass
will be set to that of the class of
minimum
.
minimum
- Minimum legal value that can be inputDefaultFormatter.setValueClass(java.lang.Class)
public Comparable getMinimum()
public void setMaximum(Comparable max)
valueClass
has
not been specified, and max
is non null, the
valueClass
will be set to that of the class of
max
.
max
- Maximum legal value that can be inputDefaultFormatter.setValueClass(java.lang.Class)
public Comparable getMaximum()
public void install(JFormattedTextField ftf)
DefaultFormatter
onto a particular
JFormattedTextField
.
This will invoke valueToString
to convert the
current value from the JFormattedTextField
to
a String. This will then install the Action
s from
getActions
, the DocumentFilter
returned from getDocumentFilter
and the
NavigationFilter
returned from
getNavigationFilter
onto the
JFormattedTextField
.
Subclasses will typically only need to override this if they
wish to install additional listeners on the
JFormattedTextField
.
If there is a ParseException
in converting the
current value to a String, this will set the text to an empty
String, and mark the JFormattedTextField
as being
in an invalid state.
While this is a public method, this is typically only useful
for subclassers of JFormattedTextField
.
JFormattedTextField
will invoke this method at
the appropriate times when the value changes, or its internal
state changes.
install
in class DefaultFormatter
ftf
- JFormattedTextField to format for, may be null indicating
uninstall from current JFormattedTextField.public String valueToString(Object value) throws ParseException
value
.
This invokes format
on the current Format
.
valueToString
in class DefaultFormatter
value
- Value to convert
ParseException
- if there is an error in the conversionpublic Object stringToValue(String text) throws ParseException
Object
representation of the
String
text
.
stringToValue
in class DefaultFormatter
text
- String
to convert
Object
representation of text
ParseException
- if there is an error in the conversionpublic Format.Field[] getFields(int offset)
Format.Field
constants associated with
the text at offset
. If offset
is not
a valid location into the current text, this will return an
empty array.
offset
- offset into text to be examined
public Object clone() throws CloneNotSupportedException
clone
in class DefaultFormatter
CloneNotSupportedException
protected Action[] getActions()
getSupportsIncrement
returns true, this returns
two Actions suitable for incrementing/decrementing the value.
getActions
in class JFormattedTextField.AbstractFormatter
Object stringToValue(String text, Format f) throws ParseException
parseObject
on f
, returning
its value.
ParseException
boolean isValidValue(Object value, boolean wantsCCE)
value
is between the min/max.
wantsCCE
- If false, and a ClassCastException is thrown in
comparing the values, the exception is consumed and
false is returned.Map getAttributes(int index)
index
.
int getAttributeStart(AttributedCharacterIterator.Attribute id)
id
. This will return -1
if the attribute
can not be found.
AttributedCharacterIterator getIterator()
AttributedCharacterIterator
used to
format the last value.
void updateMaskIfNecessary()
void updateMask()
formatToCharacterIterator
on the Format
.
If this is successful,
updateMask(AttributedCharacterIterator)
is then invoked to update the internal bitmask.
int getLiteralCountTo(int index)
index
.
boolean isLiteral(int index)
char getLiteral(int index)
boolean isNavigatable(int offset)
isLiteral
, subclasses
may wish to provide different behavior.
isNavigatable
in class DefaultFormatter
void updateValue(Object value)
updateValue
in class DefaultFormatter
void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
replace
in class DefaultFormatter
BadLocationException
private int getNextNonliteralIndex(int index, int direction)
direction
- Amount to increment looking for non-literalboolean canReplace(DefaultFormatter.ReplaceHolder rh)
If we do not allow invalid values and are in overwrite mode, this does the following for each character in the replacement range:
canReplace
in class DefaultFormatter
boolean replace(DefaultFormatter.ReplaceHolder rh) throws BadLocationException
repositionCursor
to correctly reset the cursor.
replace
in class DefaultFormatter
BadLocationException
private void repositionCursor(int startLiteralCount, int end, int direction)
startLiteralCount
gives
the number of literals to the start of the deleted range, end
gives the ending location to adjust from, direction gives
the direction relative to end
to position the
cursor from.
char getBufferedChar(int index)
index
.
boolean isValidMask()
boolean isLiteral(Map attributes)
attributes
is null or empty.
private void updateMask(AttributedCharacterIterator iterator)
iterator
. This will
set validMask
to true if iterator
is
non-null.
boolean canIncrement(Object field, int cursorPosition)
field
is non-null.
Subclasses that wish to allow incrementing to happen outside of
the known fields will need to override this.
void selectField(Object f, int count)
attributes
.
Object getAdjustField(int start, Map attributes)
private int getFieldTypeCountTo(Object f, int start)
f
before
the location start
in the current
AttributedCharacterIterator
.
Object adjustValue(Object value, Map attributes, Object field, int direction) throws BadLocationException, ParseException
value
is the current value,
attributes
gives the field the cursor is in (may be
null depending upon canIncrement
) and
direction
is the amount to increment by.
BadLocationException
ParseException
boolean getSupportsIncrement()
adjustValue
.
void resetValue(Object value) throws BadLocationException, ParseException
value
.
BadLocationException
ParseException
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
DefaultFormatter.ReplaceHolder getReplaceHolder(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
ExtendedReplaceHolder
.
getReplaceHolder
in class DefaultFormatter
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |