KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > factories > DefaultBindSupportFactory


1 /*******************************************************************************
2  * Copyright (c) 2006 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 package org.eclipse.jface.internal.databinding.provisional.factories;
13
14 import java.math.BigDecimal JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.internal.databinding.internal.ClassLookupSupport;
20 import org.eclipse.jface.internal.databinding.internal.Pair;
21 import org.eclipse.jface.internal.databinding.provisional.BindingException;
22 import org.eclipse.jface.internal.databinding.provisional.conversion.IConverter;
23 import org.eclipse.jface.internal.databinding.provisional.conversion.IdentityConverter;
24 import org.eclipse.jface.internal.databinding.provisional.validation.IDomainValidator;
25 import org.eclipse.jface.internal.databinding.provisional.validation.IValidator;
26 import org.eclipse.jface.internal.databinding.provisional.validation.ReadOnlyValidator;
27 import org.eclipse.jface.internal.databinding.provisional.validation.String2BigDecimalValidator;
28 import org.eclipse.jface.internal.databinding.provisional.validation.String2ByteValidator;
29 import org.eclipse.jface.internal.databinding.provisional.validation.String2DateValidator;
30 import org.eclipse.jface.internal.databinding.provisional.validation.String2DoubleValidator;
31 import org.eclipse.jface.internal.databinding.provisional.validation.String2FloatValidator;
32 import org.eclipse.jface.internal.databinding.provisional.validation.String2IntValidator;
33 import org.eclipse.jface.internal.databinding.provisional.validation.String2LongValidator;
34 import org.eclipse.jface.internal.databinding.provisional.validation.String2ShortValidator;
35 import org.eclipse.jface.internal.databinding.provisional.validation.ValidationError;
36
37 /**
38  * Default bind support factory. This factory adds the following converters and
39  * validators:
40  *
41  * TODO list converters and validators
42  *
43  * @since 1.0
44  *
45  */

