javax.swing.text
Class DefaultFormatter

java.lang.Object
  |
  +--javax.swing.JFormattedTextField.AbstractFormatter
        |
        +--javax.swing.text.DefaultFormatter
All Implemented Interfaces:
Cloneable, Serializable
Direct Known Subclasses:
InternationalFormatter, MaskFormatter

public class DefaultFormatter
extends javax.swing.JFormattedTextField.AbstractFormatter
implements Cloneable, Serializable

Safe: DefaultFormatter formats aribtrary objects. Formatting is done by invoking the toString method. In order to convert the value back to a String, your class must provide a constructor that takes a String argument. If no single argument constructor that takes a String is found, the returned value will be the String passed into stringToValue.

Instances of DefaultFormatter can not be used in multiple instances of JFormattedTextField. To obtain a copy of an already configured DefaultFormatter, use the clone method.

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.

Since:
1.4
Version:
1.7 03/20/02
See Also:
javax.swing.JFormattedTextField.AbstractFormatter, Serialized Form

Field Summary
private  boolean allowsInvalid
          Indicates if the value being edited must match the mask.
private  boolean commitOnEdit
          If true, any time a valid edit happens commitEdit is invoked.
private  DocumentFilter documentFilter
          DocumentFilter that forwards calls back to DefaultFormatter.
private  NavigationFilter navigationFilter
          NavigationFilter that forwards calls back to DefaultFormatter.
private  boolean overwriteMode
          If true, editing mode is in overwrite (or strikethough).
(package private)  javax.swing.text.DefaultFormatter.ReplaceHolder replaceHolder
          Used during replace to track the region to replace.
private  Class valueClass
          Class used to create new instances.
 
Fields inherited from class javax.swing.AbstractFormatter
 
Constructor Summary
DefaultFormatter()
          Enabled: Creates a DefaultFormatter.
 
Method Summary
(package private)  boolean canReplace(javax.swing.text.DefaultFormatter.ReplaceHolder rh)
          Returns true if the edit described by rh will result in a legal value.
 Object clone()
          Suppressed: Creates a copy of the DefaultFormatter.
(package private)  void commitEdit()
          Invokes commitEdit on the JFormattedTextField.
 boolean getAllowsInvalid()
          Enabled: Returns whether or not the value being edited is allowed to be invalid for a length of time.
 boolean getCommitsOnValidEdit()
          Enabled: Returns when edits are published back to the JFormattedTextField.
protected  DocumentFilter getDocumentFilter()
          Returns the DocumentFilter used to restrict the characters that can be input into the JFormattedTextField.
(package private)  int getInitialVisualPosition()
          Returns the initial location to position the cursor at.
protected  NavigationFilter getNavigationFilter()
          Returns the NavigationFilter used to restrict where the cursor can be placed.
(package private)  int getNextCursorPosition(int offset, int direction)
          Returns the next cursor position from offset by incrementing direction.
private  int getNextNavigatableChar(int offset, int direction)
          Returns the next editable character starting at offset incrementing the offset by direction.
(package private)  int getNextVisualPositionFrom(JTextComponent text, int pos, javax.swing.text.Position.Bias bias, int direction, javax.swing.text.Position.Bias[] biasRet)
          Finds the next navigatable character.
 boolean getOverwriteMode()
          Enabled: Returns the behavior when inserting characters.
