currentText = "";
}
TypeExpr typeExpr = valueEntryPanel.getValueNode().getTypeExpr();
PreludeTypeConstants typeConstants = valueEntryPanel.valueEditorManager.getPreludeTypeConstants();
if (typeExpr.sameType(typeConstants.getDoubleType()) ||
typeExpr.sameType(typeConstants.getFloatType())) {
// Remove trailing -
len = currentText.length();
if (len > 0) {
char lastChar = currentText.charAt(len - 1);
if (lastChar == '-') {
currentText = currentText.substring(0, len - 1);
len--;
changed = true;
}
}
// Check the unique case where the user types in a '.' first
// Then we want to show '0.' instead
if (currentText.indexOf(".") == 0) {
currentText = "0" + currentText;
len++;
changed = true;
}
// Remove trailing 'e' or 'E'
if (len > 0) {
char lastChar = currentText.charAt(len - 1);
if (lastChar == 'e' || lastChar == 'E') {
currentText = currentText.substring(0, len - 1);
len--;
changed = true;
}
}
// If this is empty, set the text to zero
if (len == 0) {
currentText = "0.0";
len = 3;
changed = true;
} else {
// If the value is a perfect whole number, then want the ending ".0".
int indexOfPeriod = currentText.indexOf(".");
if (indexOfPeriod == -1) {
// Check for an 'e' within the value, or if the value doesn't end with a digit (eg. "Infinity").
int indexOfE = Math.max(currentText.indexOf('e'), currentText.indexOf('E'));
boolean endsWithDigit = Character.isDigit(currentText.charAt(len - 1));
if (indexOfE < 0 && endsWithDigit) {
currentText = currentText + ".0";
len += 2;
changed = true;
}
} else if (indexOfPeriod == (currentText.length() - 1)) {
currentText = currentText + "0";
len++;
changed = true;
}
}
} else if (typeExpr.sameType(typeConstants.getDecimalType())) {
// Remove trailing -
len = currentText.length();
if (len > 0) {
char lastChar = currentText.charAt(len - 1);
if (lastChar == '-') {
currentText = currentText.substring(0, len - 1);
len--;
changed = true;
}
}
// Check the unique case where the user types in a '.' first
// Then we want to show '0.' instead
if (currentText.indexOf(".") == 0) {
currentText = "0" + currentText;
len++;
changed = true;
}
// Remove trailing 'e' or 'E'
if (len > 0) {
char lastChar = currentText.charAt(len - 1);
if (lastChar == 'e' || lastChar == 'E') {
currentText = currentText.substring(0, len - 1);
len--;
changed = true;
}
}
// If this is empty, set the text to zero
if (len == 0) {
currentText = "0";
len = 1;
changed = true;
}
} else if (typeExpr.sameType(typeConstants.getIntType()) ||
typeExpr.sameType(typeConstants.getIntegerType()) ||
typeExpr.sameType(typeConstants.getByteType()) ||
typeExpr.sameType(typeConstants.getShortType()) ||
typeExpr.sameType(typeConstants.getLongType())) {
// Remove trailing -
len = currentText.length();
if (len > 0) {
char lastChar = currentText.charAt(len - 1);
if (lastChar == '-') {
currentText = currentText.substring(0, len - 1);
len--;
changed = true;
}
}
// If this is empty, set the text to zero
if (len == 0) {
currentText = "0";
len = 1;
changed = true;
}
} else if (typeExpr.sameType(typeConstants.getCharType())) {
len = currentText.length();
if (len != 1) {
// Default value is a space.
currentText = " ";
changed = true;
}
} else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeDate)) {
RelativeDateValueNode dateValueNode = (RelativeDateValueNode) valueEntryPanel.getValueNode();
// Check. If it's parseable, then it's valid (of course!).
// If not parseable, then give a default value of the date in ValueNode.
DateFormat dateFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, -1);
try {
dateFormat.parse(currentText);
} catch (ParseException pe) {
currentText = dateFormat.format(dateValueNode.getDateValue());
changed = true;
}
} else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeTime)) {
RelativeTimeValueNode timeValueNode = (RelativeTimeValueNode) valueEntryPanel.getValueNode();
// Check. If it's parseable, then it's valid (of course!).
// If not parseable, then give a default value of the time in ValueNode.
DateFormat timeFormat = RelativeTemporalValueNode.getDateFormat(-1, DateFormat.MEDIUM);
try {
timeFormat.parse(currentText);
} catch (ParseException pe) {
currentText = timeFormat.format(timeValueNode.getTimeValue());
changed = true;
}
} else if (typeExpr.isNonParametricType(CAL_RelativeTime.TypeConstructors.RelativeDateTime)) {
RelativeDateTimeValueNode dateTimeValueNode = (RelativeDateTimeValueNode) valueEntryPanel.getValueNode();
// Check. If it's parseable, then it's valid (of course!).
// If not parseable, then give a default value of the date time in ValueNode.
DateFormat dateTimeFormat = RelativeTemporalValueNode.getDateFormat(DateFormat.FULL, DateFormat.MEDIUM);
try {
dateTimeFormat.parse(currentText);
} catch (ParseException pe) {
currentText = dateTimeFormat.format(dateTimeValueNode.getDateTimeValue());
changed = true;
}
} else if (typeExpr.isNonParametricType(CAL_Time.TypeConstructors.Time)) {
JTimeValueNode valueNode = (JTimeValueNode) valueEntryPanel.getValueNode();
// Check. If it's parseable, then it's valid (of course!).
// If not parseable, then give a default value of the date time in ValueNode.
DateFormat fmt=DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG);
fmt.setTimeZone(TimeZone.getDefault());
try {
fmt.parse(currentText);
} catch (ParseException pe) {
currentText = fmt.format(valueNode.getJavaDate());
changed = true;
}
} else if (typeExpr.sameType(typeConstants.getCharListType())) {
// Strings are valid.
} else if (typeExpr.sameType(typeConstants.getStringType())) {
// Strings are valid.
} else {