Mercurial > projects > dwt2
comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/AbstractStringToNumberValidator.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children | 383ce7bd736b |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
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 module org.eclipse.core.internal.databinding.validation.AbstractStringToNumberValidator; | |
13 | |
14 import java.lang.all; | |
15 | |
16 import org.eclipse.core.databinding.validation.IValidator; | |
17 import org.eclipse.core.databinding.validation.ValidationStatus; | |
18 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser; | |
19 import org.eclipse.core.internal.databinding.conversion.StringToNumberParser.ParseResult; | |
20 import org.eclipse.core.runtime.IStatus; | |
21 import org.eclipse.core.runtime.Status; | |
22 | |
23 /** | |
24 * Validates a number that is to be converted by a {@link NumberFormatConverter}. | |
25 * Validation is comprised of parsing the String and range checks. | |
26 * | |
27 * @since 1.0 | |
28 */ | |
29 public abstract class AbstractStringToNumberValidator : IValidator { | |
30 private final NumberFormatConverter converter; | |
31 private final bool toPrimitive; | |
32 | |
33 private final Number min; | |
34 private final Number max; | |
35 | |
36 private String outOfRangeMessage; | |
37 | |
38 /** | |
39 * Constructs a new instance. | |
40 * | |
41 * @param converter converter and thus formatter to be used in validation | |
42 * @param min minimum value, used for reporting a range error to the user | |
43 * @param max maximum value, used for reporting a range error to the user | |
44 */ | |
45 protected this(NumberFormatConverter converter, | |
46 Number min, Number max) { | |
47 this.converter = converter; | |
48 this.min = min; | |
49 this.max = max; | |
50 | |
51 if (null !is cast(ClassInfo)converter.getToType()) { | |
52 ClassInfo clazz = cast(ClassInfo) converter.getToType(); | |
53 toPrimitive = clazz.isPrimitive(); | |
54 } else { | |
55 toPrimitive = false; | |
56 } | |
57 } | |
58 | |
59 /** | |
60 * Validates the provided <code>value</code>. An error status is returned if: | |
61 * <ul> | |
62 * <li>The value cannot be parsed.</li> | |
63 * <li>The value is out of range.</li> | |
64 * </ul> | |
65 * | |
66 * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object) | |
67 */ | |
68 public final IStatus validate(Object value) { | |
69 ParseResult result = StringToNumberParser.parse(value, converter | |
70 .getNumberFormat(), toPrimitive); | |
71 | |
72 if (result.getNumber() !is null) { | |
73 if (!isInRange(result.getNumber())) { | |
74 if (outOfRangeMessage is null) { | |
75 outOfRangeMessage = StringToNumberParser | |
76 .createOutOfRangeMessage(min, max, converter | |
77 .getNumberFormat()); | |
78 } | |
79 | |
80 return ValidationStatus.error(outOfRangeMessage); | |
81 } | |
82 } else if (result.getPosition() !is null) { | |
83 String parseErrorMessage = StringToNumberParser.createParseErrorMessage( | |
84 cast(String) value, result.getPosition()); | |
85 | |
86 return ValidationStatus.error(parseErrorMessage); | |
87 } | |
88 | |
89 return Status.OK_STATUS; | |
90 } | |
91 | |
92 /** | |
93 * Invoked by {@link #validatecast(Object)} when the range is to be validated. | |
94 * | |
95 * @param number | |
96 * @return <code>true</code> if in range | |
97 */ | |
98 protected abstract bool isInRange(Number number); | |
99 } |