1 11 12 package org.eclipse.core.databinding; 13 14 import java.util.Date ; 15 import java.util.HashMap ; 16 17 import org.eclipse.core.databinding.conversion.IConverter; 18 import org.eclipse.core.databinding.observable.value.IObservableValue; 19 import org.eclipse.core.databinding.validation.IValidator; 20 import org.eclipse.core.databinding.validation.ValidationStatus; 21 import org.eclipse.core.internal.databinding.BindingMessages; 22 import org.eclipse.core.internal.databinding.Pair; 23 import org.eclipse.core.internal.databinding.conversion.NumberToBigDecimalConverter; 24 import org.eclipse.core.internal.databinding.conversion.NumberToBigIntegerConverter; 25 import org.eclipse.core.internal.databinding.conversion.NumberToByteConverter; 26 import org.eclipse.core.internal.databinding.conversion.NumberToDoubleConverter; 27 import org.eclipse.core.internal.databinding.conversion.NumberToFloatConverter; 28 import org.eclipse.core.internal.databinding.conversion.NumberToIntegerConverter; 29 import org.eclipse.core.internal.databinding.conversion.NumberToLongConverter; 30 import org.eclipse.core.internal.databinding.conversion.NumberToNumberConverter; 31 import org.eclipse.core.internal.databinding.conversion.NumberToShortConverter; 32 import org.eclipse.core.internal.databinding.conversion.StringToDateConverter; 33 import org.eclipse.core.internal.databinding.validation.NumberFormatConverter; 34 import org.eclipse.core.internal.databinding.validation.NumberToByteValidator; 35 import org.eclipse.core.internal.databinding.validation.NumberToDoubleValidator; 36 import org.eclipse.core.internal.databinding.validation.NumberToFloatValidator; 37 import org.eclipse.core.internal.databinding.validation.NumberToIntegerValidator; 38 import org.eclipse.core.internal.databinding.validation.NumberToLongValidator; 39 import org.eclipse.core.internal.databinding.validation.NumberToShortValidator; 40 import org.eclipse.core.internal.databinding.validation.NumberToUnboundedNumberValidator; 41 import org.eclipse.core.internal.databinding.validation.ObjectToPrimitiveValidator; 42 import org.eclipse.core.internal.databinding.validation.StringToByteValidator; 43 import org.eclipse.core.internal.databinding.validation.StringToDateValidator; 44 import org.eclipse.core.internal.databinding.validation.StringToDoubleValidator; 45 import org.eclipse.core.internal.databinding.validation.StringToFloatValidator; 46 import org.eclipse.core.internal.databinding.validation.StringToIntegerValidator; 47 import org.eclipse.core.internal.databinding.validation.StringToLongValidator; 48 import org.eclipse.core.internal.databinding.validation.StringToShortValidator; 49 import org.eclipse.core.runtime.IStatus; 50 import org.eclipse.core.runtime.Status; 51 52 98 public class UpdateValueStrategy extends UpdateStrategy { 99 100 105 public static int POLICY_NEVER = notInlined(1); 106 107 112 public static int POLICY_ON_REQUEST = notInlined(2); 113 114 120 public static int POLICY_CONVERT = notInlined(4); 121 122 128 public static int POLICY_UPDATE = notInlined(8); 129 130 139 private static int notInlined(int i) { 140 return i; 141 } 142 143 protected IValidator afterGetValidator; 144 protected IValidator afterConvertValidator; 145 protected IValidator beforeSetValidator; 146 protected IConverter converter; 147 148 private int updatePolicy; 149 150 private static ValidatorRegistry validatorRegistry = new ValidatorRegistry(); 151 private static HashMap validatorsByConverter = new HashMap (); 152 153 protected boolean provideDefaults; 154 155 158 private boolean defaultedConverter = false; 159 160 166 public UpdateValueStrategy() { 167 this(true, POLICY_UPDATE); 168 } 169 170 179 public UpdateValueStrategy(int updatePolicy) { 180 this(true, updatePolicy); 181 } 182 183 197 public UpdateValueStrategy(boolean provideDefaults, int updatePolicy) { 198 this.provideDefaults = provideDefaults; 199 this.updatePolicy = updatePolicy; 200 } 201 202 213 public Object convert(Object value) { 214 return converter == null ? value : converter.convert(value); 215 } 216 217 226 protected IValidator createValidator(Object fromType, Object toType) { 227 if (fromType == null || toType == null) { 228 return new IValidator() { 229 230 public IStatus validate(Object value) { 231 return Status.OK_STATUS; 232 } 233 }; 234 } 235 236 return findValidator(fromType, toType); 237 } 238 239 250 protected void fillDefaults(IObservableValue source, 251 IObservableValue destination) { 252 Object sourceType = source.getValueType(); 253 Object destinationType = destination.getValueType(); 254 if (provideDefaults && sourceType != null && destinationType != null) { 255 if (converter == null) { 256 IConverter converter = createConverter(sourceType, 257 destinationType); 258 defaultedConverter = (converter != null); 259 setConverter(converter); 260 } 261 262 if (afterGetValidator == null) { 263 afterGetValidator = createValidator(sourceType, destinationType); 264 } 265 } 266 if (converter != null) { 267 if (sourceType != null) { 268 checkAssignable(converter.getFromType(), sourceType, 269 "converter does not convert from type " + sourceType); } 271 if (destinationType != null) { 272 checkAssignable(converter.getToType(), destinationType, 273 "converter does not convert to type " + destinationType); } 275 } 276 } 277 278 private IValidator findValidator(Object fromType, Object toType) { 279 IValidator result = null; 280 281 if (defaultedConverter) { 284 if (String .class.equals(fromType)) { 285 result = (IValidator) validatorsByConverter.get(converter); 286 287 if (result == null) { 288 if (Integer .class.equals(toType) 290 || Integer.TYPE.equals(toType)) { 291 result = new StringToIntegerValidator((NumberFormatConverter) converter); 292 } else if (Long .class.equals(toType) 293 || Long.TYPE.equals(toType)) { 294 result = new StringToLongValidator((NumberFormatConverter) converter); 295 } else if (Float .class.equals(toType) 296 || Float.TYPE.equals(toType)) { 297 result = new StringToFloatValidator((NumberFormatConverter) converter); 298 } else if (Double .class.equals(toType) 299 || Double.TYPE.equals(toType)) { 300 result = new StringToDoubleValidator((NumberFormatConverter) converter); 301 } else if (Byte .class.equals(toType) 302 || Byte.TYPE.equals(toType)) { 303 result = new StringToByteValidator((NumberFormatConverter) converter); 304 } else if (Short .class.equals(toType) 305 || Short.TYPE.equals(toType)) { 306 result = new StringToShortValidator((NumberFormatConverter) converter); 307 } else if (Date .class.equals(toType) 308 && converter instanceof StringToDateConverter) { 309 result = new StringToDateValidator( 310 (StringToDateConverter) converter); 311 } 312 313 if (result != null) { 314 validatorsByConverter.put(converter, result); 315 } 316 } 317 } else if (converter instanceof NumberToNumberConverter) { 318 result = (IValidator) validatorsByConverter.get(converter); 319 320 if (result == null) { 321 if (converter instanceof NumberToByteConverter) { 322 result = new NumberToByteValidator((NumberToByteConverter) converter); 323 } else if (converter instanceof NumberToShortConverter) { 324 result = new NumberToShortValidator((NumberToShortConverter) converter); 325 } else if (converter instanceof NumberToIntegerConverter) { 326 result = new NumberToIntegerValidator((NumberToIntegerConverter) converter); 327 } else if (converter instanceof NumberToLongConverter) { 328 result = new NumberToLongValidator((NumberToLongConverter) converter); 329 } else if (converter instanceof NumberToFloatConverter) { 330 result = new NumberToFloatValidator((NumberToFloatConverter) converter); 331 } else if (converter instanceof NumberToDoubleConverter) { 332 result = new NumberToDoubleValidator((NumberToDoubleConverter) converter); 333 } else if (converter instanceof NumberToBigIntegerConverter || converter instanceof NumberToBigDecimalConverter) { 334 result = new NumberToUnboundedNumberValidator((NumberToNumberConverter) converter); 335 } 336 } 337 } 338 339 if (result == null) { 340 result = validatorRegistry.get(fromType, toType); 342 } 343 } 344 345 return result; 346 } 347 348 351 public int getUpdatePolicy() { 352 return updatePolicy; 353 } 354 355 362 public UpdateValueStrategy setAfterConvertValidator(IValidator validator) { 363 this.afterConvertValidator = validator; 364 return this; 365 } 366 367 374 public UpdateValueStrategy setAfterGetValidator(IValidator validator) { 375 this.afterGetValidator = validator; 376 return this; 377 } 378 379 386 public UpdateValueStrategy setBeforeSetValidator(IValidator validator) { 387 this.beforeSetValidator = validator; 388 return this; 389 } 390 391 398 public UpdateValueStrategy setConverter(IConverter converter) { 399 this.converter = converter; 400 return this; 401 } 402 403 414 public IStatus validateAfterConvert(Object value) { 415 return afterConvertValidator == null ? Status.OK_STATUS 416 : afterConvertValidator.validate(value); 417 } 418 419 430 public IStatus validateAfterGet(Object value) { 431 return afterGetValidator == null ? Status.OK_STATUS : afterGetValidator 432 .validate(value); 433 } 434 435 446 public IStatus validateBeforeSet(Object value) { 447 return beforeSetValidator == null ? Status.OK_STATUS 448 : beforeSetValidator.validate(value); 449 } 450 451 459 protected IStatus doSet(IObservableValue observableValue, Object value) { 460 try { 461 observableValue.setValue(value); 462 } catch (Exception ex) { 463 return ValidationStatus.error(BindingMessages 464 .getString("ValueBinding_ErrorWhileSettingValue"), ex); 466 } 467 return Status.OK_STATUS; 468 } 469 470 private static class ValidatorRegistry { 471 472 private HashMap validators = new HashMap (); 473 474 479 private ValidatorRegistry() { 480 associate(Integer .class, Integer.TYPE, 482 new ObjectToPrimitiveValidator(Integer.TYPE)); 483 associate(Byte .class, Byte.TYPE, new ObjectToPrimitiveValidator( 484 Byte.TYPE)); 485 associate(Short .class, Short.TYPE, new ObjectToPrimitiveValidator( 486 Short.TYPE)); 487 associate(Long .class, Long.TYPE, new ObjectToPrimitiveValidator( 488 Long.TYPE)); 489 associate(Float .class, Float.TYPE, new ObjectToPrimitiveValidator( 490 Float.TYPE)); 491 associate(Double .class, Double.TYPE, 492 new ObjectToPrimitiveValidator(Double.TYPE)); 493 associate(Boolean .class, Boolean.TYPE, 494 new ObjectToPrimitiveValidator(Boolean.TYPE)); 495 496 associate(Object .class, Integer.TYPE, 497 new ObjectToPrimitiveValidator(Integer.TYPE)); 498 associate(Object .class, Byte.TYPE, new ObjectToPrimitiveValidator( 499 Byte.TYPE)); 500 associate(Object .class, Short.TYPE, new ObjectToPrimitiveValidator( 501 Short.TYPE)); 502 associate(Object .class, Long.TYPE, new ObjectToPrimitiveValidator( 503 Long.TYPE)); 504 associate(Object .class, Float.TYPE, new ObjectToPrimitiveValidator( 505 Float.TYPE)); 506 associate(Object .class, Double.TYPE, 507 new ObjectToPrimitiveValidator(Double.TYPE)); 508 associate(Object .class, Boolean.TYPE, 509 new ObjectToPrimitiveValidator(Boolean.TYPE)); 510 } 511 512 523 private void associate(Object fromClass, Object toClass, 524 IValidator validator) { 525 validators.put(new Pair(fromClass, toClass), validator); 526 } 527 528 537 private IValidator get(Object fromClass, Object toClass) { 538 IValidator result = (IValidator) validators.get(new Pair(fromClass, 539 toClass)); 540 if (result != null) 541 return result; 542 if (fromClass != null && toClass != null && fromClass == toClass) { 543 return new IValidator() { 544 public IStatus validate(Object value) { 545 return Status.OK_STATUS; 546 } 547 }; 548 } 549 return new IValidator() { 550 public IStatus validate(Object value) { 551 return Status.OK_STATUS; 552 } 553 }; 554 } 555 } 556 557 } 558 | Popular Tags |