Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/internal/databinding/validation/ObjectToPrimitiveValidator.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
78 | 1 /******************************************************************************* |
2 * Copyright (c) 2006, 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.ObjectToPrimitiveValidator; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.core.databinding.validation.IValidator; | |
18 import org.eclipse.core.databinding.validation.ValidationStatus; | |
19 import org.eclipse.core.internal.databinding.BindingMessages; | |
20 import org.eclipse.core.runtime.IStatus; | |
21 import org.eclipse.core.runtime.Status; | |
22 | |
23 /** | |
24 * @since 3.2 | |
25 * | |
26 */ | |
27 public class ObjectToPrimitiveValidator : IValidator { | |
28 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
29 private Class toType; |
78 | 30 |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
31 private static Class[][] primitiveMap; |
85 | 32 static this(){ |
33 primitiveMap = [ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
34 [ Integer.TYPE, Class.fromType!(Integer) ], [ Short.TYPE, Class.fromType!(Short) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
35 [ Long.TYPE, Class.fromType!(Long) ], [ Double.TYPE, Class.fromType!(Double) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
36 [ Byte.TYPE, Class.fromType!(Byte) ], [ Float.TYPE, Class.fromType!(Float) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
37 [ Boolean.TYPE, Class.fromType!(Boolean) ], |
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
38 [ Character.TYPE, Class.fromType!(Character) ] ]; |
85 | 39 } |
78 | 40 |
41 /** | |
42 * @param toType | |
43 */ | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
44 public this(Class toType) { |
78 | 45 this.toType = toType; |
46 } | |
47 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
48 protected Class getToType() { |
78 | 49 return this.toType; |
50 } | |
51 | |
52 public IStatus validate(Object value) { | |
53 return doValidate(value); | |
54 } | |
55 | |
56 private IStatus doValidate(Object value) { | |
57 if (value !is null) { | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
58 if (!mapContainsValues(toType, Class.fromObject(value))) { |
78 | 59 return ValidationStatus.error(getClassHint()); |
60 } | |
61 return Status.OK_STATUS; | |
62 } | |
63 return ValidationStatus.error(getNullHint()); | |
64 } | |
65 | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
66 private bool mapContainsValues(Class toType, Class fromType) { |
78 | 67 for (int i = 0; i < primitiveMap.length; i++) { |
85 | 68 if ((primitiveMap[i][0] == toType ) |
69 && (primitiveMap[i][1] == fromType )) { | |
78 | 70 return true; |
71 } | |
72 } | |
73 return false; | |
74 } | |
75 | |
76 /** | |
77 * @return a hint string | |
78 */ | |
79 public String getNullHint() { | |
85 | 80 return BindingMessages.getString(BindingMessages.VALIDATE_CONVERSION_TO_PRIMITIVE); |
78 | 81 } |
82 | |
83 /** | |
84 * @return a hint string | |
85 */ | |
86 public String getClassHint() { | |
87 return BindingMessages | |
85 | 88 .getString(BindingMessages.VALIDATE_CONVERSION_FROM_CLASS_TO_PRIMITIVE); |
78 | 89 } |
90 } |