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;
|
85
|
16 import java.nonstandard.RuntimeTraits;
|
78
|
17
|
|
18 import org.eclipse.core.databinding.validation.IValidator;
|
|
19 import org.eclipse.core.databinding.validation.ValidationStatus;
|
|
20 import org.eclipse.core.internal.databinding.BindingMessages;
|
|
21 import org.eclipse.core.runtime.IStatus;
|
|
22 import org.eclipse.core.runtime.Status;
|
|
23
|
|
24 /**
|
|
25 * @since 3.2
|
|
26 *
|
|
27 */
|
|
28 public class ObjectToPrimitiveValidator : IValidator {
|
|
29
|
85
|
30 private TypeInfo toType;
|
78
|
31
|
85
|
32 private static TypeInfo[][] primitiveMap;
|
|
33 static this(){
|
|
34 primitiveMap = [
|
|
35 [ Integer.TYPE, typeid(Integer) ], [ Short.TYPE, typeid(Short) ],
|
|
36 [ Long.TYPE, typeid(Long) ], [ Double.TYPE, typeid(Double) ],
|
|
37 [ Byte.TYPE, typeid(Byte) ], [ Float.TYPE, typeid(Float) ],
|
|
38 [ Boolean.TYPE, typeid(Boolean) ],
|
|
39 [ Character.TYPE, typeid(Character) ] ];
|
|
40 }
|
78
|
41
|
|
42 /**
|
|
43 * @param toType
|
|
44 */
|
85
|
45 public this(TypeInfo toType) {
|
78
|
46 this.toType = toType;
|
|
47 }
|
|
48
|
85
|
49 protected TypeInfo getToType() {
|
78
|
50 return this.toType;
|
|
51 }
|
|
52
|
|
53 public IStatus validate(Object value) {
|
|
54 return doValidate(value);
|
|
55 }
|
|
56
|
|
57 private IStatus doValidate(Object value) {
|
|
58 if (value !is null) {
|
85
|
59 if (!mapContainsValues(toType, getTypeInfo(value.classinfo))) {
|
78
|
60 return ValidationStatus.error(getClassHint());
|
|
61 }
|
|
62 return Status.OK_STATUS;
|
|
63 }
|
|
64 return ValidationStatus.error(getNullHint());
|
|
65 }
|
|
66
|
85
|
67 private bool mapContainsValues(TypeInfo toType, TypeInfo fromType) {
|
78
|
68 for (int i = 0; i < primitiveMap.length; i++) {
|
85
|
69 if ((primitiveMap[i][0] == toType )
|
|
70 && (primitiveMap[i][1] == fromType )) {
|
78
|
71 return true;
|
|
72 }
|
|
73 }
|
|
74 return false;
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * @return a hint string
|
|
79 */
|
|
80 public String getNullHint() {
|
85
|
81 return BindingMessages.getString(BindingMessages.VALIDATE_CONVERSION_TO_PRIMITIVE);
|
78
|
82 }
|
|
83
|
|
84 /**
|
|
85 * @return a hint string
|
|
86 */
|
|
87 public String getClassHint() {
|
|
88 return BindingMessages
|
85
|
89 .getString(BindingMessages.VALIDATE_CONVERSION_FROM_CLASS_TO_PRIMITIVE);
|
78
|
90 }
|
|
91 }
|