Home » openjdk-7 » javax » swing » text » [javadoc | source]
public class: InternationalFormatter [javadoc | source]

All Implemented Interfaces:
    Cloneable, Serializable

Direct Known Subclasses:
    NumberFormatter, DateFormatter, DateEditorFormatter, NumberEditorFormatter

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:

  1. parseObject is invoked on the Format specified by setFormat
  2. If a Class has been set for the values (setValueClass), supers implementation is invoked to convert the value returned from parseObject to the appropriate class.
  3. If a ParseException has not been thrown, and the value is outside the min/max a ParseException is thrown.
  4. The value is returned.
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 java.beans.XMLEncoder .

Nested Class Summary:
static class  InternationalFormatter.ExtendedReplaceHolder  As InternationalFormatter replaces the complete text on every edit, ExtendedReplaceHolder keeps track of the offset and length passed into canReplace. 
Fields inherited from javax.swing.text.DefaultFormatter:
 public InternationalFormatter() 
 public InternationalFormatter(Format format) 
    Creates an InternationalFormatter with the specified Format instance.
    format - Format instance used for converting from/to Strings
Method from javax.swing.text.InternationalFormatter Summary:
adjustValue,   canIncrement,   canReplace,   clone,   getActions,   getAdjustField,   getAttributeStart,   getAttributes,   getBufferedChar,   getFields,   getFormat,   getIterator,   getLiteral,   getLiteralCountTo,   getMaximum,   getMinimum,   getReplaceHolder,   getSupportsIncrement,   install,   isLiteral,   isLiteral,   isNavigatable,   isValidMask,   isValidValue,   replace,   replace,   resetValue,   selectField,   setFormat,   setMaximum,   setMinimum,   stringToValue,   stringToValue,   updateMask,   updateMaskIfNecessary,   updateValue,   valueToString
Methods from javax.swing.text.DefaultFormatter:
canReplace,   clone,   commitEdit,   getAllowsInvalid,   getCommitsOnValidEdit,   getDocumentFilter,   getInitialVisualPosition,   getNavigationFilter,   getNextCursorPosition,   getNextVisualPositionFrom,   getOverwriteMode,   getReplaceHolder,   getReplaceString,   getValueClass,   install,   isLegalInsertText,   isNavigatable,   isValidEdit,   moveDot,   positionCursorAtInitialLocation,   replace,   replace,   repositionCursor,   setAllowsInvalid,   setCommitsOnValidEdit,   setDot,   setOverwriteMode,   setValueClass,   stringToValue,   updateValue,   updateValue,   valueToString
Methods from javax.swing.JFormattedTextField$AbstractFormatter:
clone,   getActions,   getDocumentFilter,   getFormattedTextField,   getNavigationFilter,   install,   invalidEdit,   setEditValid,   stringToValue,   uninstall,   valueToString
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from javax.swing.text.InternationalFormatter Detail:
 Object adjustValue(Object value,
    Map attributes,
    Object field,
    int direction) throws BadLocationException, ParseException 
    Subclasses supporting incrementing must override this to handle the actual incrementing. 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.
 boolean canIncrement(Object field,
    int cursorPosition) 
    Returns true if field is non-null. Subclasses that wish to allow incrementing to happen outside of the known fields will need to override this.
 boolean canReplace(ReplaceHolder rh) 
    Overriden in an attempt to honor the literals.

    If we do not allow invalid values and are in overwrite mode, this {@code rh.length} is corrected as to preserve trailing literals. If not in overwrite mode, and there is text to insert it is inserted at the next non literal index going forward. If there is only text to remove, it is removed from the next non literal index going backward.

 public Object clone() throws CloneNotSupportedException 
    Creates a copy of the DefaultFormatter.
 protected Action[] getActions() 
    If getSupportsIncrement returns true, this returns two Actions suitable for incrementing/decrementing the value.
 Object getAdjustField(int start,
    Map attributes) 
    Returns the field that will be adjusted by adjustValue.
 int getAttributeStart(Attribute id) 
    Returns the start of the first run that contains the attribute id. This will return -1 if the attribute can not be found.
 Map<Attribute, Object> getAttributes(int index) 
    Returns a Set of the attribute identifiers at index.
 char getBufferedChar(int index) 
    Returns the character from the mask that has been buffered at index.
 public Field[] getFields(int offset) 
    Returns the 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.
 public Format getFormat() 
    Returns the format that dictates the legal values that can be edited and displayed.
 AttributedCharacterIterator getIterator() 
    Returns the AttributedCharacterIterator used to format the last value.
 char getLiteral(int index) 
    Returns the literal character at index.
 int getLiteralCountTo(int index) 
    Returns the number of literal characters before index.
 public Comparable getMaximum() 
    Returns the maximum permissible value.
 public Comparable getMinimum() 
    Returns the minimum permissible value.
 ReplaceHolder getReplaceHolder(FilterBypass fb,
    int offset,
    int length,
    String text,
    AttributeSet attrs) 
    Overriden to return an instance of ExtendedReplaceHolder.
 boolean getSupportsIncrement() 
    Returns false, indicating InternationalFormatter does not allow incrementing of the value. Subclasses that wish to support incrementing/decrementing the value should override this and return true. Subclasses should also override adjustValue.
 public  void install(JFormattedTextField ftf) 
    Installs the 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 Actions 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.

 boolean isLiteral(int index) 
    Returns true if the character at index is a literal, that is not editable.
 boolean isLiteral(Map attributes) 
    Returns true if attributes is null or empty.
 boolean isNavigatable(int offset) 
    Returns true if the character at offset is navigatable too. This is implemented in terms of isLiteral, subclasses may wish to provide different behavior.
 boolean isValidMask() 
    Returns true if the current mask is valid.
 boolean isValidValue(Object value,
    boolean wantsCCE) 
    Returns true if value is between the min/max.
 boolean replace(ReplaceHolder rh) throws BadLocationException 
    When in !allowsInvalid mode the text is reset on every edit, thus supers implementation will position the cursor at the wrong position. As such, this invokes supers implementation and then invokes repositionCursor to correctly reset the cursor.
  void replace(FilterBypass fb,
    int offset,
    int length,
    String text,
    AttributeSet attrs) throws BadLocationException 
    Overriden to unconditionally allow the replace if ignoreDocumentMutate is true.
  void resetValue(Object value) throws BadLocationException, ParseException 
    Resets the value of the JFormattedTextField to be value.
  void selectField(Object f,
    int count) 
    Selects the fields identified by attributes.
 public  void setFormat(Format format) 
    Sets the format that dictates the legal values that can be edited and displayed.
 public  void setMaximum(Comparable max) 
    Sets the maximum permissible value. If the valueClass has not been specified, and max is non null, the valueClass will be set to that of the class of max.
 public  void setMinimum(Comparable minimum) 
    Sets the minimum permissible value. If the valueClass has not been specified, and minimum is non null, the valueClass will be set to that of the class of minimum.
 public Object stringToValue(String text) throws ParseException 
    Returns the Object representation of the String text.
 Object stringToValue(String text,
    Format f) throws ParseException 
    Invokes parseObject on f, returning its value.
  void updateMask() 
    Updates the AttributedCharacterIterator by invoking formatToCharacterIterator on the Format. If this is successful, updateMask(AttributedCharacterIterator) is then invoked to update the internal bitmask.
  void updateMaskIfNecessary() 
    Updates the AttributedCharacterIterator and bitset, if necessary.
  void updateValue(Object value) 
    Overriden to update the mask after invoking supers implementation.
 public String valueToString(Object value) throws ParseException 
    Returns a String representation of the Object value. This invokes format on the current Format.