46 public final class DefaultBindSupportFactory extends BindSupportFactory {
47
48     private ValidatorRegistry validatorRegistry = new ValidatorRegistry();
49
50     private Map JavaDoc converterMap;
51
52     public IValidator createValidator(Object JavaDoc fromType, Object JavaDoc toType) {
53         if (fromType == null || toType == null) {
54             return new IValidator() {
55
56                 public ValidationError isPartiallyValid(Object JavaDoc value) {
57                     return null;
58                 }
59
60                 public ValidationError isValid(Object JavaDoc value) {
61                     return null;
62                 }
63             };
64         }
65
66         IValidator dataTypeValidator = findValidator(fromType, toType);
67         if (dataTypeValidator == null) {
68             throw new BindingException(
69                     "No IValidator is registered for conversions from " + fromType + " to " + toType); //$NON-NLS-1$ //$NON-NLS-2$
70
}
71         return dataTypeValidator;
72     }
73
74     private IValidator findValidator(Object JavaDoc fromType, Object JavaDoc toType) {
75         // TODO string-based lookup of validator
76
return validatorRegistry.get(fromType, toType);
77     }
78
79     public IConverter createConverter(Object JavaDoc fromType, Object JavaDoc toType) {
80         if (!(fromType instanceof Class JavaDoc) || !(toType instanceof Class JavaDoc)) {
81             return null;
82         }
83         Class JavaDoc toClass = (Class JavaDoc) toType;
84         if (toClass.isPrimitive()) {
85             toClass = autoboxed(toClass);
86         }
87         Class JavaDoc fromClass = (Class JavaDoc) fromType;
88         if (fromClass.isPrimitive()) {
89             fromClass = autoboxed(fromClass);
90         }
91         if (toClass.isAssignableFrom(fromClass)) {
92             return new IdentityConverter(fromClass, toClass);
93         }
94         Map JavaDoc converterMap = getConverterMap();
95         Class JavaDoc[] supertypeHierarchyFlattened = ClassLookupSupport
96                 .getTypeHierarchyFlattened(fromClass);
97         for (int i = 0; i < supertypeHierarchyFlattened.length; i++) {
98             Class JavaDoc currentFromClass = supertypeHierarchyFlattened[i];
99             if (currentFromClass == toType) {
100                 // converting to toType is just a widening
101
return new IdentityConverter(fromClass, toClass);
102             }
103             Pair key = new Pair(currentFromClass.getName(), toClass.getName());
104             Object JavaDoc converterOrClassname = converterMap.get(key);
105             if (converterOrClassname instanceof IConverter) {
106                 return (IConverter) converterOrClassname;
107             } else if (converterOrClassname instanceof String JavaDoc) {
108                 String JavaDoc classname = (String JavaDoc) converterOrClassname;
109                 Class JavaDoc converterClass;
110                 try {
111                     converterClass = Class.forName(classname);
112                     IConverter result = (IConverter) converterClass
113                             .newInstance();
114                     converterMap.put(key, result);
115                     return result;
116                 } catch (ClassNotFoundException JavaDoc e) {
117                     e.printStackTrace();
118                 } catch (InstantiationException JavaDoc e) {
119                     e.printStackTrace();
120                 } catch (IllegalAccessException JavaDoc e) {
121                     e.printStackTrace();
122                 }
123             }
124         }
125         // Since we found no converter yet, try a "downcast" converter
126
if (fromClass.isAssignableFrom(toClass)) {
127             return new IdentityConverter(fromClass, toClass);
128         }
129         return null;
130     }
131
132     private Map JavaDoc getConverterMap() {
133         // using string-based lookup avoids loading of too many classes
134
if (converterMap == null) {
135             converterMap = new HashMap JavaDoc();
136             converterMap
137                     .put(
138                             new Pair("java.util.Date", "java.lang.String"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertDate2String"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
139
converterMap
140                     .put(
141                             new Pair("java.lang.String", "java.math.BigDecimal"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2BigDecimal"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
142
converterMap
143                     .put(
144                             new Pair("java.lang.String", "java.lang.Boolean"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Boolean"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
145
converterMap
146                     .put(
147                             new Pair("java.lang.String", "java.lang.Byte"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Byte"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
148
converterMap
149                     .put(
150                             new Pair("java.lang.String", "java.lang.Character"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Character"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
151
converterMap
152                     .put(
153                             new Pair("java.lang.String", "java.util.Date"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Date"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
154
converterMap
155                     .put(
156                             new Pair("java.lang.String", "java.lang.Double"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Double"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
157
converterMap
158                     .put(
159                             new Pair("java.lang.String", "java.lang.Float"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Float"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
160
converterMap
161                     .put(
162                             new Pair("java.lang.String", "java.lang.Integer"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Integer"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
163
converterMap
164                     .put(
165                             new Pair("java.lang.String", "java.lang.Long"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Long"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
166
converterMap
167                     .put(
168                             new Pair("java.lang.String", "java.lang.Short"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Short"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
169
converterMap
170                     .put(
171                             new Pair("java.lang.Object", "java.lang.String"), "org.eclipse.jface.internal.databinding.provisional.conversion.ToStringConverter"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
172
}
173         return converterMap;
174     }
175
176     public IDomainValidator createDomainValidator(Object JavaDoc modelType) {
177         return new IDomainValidator() {
178             public ValidationError isValid(Object JavaDoc value) {
179                 return null;
180             }
181         };
182     }
183
184     public Boolean JavaDoc isAssignableFromTo(Object JavaDoc fromType, Object JavaDoc toType) {
185         if (fromType instanceof Class JavaDoc && toType instanceof Class JavaDoc) {
186             Class JavaDoc toClass = (Class JavaDoc) toType;
187             if (toClass.isPrimitive()) {
188                 toClass = autoboxed(toClass);
189             }
190             Class JavaDoc fromClass = (Class JavaDoc) fromType;
191             if (fromClass.isPrimitive()) {
192                 fromClass = autoboxed(fromClass);
193             }
194             return Boolean.valueOf((toClass).isAssignableFrom(fromClass));
195         }
196         return null;
197     }
198
199     private Class JavaDoc autoboxed(Class JavaDoc clazz) {
200         if (clazz == Float.TYPE)
201             return Float JavaDoc.class;
202         else if (clazz == Double.TYPE)
203             return Double JavaDoc.class;
204         else if (clazz == Short.TYPE)
205             return Short JavaDoc.class;
206         else if (clazz == Integer.TYPE)
207             return Integer JavaDoc.class;
208         else if (clazz == Long.TYPE)
209             return Long JavaDoc.class;
210         else if (clazz == Boolean.TYPE)
211             return Boolean JavaDoc.class;
212         return clazz;
213     }
214
215     private static class ValidatorRegistry {
216
217         private HashMap JavaDoc validators = new HashMap JavaDoc();
218
219         /**
220          * Associate a particular validator that can validate the conversion
221          * (fromClass, toClass)
222          *
223          * @param fromClass
224          * The Class to convert from
225          * @param toClass
226          * The Class to convert to
227          * @param validator
228          * The IValidator
229          */

230         private void associate(Object JavaDoc fromClass, Object JavaDoc toClass,
231                 IValidator validator) {
232             validators.put(new Pair(fromClass, toClass), validator);
233         }
234
235         /**
236          * Return an IVerifier for a specific class.
237          *
238          * @param fromClass
239          * The Class to convert from
240          * @param toClass
241          * The Class to convert to
242          * @return An appropriate IValidator
243          */

244         private IValidator get(Object JavaDoc fromClass, Object JavaDoc toClass) {
245             IValidator result = (IValidator) validators.get(new Pair(fromClass,
246                     toClass));
247             if (result != null)
248                 return result;
249             if (fromClass != null && toClass != null && fromClass == toClass) {
250                 return new IValidator() {
251                     public ValidationError isPartiallyValid(Object JavaDoc value) {
252                         return null;
253                     }
254
255                     public ValidationError isValid(Object JavaDoc value) {
256                         return null;
257                     }
258                 };
259             }
260             return ReadOnlyValidator.getDefault();
261         }
262
263         /**
264          * Adds the system-provided validators to the current validator
265          * registry. This is done automatically for the validator registry
266          * singleton.
267          */

268         private ValidatorRegistry() {
269             // Standalone validators here...
270
associate(String JavaDoc.class, Integer.TYPE, new String2IntValidator());
271             associate(String JavaDoc.class, Byte.TYPE, new String2ByteValidator());
272             associate(String JavaDoc.class, Short.TYPE, new String2ShortValidator());
273             associate(String JavaDoc.class, Long.TYPE, new String2LongValidator());
274             associate(String JavaDoc.class, Float.TYPE, new String2FloatValidator());
275             associate(String JavaDoc.class, Double.TYPE, new String2DoubleValidator());
276
277             associate(String JavaDoc.class, Integer JavaDoc.class, new String2IntValidator());
278             associate(String JavaDoc.class, Byte JavaDoc.class, new String2ByteValidator());
279             associate(String JavaDoc.class, Short JavaDoc.class, new String2ShortValidator());
280             associate(String JavaDoc.class, Long JavaDoc.class, new String2LongValidator());
281             associate(String JavaDoc.class, Float JavaDoc.class, new String2FloatValidator());
282             associate(String JavaDoc.class, Double JavaDoc.class, new String2DoubleValidator());
283             associate(String JavaDoc.class, Date JavaDoc.class, new String2DateValidator());
284
285             associate(String JavaDoc.class, BigDecimal JavaDoc.class,
286                     new String2BigDecimalValidator());
287
288             // Regex-implemented validators here...
289
// associate(String.class, Character.TYPE, new RegexStringValidator(
290
// "^.$|^$", ".",
291
// BindingMessages.getString("Validate_CharacterHelp")));
292
// //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
293
// associate(String.class, Character.class, new
294
// RegexStringValidator(
295
// "^.$|^$", ".",
296
// BindingMessages.getString("Validate_CharacterHelp")));
297
// //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
298
// associate(String.class, Boolean.TYPE, new RegexStringValidator(
299
// BindingMessages.getString("Validate_BooleanPartialValidRegex"),
300
// //$NON-NLS-1$
301
// BindingMessages.getString("Validate_BooleanValidRegex"),
302
// //$NON-NLS-1$
303
// BindingMessages.getString("Validate_BooleanHelp")));
304
// //$NON-NLS-1$
305
// associate(String.class, Boolean.class, new RegexStringValidator(
306
// BindingMessages.getString("Validate_BooleanPartialValidRegex"),
307
// //$NON-NLS-1$
308
// BindingMessages.getString("Validate_BooleanValidRegex"),
309
// //$NON-NLS-1$
310
// BindingMessages.getString("Validate_BooleanHelp")));
311
// //$NON-NLS-1$
312
// associate(String.class, String.class, new
313
// RegexStringValidator("^.*$", "^.*$", "")); //$NON-NLS-1$
314
// //$NON-NLS-2$ //$NON-NLS-3$
315
}
316     }
317
318 }
Popular Tags