1 11 12 package org.eclipse.core.internal.databinding.validation; 13 14 import org.eclipse.core.databinding.validation.IValidator; 15 import org.eclipse.core.databinding.validation.ValidationStatus; 16 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; 17 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 27 public abstract class AbstractStringToNumberValidator implements IValidator { 28 private final NumberFormatConverter converter; 29 private final boolean toPrimitive; 30 31 private final Number min; 32 private final Number max; 33 34 private String outOfRangeMessage; 35 36 43 protected AbstractStringToNumberValidator(NumberFormatConverter converter, 44 Number min, Number max) { 45 this.converter = converter; 46 this.min = min; 47 this.max = max; 48 49 if (converter.getToType() instanceof Class ) { 50 Class clazz = (Class ) converter.getToType(); 51 toPrimitive = clazz.isPrimitive(); 52 } else { 53 toPrimitive = false; 54 } 55 } 56 57 66 public final IStatus validate(Object value) { 67 ParseResult result = StringToNumberParser.parse(value, converter 68 .getNumberFormat(), toPrimitive); 69 70 if (result.getNumber() != null) { 71 if (!isInRange(result.getNumber())) { 72 if (outOfRangeMessage == null) { 73 outOfRangeMessage = StringToNumberParser 74 .createOutOfRangeMessage(min, max, converter 75 .getNumberFormat()); 76 } 77 78 return ValidationStatus.error(outOfRangeMessage); 79 } 80 } else if (result.getPosition() != null) { 81 String parseErrorMessage = StringToNumberParser.createParseErrorMessage( 82 (String ) value, result.getPosition()); 83 84 return ValidationStatus.error(parseErrorMessage); 85 } 86 87 return Status.OK_STATUS; 88 } 89 90 96 protected abstract boolean isInRange(Number number); 97 } 98 | Popular Tags |