(package private)  javax.swing.text.DefaultFormatter.ReplaceHolder getReplaceHolder(javax.swing.text.DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
          Returns the ReplaceHolder to track the replace of the specified text.
(package private)  String getReplaceString(int offset, int deleteLength, String replaceString)
          A convenience methods to return the result of deleting deleteLength characters at offset and inserting replaceString at offset in the current text field.
 Class getValueClass()
          Suppressed: Returns that class that is used to create new Objects.
 void install(JFormattedTextField ftf)
          Enabled: Installs the DefaultFormatter onto a particular JFormattedTextField.
(package private)  boolean isLegalInsertText(String text)
          Returns true if the text in text can be inserted.
(package private)  boolean isNavigatable(int offset)
          Subclasses should override this if they want cursor navigation to skip certain characters.
(package private)  boolean isValidEdit(javax.swing.text.DefaultFormatter.ReplaceHolder rh)
           
(package private)  void moveDot(javax.swing.text.NavigationFilter.FilterBypass fb, int dot, javax.swing.text.Position.Bias bias)
          NavigationFilter method, subclasses that wish finer control should override this.
(package private)  void positionCursorAtInitialLocation()
          Positions the cursor at the initial location.
(package private)  boolean replace(javax.swing.text.DefaultFormatter.ReplaceHolder rh)
          If the edit described by rh is legal, this will return true, commit the edit (if necessary) and update the cursor position.
(package private)  void replace(javax.swing.text.DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
          DocumentFilter method, funnels into replace.
(package private)  void repositionCursor(int offset, int direction)
          Resets the cursor by using getNextCursorPosition.
 void setAllowsInvalid(boolean allowsInvalid)
          Enabled: Sets whether or not the value being edited is allowed to be invalid for a length of time (that is, stringToValue throws a ParseException).
 void setCommitsOnValidEdit(boolean commit)
          Enabled: Sets when edits are published back to the JFormattedTextField.
(package private)  void setDot(javax.swing.text.NavigationFilter.FilterBypass fb, int dot, javax.swing.text.Position.Bias bias)
          NavigationFilter method, subclasses that wish finer control should override this.
 void setOverwriteMode(boolean overwriteMode)
          Enabled: Configures the behavior when inserting characters.
 void setValueClass(Class valueClass)
          Suppressed: Sets that class that is used to create new Objects.
 Object stringToValue(String string)
          Suppressed: Converts the passed in String into an instance of getValueClass by way of the constructor that takes a String argument.
(package private)  void updateValue()
          Pushes the value to the JFormattedTextField if the current value is valid and invokes setEditValid based on the validity of the value.
(package private)  void updateValue(Object value)
          Pushes the value to the editor if we are to commit on edits.
 String valueToString(Object value)
          Suppressed: Converts the passed in Object into a String by way of the toString method.
 
Methods inherited from class javax.swing.AbstractFormatter
getActions, getFormattedTextField, invalidEdit, setEditValid, uninstall
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

allowsInvalid

private boolean allowsInvalid
Indicates if the value being edited must match the mask.


overwriteMode

private boolean overwriteMode
If true, editing mode is in overwrite (or strikethough).


commitOnEdit

private boolean commitOnEdit
If true, any time a valid edit happens commitEdit is invoked.


valueClass

private Class valueClass
Class used to create new instances.


navigationFilter

private NavigationFilter navigationFilter
NavigationFilter that forwards calls back to DefaultFormatter.


documentFilter

private DocumentFilter documentFilter
DocumentFilter that forwards calls back to DefaultFormatter.


replaceHolder

transient javax.swing.text.DefaultFormatter.ReplaceHolder replaceHolder
Used during replace to track the region to replace.

Constructor Detail

DefaultFormatter

public DefaultFormatter()
Enabled: Creates a DefaultFormatter.

Method Detail

install

public void install(JFormattedTextField ftf)
Enabled: 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.

Overrides:
install in class javax.swing.JFormattedTextField.AbstractFormatter
Parameters:
ftf - JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.

setCommitsOnValidEdit

public void setCommitsOnValidEdit(boolean commit)
Enabled: Sets when edits are published back to the JFormattedTextField. If true, commitEdit is invoked after every valid edit (any time the text is edited). On the other hand, if this is false than the DefaultFormatter does not publish edits back to the JFormattedTextField. As such, the only time the value of the JFormattedTextField will change is when commitEdit is invoked on JFormattedTextField, typically when enter is pressed or focus leaves the JFormattedTextField.

Parameters:
commit - Used to indicate when edits are commited back to the JTextComponent

getCommitsOnValidEdit

public boolean getCommitsOnValidEdit()
Enabled: Returns when edits are published back to the JFormattedTextField.

Returns:
true if edits are commited after evey valid edit

setOverwriteMode

public void setOverwriteMode(boolean overwriteMode)
Enabled: Configures the behavior when inserting characters. If overwriteMode is true (the default), new characters overwrite existing characters in the model.

Parameters:
overwriteMode - Indicates if overwrite or overstrike mode is used

getOverwriteMode

public boolean getOverwriteMode()
Enabled: Returns the behavior when inserting characters.

Returns:
true if newly inserted characters overwrite existing characters

setAllowsInvalid

public void setAllowsInvalid(boolean allowsInvalid)
Enabled: Sets whether or not the value being edited is allowed to be invalid for a length of time (that is, stringToValue throws a ParseException). It is often convenient to allow the user to temporarily input an invalid value.

Parameters:
allowsInvalid - Used to indicate if the edited value must always be valid

getAllowsInvalid

public boolean getAllowsInvalid()
Enabled: Returns whether or not the value being edited is allowed to be invalid for a length of time.

Returns:
false if the edited value must always be valid

setValueClass

public void setValueClass(Class valueClass)
Suppressed: Sets that class that is used to create new Objects. If the passed in class does not have a single argument constructor that takes a String, String values will be used.

Parameters:
valueClass - Class used to construct return value from stringToValue

getValueClass

public Class getValueClass()
Suppressed: Returns that class that is used to create new Objects.

Returns:
Class used to constuct return value from stringToValue

stringToValue

public Object stringToValue(String string)
                     throws java.text.ParseException
Suppressed: Converts the passed in String into an instance of getValueClass by way of the constructor that takes a String argument. If getValueClass returns null, the Class of the current value in the JFormattedTextField will be used. If this is null, a String will be returned. If the constructor thows an exception, a ParseException will be thrown. If there is no single argument String constructor, string will be returned.

Specified by:
stringToValue in class javax.swing.JFormattedTextField.AbstractFormatter
Parameters:
string - String to convert
Returns:
Object representation of text
Throws:
java.text.ParseException - if there is an error in the conversion

valueToString

public String valueToString(Object value)
                     throws java.text.ParseException
Suppressed: Converts the passed in Object into a String by way of the toString method.

Specified by:
valueToString in class javax.swing.JFormattedTextField.AbstractFormatter
Parameters:
value - Value to convert
Returns:
String representation of value
Throws:
java.text.ParseException - if there is an error in the conversion

getDocumentFilter

protected DocumentFilter getDocumentFilter()
Returns the DocumentFilter used to restrict the characters that can be input into the JFormattedTextField.

Overrides:
getDocumentFilter in class javax.swing.JFormattedTextField.AbstractFormatter
Returns:
DocumentFilter to restrict edits

getNavigationFilter

protected NavigationFilter getNavigationFilter()
Returns the NavigationFilter used to restrict where the cursor can be placed. characters that can be input into the JFormattedTextField.

Overrides:
getNavigationFilter in class javax.swing.JFormattedTextField.AbstractFormatter
Returns:
NavigationFilter to restrict navigation

clone

public Object clone()
             throws CloneNotSupportedException
Suppressed: Creates a copy of the DefaultFormatter.

Overrides:
clone in class javax.swing.JFormattedTextField.AbstractFormatter
Returns:
copy of the DefaultFormatter
CloneNotSupportedException

positionCursorAtInitialLocation

void positionCursorAtInitialLocation()
Positions the cursor at the initial location.


getInitialVisualPosition

int getInitialVisualPosition()
Returns the initial location to position the cursor at. This forwards the call to getNextNavigatableChar.


isNavigatable

boolean isNavigatable(int offset)
Subclasses should override this if they want cursor navigation to skip certain characters. A return value of false indicates the character at offset should be skipped when navigating throught the field.


isLegalInsertText

boolean isLegalInsertText(String text)
Returns true if the text in text can be inserted. This does not mean the text will ultimately be inserted, it is used if text can trivially reject certain characters.


getNextNavigatableChar

private int getNextNavigatableChar(int offset,
                                   int direction)
Returns the next editable character starting at offset incrementing the offset by direction.


getReplaceString

String getReplaceString(int offset,
                        int deleteLength,
                        String replaceString)
A convenience methods to return the result of deleting deleteLength characters at offset and inserting replaceString at offset in the current text field.


isValidEdit

boolean isValidEdit(javax.swing.text.DefaultFormatter.ReplaceHolder rh)

commitEdit

void commitEdit()
          throws java.text.ParseException
Invokes commitEdit on the JFormattedTextField.

java.text.ParseException

updateValue

void updateValue()
Pushes the value to the JFormattedTextField if the current value is valid and invokes setEditValid based on the validity of the value.


updateValue

void updateValue(Object value)
Pushes the value to the editor if we are to commit on edits. If value is null, the current value will be obtained from the text component.


getNextCursorPosition

int getNextCursorPosition(int offset,
                          int direction)
Returns the next cursor position from offset by incrementing direction. This uses getNextNavigatableChar as well as constraining the location to the max position.


repositionCursor

void repositionCursor(int offset,
                      int direction)
Resets the cursor by using getNextCursorPosition.


getNextVisualPositionFrom

int getNextVisualPositionFrom(JTextComponent text,
                              int pos,
                              javax.swing.text.Position.Bias bias,
                              int direction,
                              javax.swing.text.Position.Bias[] biasRet)
                        throws BadLocationException
Finds the next navigatable character.

BadLocationException

canReplace

boolean canReplace(javax.swing.text.DefaultFormatter.ReplaceHolder rh)
Returns true if the edit described by rh will result in a legal value.


replace

void replace(javax.swing.text.DocumentFilter.FilterBypass fb,
             int offset,
             int length,
             String text,
             AttributeSet attrs)
       throws BadLocationException
DocumentFilter method, funnels into replace.

BadLocationException

replace

boolean replace(javax.swing.text.DefaultFormatter.ReplaceHolder rh)
          throws BadLocationException
If the edit described by rh is legal, this will return true, commit the edit (if necessary) and update the cursor position. This forwards to canReplace and isLegalInsertText as necessary to determine if the edit is in fact legal.

All of the DocumentFilter methods funnel into here, you should generally only have to override this.

BadLocationException

setDot

void setDot(javax.swing.text.NavigationFilter.FilterBypass fb,
            int dot,
            javax.swing.text.Position.Bias bias)
NavigationFilter method, subclasses that wish finer control should override this.


moveDot

void moveDot(javax.swing.text.NavigationFilter.FilterBypass fb,
             int dot,
             javax.swing.text.Position.Bias bias)
NavigationFilter method, subclasses that wish finer control should override this.


getReplaceHolder

javax.swing.text.DefaultFormatter.ReplaceHolder getReplaceHolder(javax.swing.text.DocumentFilter.FilterBypass fb,
                                                                 int offset,
                                                                 int length,
                                                                 String text,
                                                                 AttributeSet attrs)
Returns the ReplaceHolder to track the replace of the specified text.



comments?