KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > validation > StringToDateValidator


1 /*******************************************************************************
2  * Copyright (c) 2007 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  ******************************************************************************/

11
12 package org.eclipse.core.internal.databinding.validation;
13
14 import java.util.Date JavaDoc;
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 /**
25  * @since 1.0
26  */

27 public class StringToDateValidator implements IValidator {
28     private final StringToDateConverter converter;
29
30     /**
31      * @param converter
32      */

33     public StringToDateValidator(StringToDateConverter converter) {
34         this.converter = converter;
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
41      */

42     public IStatus validate(Object JavaDoc value) {
43         Object JavaDoc convertedValue = converter.convert(value);
44         //The StringToDateConverter returns null if it can't parse the date.
45
if (convertedValue == null) {
46             return ValidationStatus.error(getErrorMessage());
47         }
48
49         return Status.OK_STATUS;
50     }
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.eclipse.core.internal.databinding.validation.WrappedConverterValidator#getErrorMessage()
56      */

57     protected String JavaDoc getErrorMessage() {
58         Date JavaDoc sampleDate = new Date JavaDoc();
59
60         // FIXME We need to use the information from the
61
// converter, not use another instance of DateConversionSupport.
62
FormatUtil util = new FormatUtil();
63         StringBuffer JavaDoc samples = new StringBuffer JavaDoc();
64         for (int formatterIdx = 1; formatterIdx < util.numFormatters() - 2; formatterIdx++) {
65             samples.append('\'');
66             samples.append(util.format(sampleDate, formatterIdx));
67             samples.append("', "); //$NON-NLS-1$
68
}
69         samples.append('\'');
70         samples.append(util.format(sampleDate, 0));
71         samples.append('\'');
72         return BindingMessages.getString("Examples") + ": " + samples + ",..."; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
73
}
74
75     private static class FormatUtil extends DateConversionSupport {
76         /*
77          * (non-Javadoc)
78          *
79          * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#numFormatters()
80          */

81         protected int numFormatters() {
82             return super.numFormatters();
83         }
84
85         /*
86          * (non-Javadoc)
87          *
88          * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date)
89          */

90         protected String JavaDoc format(Date JavaDoc date) {
91             return super.format(date);
92         }
93
94         /*
95          * (non-Javadoc)
96          *
97          * @see org.eclipse.core.internal.databinding.conversion.DateConversionSupport#format(java.util.Date,
98          * int)
99          */

100         protected String JavaDoc format(Date JavaDoc date, int formatterIdx) {
101             return super.format(date, formatterIdx);
102         }
103     }
104 }
105
Popular Tags