comparison org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/ObjectToPrimitiveValidator.d @ 85:6be48cf9f95c

Work on databinding
author Frank Benoit <benoit@tionex.de>
date Sat, 18 Apr 2009 13:54:50 +0200
parents 0a55d2d5a946
children 9e0ab372d5d8
comparison
equal deleted inserted replaced
84:fcf926c91ca4 85:6be48cf9f95c
11 *******************************************************************************/ 11 *******************************************************************************/
12 12
13 module org.eclipse.core.internal.databinding.validation.ObjectToPrimitiveValidator; 13 module org.eclipse.core.internal.databinding.validation.ObjectToPrimitiveValidator;
14 14
15 import java.lang.all; 15 import java.lang.all;
16 import java.nonstandard.RuntimeTraits;
16 17
17 import org.eclipse.core.databinding.validation.IValidator; 18 import org.eclipse.core.databinding.validation.IValidator;
18 import org.eclipse.core.databinding.validation.ValidationStatus; 19 import org.eclipse.core.databinding.validation.ValidationStatus;
19 import org.eclipse.core.internal.databinding.BindingMessages; 20 import org.eclipse.core.internal.databinding.BindingMessages;
20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.IStatus;
24 * @since 3.2 25 * @since 3.2
25 * 26 *
26 */ 27 */
27 public class ObjectToPrimitiveValidator : IValidator { 28 public class ObjectToPrimitiveValidator : IValidator {
28 29
29 private ClassInfo toType; 30 private TypeInfo toType;
30 31
31 private ClassInfo[][] primitiveMap = new ClassInfo[][] [ 32 private static TypeInfo[][] primitiveMap;
32 [ Integer.TYPE, Integer.classinfo ], [ Short.TYPE, Short.classinfo ], 33 static this(){
33 [ Long.TYPE, Long.classinfo ], [ Double.TYPE, Double.classinfo ], 34 primitiveMap = [
34 [ Byte.TYPE, Byte.classinfo ], [ Float.TYPE, Float.classinfo ], 35 [ Integer.TYPE, typeid(Integer) ], [ Short.TYPE, typeid(Short) ],
35 [ Boolean.TYPE, Boolean.classinfo ], 36 [ Long.TYPE, typeid(Long) ], [ Double.TYPE, typeid(Double) ],
36 [ Character.TYPE, Character.classinfo ] ]; 37 [ Byte.TYPE, typeid(Byte) ], [ Float.TYPE, typeid(Float) ],
38 [ Boolean.TYPE, typeid(Boolean) ],
39 [ Character.TYPE, typeid(Character) ] ];
40 }
37 41
38 /** 42 /**
39 * @param toType 43 * @param toType
40 */ 44 */
41 public this(ClassInfo toType) { 45 public this(TypeInfo toType) {
42 this.toType = toType; 46 this.toType = toType;
43 } 47 }
44 48
45 protected ClassInfo getToType() { 49 protected TypeInfo getToType() {
46 return this.toType; 50 return this.toType;
47 } 51 }
48 52
49 public IStatus validate(Object value) { 53 public IStatus validate(Object value) {
50 return doValidate(value); 54 return doValidate(value);
51 } 55 }
52 56
53 private IStatus doValidate(Object value) { 57 private IStatus doValidate(Object value) {
54 if (value !is null) { 58 if (value !is null) {
55 if (!mapContainsValues(toType, value.getClass())) { 59 if (!mapContainsValues(toType, getTypeInfo(value.classinfo))) {
56 return ValidationStatus.error(getClassHint()); 60 return ValidationStatus.error(getClassHint());
57 } 61 }
58 return Status.OK_STATUS; 62 return Status.OK_STATUS;
59 } 63 }
60 return ValidationStatus.error(getNullHint()); 64 return ValidationStatus.error(getNullHint());
61 } 65 }
62 66
63 private bool mapContainsValues(ClassInfo toType, ClassInfo fromType) { 67 private bool mapContainsValues(TypeInfo toType, TypeInfo fromType) {
64 for (int i = 0; i < primitiveMap.length; i++) { 68 for (int i = 0; i < primitiveMap.length; i++) {
65 if ((primitiveMap[i][0].equals(toType)) 69 if ((primitiveMap[i][0] == toType )
66 && (primitiveMap[i][1].equals(fromType))) { 70 && (primitiveMap[i][1] == fromType )) {
67 return true; 71 return true;
68 } 72 }
69 } 73 }
70 return false; 74 return false;
71 } 75 }
72 76
73 /** 77 /**
74 * @return a hint string 78 * @return a hint string
75 */ 79 */
76 public String getNullHint() { 80 public String getNullHint() {
77 return BindingMessages.getStringcast(BindingMessages.VALIDATE_CONVERSION_TO_PRIMITIVE); 81 return BindingMessages.getString(BindingMessages.VALIDATE_CONVERSION_TO_PRIMITIVE);
78 } 82 }
79 83
80 /** 84 /**
81 * @return a hint string 85 * @return a hint string
82 */ 86 */
83 public String getClassHint() { 87 public String getClassHint() {
84 return BindingMessages 88 return BindingMessages
85 .getStringcast(BindingMessages.VALIDATE_CONVERSION_FROM_CLASS_TO_PRIMITIVE); 89 .getString(BindingMessages.VALIDATE_CONVERSION_FROM_CLASS_TO_PRIMITIVE);
86 } 90 }
87 } 91 }