// System.out.println("Initial text = " + text);
// String charsToRemove;
// If the user is trying to backspace over a special character, like a comma, we don't
// allow it, but instead move to the previous number. This only applies if the dot and mark
// of the filter are the same (ie, they don't have a highlighted selection)
JFormattedTextField comp = this.field;
if (length == 1 && comp.getCaret().getDot() == comp.getCaret().getMark()) {
while (offset >= 0 && !Character.isDigit(text.charAt(offset))) {
offset--;
}
if (offset < 0) {
// Nothing to do
return;
}
// else {
// charsToRemove = text.substring(offset, offset + length);
// }
}
// else {
// charsToRemove = text.substring(offset, offset + length);
// }
// System.out.println("Chars to remove = " + charsToRemove);
try {
Object o = comp.getFormatter().stringToValue(text);
String value = text;
// TF:11/04/2008: Check for null coming back in case it's a widget mapped to
// a nullable field with a null value
if (o != null) {
value = o.toString();
// System.out.println(o.toString() + " - " + o.getClass().toString());
char[] chars = text.toCharArray();
int srcIndex = offset-1;
int destIndex = offset + length-1;
while (destIndex >= 0) {
if (Character.isDigit(chars[destIndex])) {
//need to replace this digit with another one
while (srcIndex >= 0 && !Character.isDigit(chars[srcIndex])) {
srcIndex--;
}
if (srcIndex >= 0) {
// Replace this index with the source digit
chars[destIndex] = chars[srcIndex];
// Now drop the source index back one
srcIndex--;
}
else {
// No character, must replace with 0
chars[destIndex] = '0';
}
}
destIndex--;
}
// System.out.println("Current dot at:" + this.field.getCaret().getDot());
// System.out.println("Current mark at:" + this.field.getCaret().getMark());
// System.out.println("New value = " + new String(chars));
o = comp.getFormatter().stringToValue(new String(chars));
}
value = comp.getFormatter().valueToString(o);
int lengthToReplace = getReplaceToOffset(text, value);
lengthToReplace = Math.max(length + offset, lengthToReplace);
// System.out.println("new value = " + value);
// System.out.println("replacement length(" + text + ", " + value + ") = " + lengthToReplace);