1 11 12 package org.eclipse.core.internal.databinding.validation; 13 14 import java.util.Date ; 15 16 import org.eclipse.core.databinding.validation.IValidator; 17 import org.eclipse.core.databinding.validation.ValidationStatus; 18 import org.eclipse.core.internal.databinding.BindingMessages; 19 import org.eclipse.core.internal.databinding.conversion.DateConversionSupport; 20 import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 24 27 public class StringToDateValidator implements IValidator { 28 private final StringToDateConverter converter; 29 30 33 public StringToDateValidator(StringToDateConverter converter) { 34 this.converter = converter; 35 } 36 37 42 public IStatus validate(Object value) { 43 Object convertedValue = converter.convert(value); 44 if (convertedValue == null) { 46 return ValidationStatus.error(getErrorMessage()); 47 } 48 49 return Status.OK_STATUS; 50 } 51 52 57 protected String getErrorMessage() { 58 Date sampleDate = new Date (); 59 60 FormatUtil util = new FormatUtil(); 63 StringBuffer samples = new StringBuffer (); 64 for (int formatterIdx = 1; formatterIdx < util.numFormatters() - 2; formatterIdx++) { 65 samples.append('\''); 66 samples.append(util.format(sampleDate, formatterIdx)); 67 samples.append("', "); } 69 samples.append('\''); 70 samples.append(util.format(sampleDate, 0)); 71 samples.append('\''); 72 return BindingMessages.getString("Examples") + ": " + samples + ",..."; } 74 75 private static class FormatUtil extends DateConversionSupport { 76 81 protected int numFormatters() { 82 return super.numFormatters(); 83 } 84 85 90 protected String format(Date date) { 91 return super.format(date); 92 } 93 94 100 protected String format(Date date, int formatterIdx) { 101 return super.format(date, formatterIdx); 102 } 103 } 104 } 105 | Popular Tags |