1 11 12 package org.eclipse.jface.internal.databinding.provisional.factories; 13 14 import java.math.BigDecimal ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Map ; 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 46 public final class DefaultBindSupportFactory extends BindSupportFactory { 47 48 private ValidatorRegistry validatorRegistry = new ValidatorRegistry(); 49 50 private Map converterMap; 51 52 public IValidator createValidator(Object fromType, Object toType) { 53 if (fromType == null || toType == null) { 54 return new IValidator() { 55 56 public ValidationError isPartiallyValid(Object value) { 57 return null; 58 } 59 60 public ValidationError isValid(Object 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); } 71 return dataTypeValidator; 72 } 73 74 private IValidator findValidator(Object fromType, Object toType) { 75 return validatorRegistry.get(fromType, toType); 77 } 78 79 public IConverter createConverter(Object fromType, Object toType) { 80 if (!(fromType instanceof Class ) || !(toType instanceof Class )) { 81 return null; 82 } 83 Class toClass = (Class ) toType; 84 if (toClass.isPrimitive()) { 85 toClass = autoboxed(toClass); 86 } 87 Class fromClass = (Class ) fromType; 88 if (fromClass.isPrimitive()) { 89 fromClass = autoboxed(fromClass); 90 } 91 if (toClass.isAssignableFrom(fromClass)) { 92 return new IdentityConverter(fromClass, toClass); 93 } 94 Map converterMap = getConverterMap(); 95 Class [] supertypeHierarchyFlattened = ClassLookupSupport 96 .getTypeHierarchyFlattened(fromClass); 97 for (int i = 0; i < supertypeHierarchyFlattened.length; i++) { 98 Class currentFromClass = supertypeHierarchyFlattened[i]; 99 if (currentFromClass == toType) { 100 return new IdentityConverter(fromClass, toClass); 102 } 103 Pair key = new Pair(currentFromClass.getName(), toClass.getName()); 104 Object converterOrClassname = converterMap.get(key); 105 if (converterOrClassname instanceof IConverter) { 106 return (IConverter) converterOrClassname; 107 } else if (converterOrClassname instanceof String ) { 108 String classname = (String ) converterOrClassname; 109 Class 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 e) { 117 e.printStackTrace(); 118 } catch (InstantiationException e) { 119 e.printStackTrace(); 120 } catch (IllegalAccessException e) { 121 e.printStackTrace(); 122 } 123 } 124 } 125 if (fromClass.isAssignableFrom(toClass)) { 127 return new IdentityConverter(fromClass, toClass); 128 } 129 return null; 130 } 131 132 private Map getConverterMap() { 133 if (converterMap == null) { 135 converterMap = new HashMap (); 136 converterMap 137 .put( 138 new Pair("java.util.Date", "java.lang.String"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertDate2String"); converterMap 140 .put( 141 new Pair("java.lang.String", "java.math.BigDecimal"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2BigDecimal"); converterMap 143 .put( 144 new Pair("java.lang.String", "java.lang.Boolean"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Boolean"); converterMap 146 .put( 147 new Pair("java.lang.String", "java.lang.Byte"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Byte"); converterMap 149 .put( 150 new Pair("java.lang.String", "java.lang.Character"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Character"); converterMap 152 .put( 153 new Pair("java.lang.String", "java.util.Date"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Date"); converterMap 155 .put( 156 new Pair("java.lang.String", "java.lang.Double"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Double"); converterMap 158 .put( 159 new Pair("java.lang.String", "java.lang.Float"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Float"); converterMap 161 .put( 162 new Pair("java.lang.String", "java.lang.Integer"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Integer"); converterMap 164 .put( 165 new Pair("java.lang.String", "java.lang.Long"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Long"); converterMap 167 .put( 168 new Pair("java.lang.String", "java.lang.Short"), "org.eclipse.jface.internal.databinding.provisional.conversion.ConvertString2Short"); converterMap 170 .put( 171 new Pair("java.lang.Object", "java.lang.String"), "org.eclipse.jface.internal.databinding.provisional.conversion.ToStringConverter"); } 173 return converterMap; 174 } 175 176 public IDomainValidator createDomainValidator(Object modelType) { 177 return new IDomainValidator() { 178 public ValidationError isValid(Object value) { 179 return null; 180 } 181 }; 182 } 183 184 public Boolean isAssignableFromTo(Object fromType, Object toType) { 185 if (fromType instanceof Class && toType instanceof Class ) { 186 Class toClass = (Class ) toType; 187 if (toClass.isPrimitive()) { 188 toClass = autoboxed(toClass); 189 } 190 Class fromClass = (Class ) 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 autoboxed(Class clazz) { 200 if (clazz == Float.TYPE) 201 return Float .class; 202 else if (clazz == Double.TYPE) 203 return Double .class; 204 else if (clazz == Short.TYPE) 205 return Short .class; 206 else if (clazz == Integer.TYPE) 207 return Integer .class; 208 else if (clazz == Long.TYPE) 209 return Long .class; 210 else if (clazz == Boolean.TYPE) 211 return Boolean .class; 212 return clazz; 213 } 214 215 private static class ValidatorRegistry { 216 217 private HashMap validators = new HashMap (); 218 219 230 private void associate(Object fromClass, Object toClass, 231 IValidator validator) { 232 validators.put(new Pair(fromClass, toClass), validator); 233 } 234 235 244 private IValidator get(Object fromClass, Object 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 value) { 252 return null; 253 } 254 255 public ValidationError isValid(Object value) { 256 return null; 257 } 258 }; 259 } 260 return ReadOnlyValidator.getDefault(); 261 } 262 263 268 private ValidatorRegistry() { 269 associate(String .class, Integer.TYPE, new String2IntValidator()); 271 associate(String .class, Byte.TYPE, new String2ByteValidator()); 272 associate(String .class, Short.TYPE, new String2ShortValidator()); 273 associate(String .class, Long.TYPE, new String2LongValidator()); 274 associate(String .class, Float.TYPE, new String2FloatValidator()); 275 associate(String .class, Double.TYPE, new String2DoubleValidator()); 276 277 associate(String .class, Integer .class, new String2IntValidator()); 278 associate(String .class, Byte .class, new String2ByteValidator()); 279 associate(String .class, Short .class, new String2ShortValidator()); 280 associate(String .class, Long .class, new String2LongValidator()); 281 associate(String .class, Float .class, new String2FloatValidator()); 282 associate(String .class, Double .class, new String2DoubleValidator()); 283 associate(String .class, Date .class, new String2DateValidator()); 284 285 associate(String .class, BigDecimal .class, 286 new String2BigDecimalValidator()); 287 288 } 316 } 317 318 } | Popular Tags |