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
|
|
29 private ClassInfo toType;
|
|
30
|
|
31 private ClassInfo[][] primitiveMap = new ClassInfo[][] [
|
|
32 [ Integer.TYPE, Integer.classinfo ], [ Short.TYPE, Short.classinfo ],
|
|
33 [ Long.TYPE, Long.classinfo ], [ Double.TYPE, Double.classinfo ],
|
|
34 [ Byte.TYPE, Byte.classinfo ], [ Float.TYPE, Float.classinfo ],
|
|
35 [ Boolean.TYPE, Boolean.classinfo ],
|
|
36 [ Character.TYPE, Character.classinfo ] ];
|
|
37
|
|
38 /**
|
|
39 * @param toType
|
|
40 */
|
|
41 public this(ClassInfo toType) {
|
|
42 this.toType = toType;
|
|
43 }
|
|
44
|
|
45 protected ClassInfo getToType() {
|
|
46 return this.toType;
|
|
47 }
|
|
48
|
|
49 public IStatus validate(Object value) {
|
|
50 return doValidate(value);
|
|
51 }
|
|
52
|
|
53 private IStatus doValidate(Object value) {
|
|
54 if (value !is null) {
|
|
55 if (!mapContainsValues(toType, value.getClass())) {
|
|
56 return ValidationStatus.error(getClassHint());
|
|
57 }
|
|
58 return Status.OK_STATUS;
|
|
59 }
|
|
60 return ValidationStatus.error(getNullHint());
|
|
61 }
|
|
62
|
|
63 private bool mapContainsValues(ClassInfo toType, ClassInfo fromType) {
|
|
64 for (int i = 0; i < primitiveMap.length; i++) {
|
|
65 if ((primitiveMap[i][0].equals(toType))
|
|
66 && (primitiveMap[i][1].equals(fromType))) {
|
|
67 return true;
|
|
68 }
|
|
69 }
|
|
70 return false;
|
|
71 }
|
|
72
|
|
73 /**
|
|
74 * @return a hint string
|
|
75 */
|
|
76 public String getNullHint() {
|
|
77 return BindingMessages.getStringcast(BindingMessages.VALIDATE_CONVERSION_TO_PRIMITIVE);
|
|
78 }
|
|
79
|
|
80 /**
|
|
81 * @return a hint string
|
|
82 */
|
|
83 public String getClassHint() {
|
|
84 return BindingMessages
|
|
85 .getStringcast(BindingMessages.VALIDATE_CONVERSION_FROM_CLASS_TO_PRIMITIVE);
|
|
86 }
|
|
87 }
|