Method from javax.swing.text.DefaultFormatter Detail: |
boolean canReplace(ReplaceHolder rh) {
return isValidEdit(rh);
}
Returns true if the edit described by rh will result
in a legal value. |
public Object clone() throws CloneNotSupportedException {
DefaultFormatter formatter = (DefaultFormatter)super.clone();
formatter.navigationFilter = null;
formatter.documentFilter = null;
formatter.replaceHolder = null;
return formatter;
}
Creates a copy of the DefaultFormatter. |
void commitEdit() throws ParseException {
JFormattedTextField ftf = getFormattedTextField();
if (ftf != null) {
ftf.commitEdit();
}
}
Invokes commitEdit on the JFormattedTextField. |
public boolean getAllowsInvalid() {
return allowsInvalid;
}
Returns whether or not the value being edited is allowed to be invalid
for a length of time. |
public boolean getCommitsOnValidEdit() {
return commitOnEdit;
}
Returns when edits are published back to the
JFormattedTextField . |
protected DocumentFilter getDocumentFilter() {
if (documentFilter == null) {
documentFilter = new DefaultDocumentFilter();
}
return documentFilter;
}
Returns the DocumentFilter used to restrict the characters
that can be input into the JFormattedTextField . |
int getInitialVisualPosition() {
return getNextNavigatableChar(0, 1);
}
Returns the initial location to position the cursor at. This forwards
the call to getNextNavigatableChar . |
protected NavigationFilter getNavigationFilter() {
if (navigationFilter == null) {
navigationFilter = new DefaultNavigationFilter();
}
return navigationFilter;
}
Returns the NavigationFilter used to restrict where the
cursor can be placed. |
int getNextCursorPosition(int offset,
int direction) {
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;
}
Returns the next cursor position from offset by incrementing
direction . This uses
getNextNavigatableChar
as well as constraining the location to the max position. |
int getNextVisualPositionFrom(JTextComponent text,
int pos,
Bias bias,
int direction,
Bias[] biasRet) throws BadLocationException {
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;
}
Finds the next navigatable character. |
public boolean getOverwriteMode() {
return overwriteMode;
}
Returns the behavior when inserting characters. |
ReplaceHolder getReplaceHolder(FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs) {
if (replaceHolder == null) {
replaceHolder = new ReplaceHolder();
}
replaceHolder.reset(fb, offset, length, text, attrs);
return replaceHolder;
}
Returns the ReplaceHolder to track the replace of the specified
text. |
String getReplaceString(int offset,
int deleteLength,
String replaceString) {
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;
}
A convenience methods to return the result of deleting
deleteLength characters at offset
and inserting replaceString at offset
in the current text field. |
public Class<?> getValueClass() {
return valueClass;
}
Returns that class that is used to create new Objects. |
public void install(JFormattedTextField ftf) {
super.install(ftf);
positionCursorAtInitialLocation();
}
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. |
boolean isLegalInsertText(String text) {
return true;
}
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. |
boolean isNavigatable(int offset) {
return true;
}
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. |
boolean isValidEdit(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(FilterBypass fb,
int dot,
Bias bias) {
fb.moveDot(dot, bias);
}
NavigationFilter method, subclasses that wish finer control should
override this. |
void positionCursorAtInitialLocation() {
JFormattedTextField ftf = getFormattedTextField();
if (ftf != null) {
ftf.setCaretPosition(getInitialVisualPosition());
}
}
Positions the cursor at the initial location. |
boolean replace(ReplaceHolder rh) throws BadLocationException {
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 &&
getFormattedTextField().getSelectedText() == 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;
}
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. |
void replace(FilterBypass fb,
int offset,
int length,
String text,
AttributeSet attrs) throws BadLocationException {
ReplaceHolder rh = getReplaceHolder(fb, offset, length, text, attrs);
replace(rh);
}
DocumentFilter method, funnels into replace . |
void repositionCursor(int offset,
int direction) {
getFormattedTextField().getCaret().setDot(getNextCursorPosition
(offset, direction));
}
Resets the cursor by using getNextCursorPosition. |
public void setAllowsInvalid(boolean allowsInvalid) {
this.allowsInvalid = 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. |
public void setCommitsOnValidEdit(boolean commit) {
commitOnEdit = 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 . |
void setDot(FilterBypass fb,
int dot,
Bias bias) {
fb.setDot(dot, bias);
}
NavigationFilter method, subclasses that wish finer control should
override this. |
public void setOverwriteMode(boolean overwriteMode) {
this.overwriteMode = overwriteMode;
}
Configures the behavior when inserting characters. If
overwriteMode is true (the default), new characters
overwrite existing characters in the model. |
public void setValueClass(Class<?> valueClass) {
this.valueClass = 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. |
public Object stringToValue(String string) throws ParseException {
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;
}
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. |
void updateValue() {
updateValue(null);
}
Pushes the value to the JFormattedTextField if the current value
is valid and invokes setEditValid based on the
validity of the value. |
void updateValue(Object value) {
try {
if (value == null) {
String string = getFormattedTextField().getText();
value = stringToValue(string);
}
if (getCommitsOnValidEdit()) {
commitEdit();
}
setEditValid(true);
} catch (ParseException pe) {
setEditValid(false);
}
}
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. |
public String valueToString(Object value) throws ParseException {
if (value == null) {
return "";
}
return value.toString();
}
Converts the passed in Object into a String by way of the
toString method. |