javax.swing.text

Class InternationalFormatter

Implemented Interfaces:
Cloneable, Serializable
Known Direct Subclasses:
DateFormatter, NumberFormatter

public class InternationalFormatter
extends DefaultFormatter

This extends DefaultFormatter so that the value to string conversion is done via a Format object. This allows various additional formats to be handled by JFormattedField.
See Also:
Serialized Form

Constructor Summary

InternationalFormatter()
Creates a new InternationalFormatter with no Format specified.
InternationalFormatter(Format format)
Creates a new InternationalFormatter that uses the specified Format object for value to string conversion.

Method Summary

Object
clone()
This creates and returns a clone of this Formatter.
protected Action[]
getActions()
Returns the Actions that are supported by this Formatter.
Format.Field[]
getFields(int offset)
Returns the Format.Field constants that are associated with the specified position in the text.
Format
getFormat()
Returns the currently used Format object that is used to format the JFormattedField.
Comparable<T>
getMaximum()
Returns the maximal value that is allowed by this Formatter.
Comparable<T>
getMinimum()
Returns the minimal value that is allowed by this Formatter.
void
install(JFormattedTextField ftf)
Installs the formatter on the specified JFormattedTextField.
void
setFormat(Format format)
Sets the Format object that is used to convert values to strings.
void
setMaximum(Comparable<T> maxValue)
Sets the maximum value that is allowed by this Formatter.
void
setMinimum(Comparable<T> minValue)
Sets the minimum value that is allowed by this Formatter.
Object
stringToValue(String string)
Converts a String (from the JFormattedTextField input) to a value.
String
valueToString(Object value)
Converts a value object into a String.

Methods inherited from class javax.swing.text.DefaultFormatter

clone, getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getOverwriteMode, getValueClass, install, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass, stringToValue, valueToString

Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter

clone, getActions, getDocumentFilter, getFormattedTextField, getNavigationFilter, install, invalidEdit, setEditValid, stringToValue, uninstall, valueToString

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

InternationalFormatter

public InternationalFormatter()
Creates a new InternationalFormatter with no Format specified.

InternationalFormatter

public InternationalFormatter(Format format)
Creates a new InternationalFormatter that uses the specified Format object for value to string conversion.
Parameters:
format - the Format object to use for value to string conversion

Method Details

clone

public Object clone()
            throws CloneNotSupportedException
This creates and returns a clone of this Formatter.
Overrides:
clone in interface DefaultFormatter
Returns:
a clone of this formatter
Throws:
CloneNotSupportedException - not thrown here, since cloning is supported

getActions

protected Action[] getActions()
Returns the Actions that are supported by this Formatter.
Overrides:
getActions in interface JFormattedTextField.AbstractFormatter

getFields

public Format.Field[] getFields(int offset)
Returns the Format.Field constants that are associated with the specified position in the text. If offset is not a valid location in the input field, an empty array of fields is returned.
Parameters:
offset - the position in the text from which we want to fetch the fields constants
Returns:
the field values associated with the specified position in the text

getFormat

public Format getFormat()
Returns the currently used Format object that is used to format the JFormattedField.
Returns:
the current Format

getMaximum

public Comparable<T> getMaximum()
Returns the maximal value that is allowed by this Formatter. A null value means that there is no restriction.
Returns:
the maximal value that is allowed by this Formatter or null if there is no restriction

getMinimum

public Comparable<T> getMinimum()
Returns the minimal value that is allowed by this Formatter. A null value means that there is no restriction.
Returns:
the minimal value that is allowed by this Formatter or null if there is no restriction

install

public void install(JFormattedTextField ftf)
Installs the formatter on the specified JFormattedTextField. This method does the following things:
  • Display the value of #valueToString in the JFormattedTextField
  • Install the Actions from #getActions on the JTextField
  • Install the DocumentFilter returned by #getDocumentFilter
  • Install the NavigationFilter returned by #getNavigationFilter
This method is typically not overridden by subclasses. Instead override one of the mentioned methods in order to customize behaviour.
Overrides:
install in interface DefaultFormatter
Parameters:
ftf - the JFormattedTextField in which this formatter is installed

setFormat

public void setFormat(Format format)
Sets the Format object that is used to convert values to strings.
Parameters:
format - the Format to use for value to string conversion
See Also:
Format

setMaximum

public void setMaximum(Comparable<T> maxValue)
Sets the maximum value that is allowed by this Formatter. The maximum value is given as an object that implements the Comparable interface. If maxValue is null, then the Formatter has no restrictions at the upper end. If value class is not yet specified and maxValue is not null, then valueClass is set to the class of the maximum value.
Parameters:
maxValue - the maximum permissable value
See Also:
Comparable

setMinimum

public void setMinimum(Comparable<T> minValue)
Sets the minimum value that is allowed by this Formatter. The minimum value is given as an object that implements the Comparable interface. If minValue is null, then the Formatter has no restrictions at the lower end. If value class is not yet specified and minValue is not null, then valueClass is set to the class of the minimum value.
Parameters:
minValue - the minimum permissable value
See Also:
Comparable

stringToValue

public Object stringToValue(String string)
            throws ParseException
Converts a String (from the JFormattedTextField input) to a value. This is achieved by invoking Format.parseObject(String) on the specified Format object. This implementation differs slightly from DefaultFormatter, it does:
  1. Convert the string to an Object using the Formatter.
  2. If a valueClass has been set, this object is passed to DefaultFormatter.stringToValue(String) so that the value has the correct type. This may or may not work correctly, depending on the implementation of toString() in the value class and if the class implements a constructor that takes one String as argument.
  3. If no ParseException has been thrown so far, we check if the value exceeds either minimum or maximum if one of those has been specified and throw a ParseException if it does.
  4. Return the value.
If no format has been specified, then DefaultFormatter.stringToValue(String) is invoked as fallback.
Overrides:
stringToValue in interface DefaultFormatter
Parameters:
string - the string to convert
Returns:
the value for the string
Throws:
ParseException - if the string cannot be converted into a value object (e.g. invalid input)

valueToString

public String valueToString(Object value)
            throws ParseException
Converts a value object into a String. This is done by invoking Format.format(Object) on the specified Format object. If no format is set, then DefaultFormatter.valueToString(Object) is called as a fallback.
Overrides:
valueToString in interface DefaultFormatter
Parameters:
value - the value to be converted
Returns:
the string representation of the value
Throws:
ParseException - if the value cannot be converted

InternationalFormatter.java -- Copyright (C) 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.