Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/StringToDateValidator.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 6be48cf9f95c |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix for 217940 | |
11 *******************************************************************************/ | |
12 | |
13 module org.eclipse.core.internal.databinding.validation.StringToDateValidator; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import java.util.Date; | |
18 | |
19 import org.eclipse.core.databinding.validation.IValidator; | |
20 import org.eclipse.core.databinding.validation.ValidationStatus; | |
21 import org.eclipse.core.internal.databinding.BindingMessages; | |
22 import org.eclipse.core.internal.databinding.conversion.DateConversionSupport; | |
23 import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; | |
24 import org.eclipse.core.runtime.IStatus; | |
25 import org.eclipse.core.runtime.Status; | |
26 | |
27 /** | |
28 * @since 1.0 | |
29 */ | |
30 public class StringToDateValidator : IValidator { | |
31 private final StringToDateConverter converter; | |
32 | |
33 /** | |
34 * @param converter | |
35 */ | |
36 public this(StringToDateConverter converter) { | |
37 this.converter = converter; | |
38 } | |
39 | |
40 /* | |
41 * (non-Javadoc) | |
42 * | |
43 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object) | |
44 */ | |
45 public IStatus validate(Object value) { | |
46 if ( null !is cast(String )value && (cast(String)value).trim().length() is 0) { | |
47 return Status.OK_STATUS; | |
48 } | |
49 Object convertedValue = converter.convert(value); | |
50 //The StringToDateConverter returns null if it can't parse the date. | |
51 if (convertedValue is null) { | |
52 return ValidationStatus.error(getErrorMessage()); | |
53 } | |
54 | |
55 return Status.OK_STATUS; | |
56 } | |
57 | |
58 /* | |
59 * (non-Javadoc) | |
60 * | |
61 * @see org.eclipse.core.internal.databinding.validation.WrappedConverterValidator#getErrorMessage() | |
62 */ | |
63 protected String getErrorMessage() { | |
64 Date sampleDate = new Date(); | |
65 | |
66 // FIXME We need to use the information from the | |
67 // converter, not use another instance of DateConversionSupport. | |
68 FormatUtil util = new FormatUtil(); | |
69 StringBuffer samples = new StringBuffer(); | |
70 for (int formatterIdx = 1; formatterIdx < util.numFormatters() - 2; formatterIdx++) { | |
71 samples.append('\''); | |
72 samples.append(util.format(sampleDate, formatterIdx)); | |
73 samples.append("', "); //$NON-NLS-1$ | |
74 } | |
75 samples.append('\''); | |
76 samples.append(util.format(sampleDate, 0)); | |
77 samples.append('\''); | |
78 return BindingMessages.getStringcast(BindingMessages.EXAMPLES) + ": " + samples + ",..."; //$NON-NLS-1$//$NON-NLS-2$ | |
79 } | |
80 | |
81 private static class FormatUtil : DateConversionSupport { | |
82 /* | |
83 * (non-Javadoc) | |
84 * | |
85 * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#numFormatters() | |
86 */ | |
87 protected int numFormatters() { | |
88 return super.numFormatters(); | |
89 } | |
90 | |
91 /* | |
92 * (non-Javadoc) | |
93 * | |
94 * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date) | |
95 */ | |
96 protected String format(Date date) { | |
97 return super.format(date); | |
98 } | |
99 | |
100 /* | |
101 * (non-Javadoc) | |
102 * | |
103 * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date, | |
104 * int) | |
105 */ | |
106 protected String format(Date date, int formatterIdx) { | |
107 return super.format(date, formatterIdx); | |
108 } | |
109 } | |
110 } |