public void insertString(int offset,
String str,
AttributeSet a) throws BadLocationException {
if (str == null || str.length() == 0) {
return;
}
Content content = getContent();
int length = content.length();
int originalLength = length;
parsePos.setIndex(0);
// Create the result of inserting the new data,
// but ignore the trailing newline
String targetString = content.getString(0, offset) + str +
content.getString(offset, length - offset - 1);
// Parse the input string and check for errors
do {
boolean gotPositive = targetString.startsWith(positivePrefix);
boolean gotNegative = targetString.startsWith(negativePrefix);
length = targetString.length();
// If we have a valid prefix, the parse fails if the
// suffix is not present and the error is reported
// at index 0. So, we need to add the appropriate
// suffix if it is not present at this point.
if (gotPositive == true || gotNegative == true) {
String suffix;
int suffixLength;
int prefixLength;
if (gotPositive == true && gotNegative == true) {
// This happens if one is the leading part of
// the other - e.g. if one is "(" and the other "(("
if (positivePrefixLen > negativePrefixLen) {
gotNegative = false;
} else {
gotPositive = false;
}
}
if (gotPositive == true) {
suffix = positiveSuffix;
suffixLength = positiveSuffixLen;
prefixLength = positivePrefixLen;
} else {
// Must have the negative prefix
suffix = negativeSuffix;
suffixLength = negativeSuffixLen;
prefixLength = negativePrefixLen;
}
// If the string consists of the prefix alone,
// do nothing, or the result won't parse.
if (length == prefixLength) {
break;
}
// We can't just add the suffix, because part of it
// may already be there. For example, suppose the
// negative prefix is "(" and the negative suffix is
// "$)". If the user has typed "(345$", then it is not
// correct to add "$)". Instead, only the missing part
// should be added, in this case ")".
if (targetString.endsWith(suffix) == false) {
int i;
for (i = suffixLength - 1; i > 0 ; i--) {
if (targetString.regionMatches(length - i,
suffix, 0, i)) {
targetString += suffix.substring(i);
break;
}
}
if (i == 0) {
// None of the suffix was present
targetString += suffix;
}
length = targetString.length();
}
}
format.parse(targetString, parsePos);
int endIndex = parsePos.getIndex();
if (endIndex == length) {
break; // Number is acceptable
}
// Parse ended early
// Since incomplete numbers don't always parse, try
// to work out what went wrong.
// First check for an incomplete positive prefix
if (positivePrefixLen > 0 &&
endIndex < positivePrefixLen &&
length < = positivePrefixLen &&
targetString.regionMatches(0, positivePrefix, 0, length)) {
break; // Accept for now
}
// Next check for an incomplete negative prefix
if (negativePrefixLen > 0 &&
endIndex < negativePrefixLen &&
length < = negativePrefixLen &&
targetString.regionMatches(0, negativePrefix, 0, length)) {
break; // Accept for now
}
// Allow a number that ends with the group
// or decimal separator, if these are in use
char lastChar = targetString.charAt(originalLength - 1);
int decimalIndex = targetString.indexOf(decimalSeparator);
if (format.isGroupingUsed() &&
lastChar == groupingSeparator &&
decimalIndex == -1) {
// Allow a "," but only in integer part
break;
}
if(format.isParseIntegerOnly() == false &&
lastChar == decimalSeparator &&
decimalIndex == originalLength - 1) {
// Allow a ".", but only one
break;
}
// No more corrections to make: must be an error
if (errorListener != null) {
errorListener.insertFailed(this, offset, str, a);
}
return;
} while (true == false);
// Finally, add to the model
super.insertString(offset, str, a);
}
|