Methods Summary |
---|
boolean | canReplace(javax.swing.text.DefaultFormatter$ReplaceHolder rh)Returns true if the edit described by rh will result
in a legal value.
return isValidEdit(rh);
|
public java.lang.Object | clone()Creates a copy of the DefaultFormatter.
DefaultFormatter formatter = (DefaultFormatter)super.clone();
formatter.navigationFilter = null;
formatter.documentFilter = null;
formatter.replaceHolder = null;
return formatter;
|
void | commitEdit()Invokes commitEdit on the JFormattedTextField.
JFormattedTextField ftf = getFormattedTextField();
if (ftf != null) {
ftf.commitEdit();
}
|
public boolean | getAllowsInvalid()Returns whether or not the value being edited is allowed to be invalid
for a length of time.
return allowsInvalid;
|
public boolean | getCommitsOnValidEdit()Returns when edits are published back to the
JFormattedTextField .
return commitOnEdit;
|
protected javax.swing.text.DocumentFilter | getDocumentFilter()Returns the DocumentFilter used to restrict the characters
that can be input into the JFormattedTextField .
if (documentFilter == null) {
documentFilter = new DefaultDocumentFilter();
}
return documentFilter;
|
int | getInitialVisualPosition()Returns the initial location to position the cursor at. This forwards
the call to getNextNavigatableChar .
return getNextNavigatableChar(0, 1);
|
protected javax.swing.text.NavigationFilter | getNavigationFilter()Returns the NavigationFilter used to restrict where the
cursor can be placed.
if (navigationFilter == null) {
navigationFilter = new DefaultNavigationFilter();
}
return navigationFilter;
|
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.
int newOffset = getNextNavigatableChar(offset, direction);
int max = getFormattedTextField().getDocument().getLength();
if (!getAllowsInvalid()) {
if (direction == -1 && offset == newOffset) {
// Case where hit backspace and only characters before
// offset are fixed.
newOffset = getNextNavigatableChar(newOffset, 1);
if (newOffset >= max) {
newOffset = offset;
}
}
else if (direction == 1 && newOffset >= max) {
// Don't go beyond last editable character.
newOffset = getNextNavigatableChar(max - 1, -1);
if (newOffset < max) {
newOffset++;
}
}
}
return newOffset;
|
private int | getNextNavigatableChar(int offset, int direction)Returns the next editable character starting at offset incrementing
the offset by direction .
int max = getFormattedTextField().getDocument().getLength();
while (offset >= 0 && offset < max) {
if (isNavigatable(offset)) {
return offset;
}
offset += direction;
}
return offset;
|
int | getNextVisualPositionFrom(javax.swing.text.JTextComponent text, int pos, javax.swing.text.Position$Bias bias, int direction, javax.swing.text.Position$Bias[] biasRet)Finds the next navigatable character.
int value = text.getUI().getNextVisualPositionFrom(text, pos, bias,
direction, biasRet);
if (value == -1) {
return -1;
}
if (!getAllowsInvalid() && (direction == SwingConstants.EAST ||
direction == SwingConstants.WEST)) {
int last = -1;
while (!isNavigatable(value) && value != last) {
last = value;
value = text.getUI().getNextVisualPositionFrom(
text, value, bias, direction,biasRet);
}
int max = getFormattedTextField().getDocument().getLength();
if (last == value || value == max) {
if (value == 0) {
biasRet[0] = Position.Bias.Forward;
value = getInitialVisualPosition();
}
if (value >= max && max > 0) {
// Pending: should not assume forward!
biasRet[0] = Position.Bias.Forward;
value = getNextNavigatableChar(max - 1, -1) + 1;
}
}
}
return value;
|
public boolean | getOverwriteMode()Returns the behavior when inserting characters.
return overwriteMode;
|
javax.swing.text.DefaultFormatter$ReplaceHolder | getReplaceHolder(javax.swing.text.DocumentFilter$FilterBypass fb, int offset, int length, java.lang.String text, javax.swing.text.AttributeSet attrs)Returns the ReplaceHolder to track the replace of the specified
text.
if (replaceHolder == null) {
replaceHolder = new ReplaceHolder();
}
replaceHolder.reset(fb, offset, length, text, attrs);
return replaceHolder;
|
java.lang.String | getReplaceString(int offset, int deleteLength, java.lang.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.
String string = getFormattedTextField().getText();
String result;
result = string.substring(0, offset);
if (replaceString != null) {
result += replaceString;
}
if (offset + deleteLength < string.length()) {
result += string.substring(offset + deleteLength);
}
return result;
|
public java.lang.Class | getValueClass()Returns that class that is used to create new Objects.
return valueClass;
|
public void | install(javax.swing.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 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.
super.install(ftf);
positionCursorAtInitialLocation();
|
boolean | isLegalInsertText(java.lang.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.
return true;
|
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.
return true;
|
boolean | isValidEdit(javax.swing.text.DefaultFormatter$ReplaceHolder rh)
if (!getAllowsInvalid()) {
String newString = getReplaceString(rh.offset, rh.length, rh.text);
try {
rh.value = stringToValue(newString);
return true;
} catch (ParseException pe) {
return false;
}
}
return true;
|
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.
fb.moveDot(dot, bias);
|
void | positionCursorAtInitialLocation()Positions the cursor at the initial location.
JFormattedTextField ftf = getFormattedTextField();
if (ftf != null) {
ftf.setCaretPosition(getInitialVisualPosition());
}
|
void | replace(javax.swing.text.DocumentFilter$FilterBypass fb, int offset, int length, java.lang.String text, javax.swing.text.AttributeSet attrs)DocumentFilter method, funnels into replace .
ReplaceHolder rh = getReplaceHolder(fb, offset, length, text, attrs);
replace(rh);
|
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. 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.
boolean valid = true;
int direction = 1;
if (rh.length > 0 && (rh.text == null || rh.text.length() == 0) &&
(getFormattedTextField().getSelectionStart() != rh.offset ||
rh.length > 1)) {
direction = -1;
}
if (getOverwriteMode() && rh.text != null) {
rh.length = Math.min(Math.max(rh.length, rh.text.length()),
rh.fb.getDocument().getLength() - rh.offset);
}
if ((rh.text != null && !isLegalInsertText(rh.text)) ||
!canReplace(rh) ||
(rh.length == 0 && (rh.text == null || rh.text.length() == 0))) {
valid = false;
}
if (valid) {
int cursor = rh.cursorPosition;
rh.fb.replace(rh.offset, rh.length, rh.text, rh.attrs);
if (cursor == -1) {
cursor = rh.offset;
if (direction == 1 && rh.text != null) {
cursor = rh.offset + rh.text.length();
}
}
updateValue(rh.value);
repositionCursor(cursor, direction);
return true;
}
else {
invalidEdit();
}
return false;
|
void | repositionCursor(int offset, int direction)Resets the cursor by using getNextCursorPosition.
getFormattedTextField().getCaret().setDot(getNextCursorPosition
(offset, direction));
|
public void | setAllowsInvalid(boolean allowsInvalid)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.
this.allowsInvalid = allowsInvalid;
|
public void | setCommitsOnValidEdit(boolean commit)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 .
commitOnEdit = commit;
|
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.
fb.setDot(dot, bias);
|
public void | setOverwriteMode(boolean overwriteMode)Configures the behavior when inserting characters. If
overwriteMode is true (the default), new characters
overwrite existing characters in the model.
this.overwriteMode = overwriteMode;
|
public void | setValueClass(java.lang.Class valueClass)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.
this.valueClass = valueClass;
|
public java.lang.Object | stringToValue(java.lang.String string)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.
Class vc = getValueClass();
JFormattedTextField ftf = getFormattedTextField();
if (vc == null && ftf != null) {
Object value = ftf.getValue();
if (value != null) {
vc = value.getClass();
}
}
if (vc != null) {
Constructor cons;
try {
cons = vc.getConstructor(new Class[] { String.class });
} catch (NoSuchMethodException nsme) {
cons = null;
}
if (cons != null) {
try {
return cons.newInstance(new Object[] { string });
} catch (Throwable ex) {
throw new ParseException("Error creating instance", 0);
}
}
}
return string;
|
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(null);
|
void | updateValue(java.lang.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.
try {
if (value == null) {
String string = getFormattedTextField().getText();
value = stringToValue(string);
}
if (getCommitsOnValidEdit()) {
commitEdit();
}
setEditValid(true);
} catch (ParseException pe) {
setEditValid(false);
}
|
public java.lang.String | valueToString(java.lang.Object value)Converts the passed in Object into a String by way of the
toString method.
if (value == null) {
return "";
}
return value.toString();
|