1 11 package org.eclipse.jface.internal.databinding.provisional; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.jface.internal.databinding.internal.ValidationErrorList; 18 import org.eclipse.jface.internal.databinding.provisional.conversion.IConverter; 19 import org.eclipse.jface.internal.databinding.provisional.factories.BindSupportFactory; 20 import org.eclipse.jface.internal.databinding.provisional.factories.DefaultBindSupportFactory; 21 import org.eclipse.jface.internal.databinding.provisional.factories.DefaultBindingFactory; 22 import org.eclipse.jface.internal.databinding.provisional.factories.IBindingFactory; 23 import org.eclipse.jface.internal.databinding.provisional.factories.IObservableFactory; 24 import org.eclipse.jface.internal.databinding.provisional.observable.IObservable; 25 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList; 26 import org.eclipse.jface.internal.databinding.provisional.observable.list.ObservableList; 27 import org.eclipse.jface.internal.databinding.provisional.observable.list.WritableList; 28 import org.eclipse.jface.internal.databinding.provisional.observable.value.ComputedValue; 29 import org.eclipse.jface.internal.databinding.provisional.observable.value.IObservableValue; 30 import org.eclipse.jface.internal.databinding.provisional.validation.IDomainValidator; 31 import org.eclipse.jface.internal.databinding.provisional.validation.IValidator; 32 import org.eclipse.jface.internal.databinding.provisional.validation.ValidationError; 33 import org.eclipse.jface.util.Assert; 34 35 55 public final class DataBindingContext { 56 57 62 public static final int POLICY_AUTOMATIC = 1; 63 64 69 public static final int POLICY_EXPLICIT = 2; 70 71 75 public static final int TIME_EARLY = 0; 76 77 81 public static final int TIME_LATE = 1; 82 83 89 public static DataBindingContext createContext(DataBindingContext parent) { 90 DataBindingContext result = new DataBindingContext(parent); 91 return result; 92 } 93 94 103 public static DataBindingContext createContext( 104 IObservableFactory[] observableFactories) { 105 return createContext(observableFactories, 106 new BindSupportFactory[] { new DefaultBindSupportFactory() }, 107 new IBindingFactory[] { new DefaultBindingFactory() }); 108 } 109 110 121 public static DataBindingContext createContext( 122 IObservableFactory[] observableFactories, 123 BindSupportFactory[] bindSupportFactories, 124 IBindingFactory[] bindingFactories) { 125 DataBindingContext result = new DataBindingContext(); 126 if (observableFactories != null) 127 for (int i = 0; i < observableFactories.length; i++) { 128 result.addObservableFactory(observableFactories[i]); 129 } 130 if (bindSupportFactories != null) 131 for (int i = 0; i < bindSupportFactories.length; i++) { 132 result.addBindSupportFactory(bindSupportFactories[i]); 133 } 134 if (bindingFactories != null) 135 for (int i = 0; i < bindingFactories.length; i++) { 136 result.addBindingFactory(bindingFactories[i]); 137 } 138 return result; 139 } 140 141 private List bindingEventListeners = new ArrayList (); 142 143 private WritableList bindings = new WritableList(); 144 145 private List bindSupportFactories = new ArrayList (); 146 147 private List bindingFactories = new ArrayList (); 148 149 private List createdObservables = new ArrayList (); 150 151 private List factories = new ArrayList (); 152 153 private DataBindingContext parent; 154 155 private ComputedValue partialValidationError = new ComputedValue() { 156 protected Object calculate() { 157 int size = partialValidationErrors.size(); 158 return size == 0 ? null : partialValidationErrors.get(size - 1); 159 } 160 }; 161 162 private ObservableList partialValidationErrors = new ValidationErrorList( 163 bindings, true); 164 165 private ComputedValue validationError = new ComputedValue() { 166 protected Object calculate() { 167 int size = validationErrors.size(); 168 return size == 0 ? null : validationErrors.get(size - 1); 169 } 170 }; 171 172 private ObservableList validationErrors = new ValidationErrorList(bindings, 173 false); 174 175 178 public DataBindingContext() { 179 } 180 181 185 public DataBindingContext(DataBindingContext parent) { 186 this.parent = parent; 187 } 188 189 197 public void addBindingEventListener(IBindingListener listener) { 198 bindingEventListeners.add(listener); 199 } 200 201 209 public void addBindSupportFactory(BindSupportFactory factory) { 210 bindSupportFactories.add(factory); 211 } 212 213 220 public void addObservableFactory(IObservableFactory observableFactory) { 221 factories.add(observableFactory); 226 } 227 228 233 public void addBindingFactory(IBindingFactory factory) { 234 bindingFactories.add(factory); 235 } 236 237 249 public Binding bind(IObservable targetObservable, 250 IObservable modelObservable, BindSpec bindSpec) { 251 Binding result = doCreateBinding(targetObservable, modelObservable, 252 bindSpec, this); 253 if (result != null) 254 return result; 255 throw new BindingException( 256 "No binding found for target: " + targetObservable.getClass().getName() + ", model: " + modelObservable.getClass().getName()); } 258 259 private Binding doCreateBinding(IObservable targetObservable, 260 IObservable modelObservable, BindSpec bindSpec, 261 DataBindingContext originatingContext) { 262 for (int i = bindingFactories.size() - 1; i >= 0; i--) { 263 IBindingFactory factory = (IBindingFactory) bindingFactories.get(i); 264 Binding binding = factory.createBinding(originatingContext, targetObservable, 265 modelObservable, bindSpec); 266 if (binding != null) { 267 bindings.add(binding); 268 return binding; 269 } 270 } 271 if (parent != null) { 272 return parent.doCreateBinding(targetObservable, modelObservable, 273 bindSpec, originatingContext); 274 } 275 return null; 276 } 277 278 289 public Binding bind(IObservable targetObservable, Object modelDescription, 290 BindSpec bindSpec) { 291 return bind(targetObservable, createObservable(modelDescription), 292 bindSpec); 293 } 294 295 306 public Binding bind(Object targetDescription, IObservable modelObservable, 307 BindSpec bindSpec) { 308 return bind(createObservable(targetDescription), modelObservable, 309 bindSpec); 310 } 311 312 323 public Binding bind(Object targetDescription, Object modelDescription, 324 BindSpec bindSpec) { 325 return bind(createObservable(targetDescription), modelDescription, 326 bindSpec); 327 } 328 329 342 public IConverter createConverter(Object fromType, Object toType) { 343 for (int i = bindSupportFactories.size() - 1; i >= 0; i--) { 344 BindSupportFactory bindSupportFactory = (BindSupportFactory) bindSupportFactories 345 .get(i); 346 IConverter converter = bindSupportFactory.createConverter(fromType, 347 toType); 348 if (converter != null) { 349 return converter; 350 } 351 } 352 if (parent != null) { 353 return parent.createConverter(fromType, toType); 354 } 355 return null; 356 } 357 358 362 public IDomainValidator createDomainValidator(Object modelType) { 363 for (int i = bindSupportFactories.size() - 1; i >= 0; i--) { 364 BindSupportFactory bindSupportFactory = (BindSupportFactory) bindSupportFactories 365 .get(i); 366 IDomainValidator validator = bindSupportFactory 367 .createDomainValidator(modelType); 368 if (validator != null) { 369 return validator; 370 } 371 } 372 if (parent != null) { 373 return parent.createDomainValidator(modelType); 374 } 375 return null; 376 } 377 378 386 public IObservable createObservable(Object description) { 387 IObservable updatable = doCreateObservable(description, this); 388 if (updatable != null) { 389 createdObservables.add(updatable); 390 } 391 return updatable; 392 } 393 394 408 public IValidator createValidator(Object fromType, Object toType) { 409 for (int i = bindSupportFactories.size() - 1; i >= 0; i--) { 410 BindSupportFactory bindSupportFactory = (BindSupportFactory) bindSupportFactories 411 .get(i); 412 IValidator validator = bindSupportFactory.createValidator(fromType, 413 toType); 414 if (validator != null) { 415 return validator; 416 } 417 } 418 if (parent != null) { 419 return parent.createValidator(fromType, toType); 420 } 421 return null; 422 } 423 424 428 public void dispose() { 429 for (Iterator it = createdObservables.iterator(); it.hasNext();) { 430 IObservable updatable = (IObservable) it.next(); 431 updatable.dispose(); 432 } 433 } 434 435 private IObservable doCreateObservable(Object description, 436 DataBindingContext thisDatabindingContext) { 437 for (int i = factories.size() - 1; i >= 0; i--) { 438 IObservableFactory factory = (IObservableFactory) factories.get(i); 439 IObservable result = factory.createObservable(description); 440 if (result != null) { 441 return result; 442 } 443 } 444 if (parent != null) { 445 return parent.doCreateObservable(description, 446 thisDatabindingContext); 447 } 448 throw new BindingException("could not find updatable for " + description); 450 } 451 452 458 public void fillBindSpecDefaults(DataBindingContext dataBindingContext, 459 BindSpec bindSpec, Object targetType, Object modelType) { 460 if (bindSpec.getTypeConversionValidator() == null) { 461 bindSpec.setValidator(dataBindingContext.createValidator( 462 targetType, modelType)); 463 } 464 if (bindSpec.getDomainValidator() == null) { 465 bindSpec.setDomainValidator(dataBindingContext 466 .createDomainValidator(modelType)); 467 } 468 if (bindSpec.getModelToTargetConverter() == null) { 469 bindSpec.setModelToTargetConverter(dataBindingContext 470 .createConverter(modelType, targetType)); 471 } 472 if (bindSpec.getTargetToModelConverter() == null) { 473 bindSpec.setTargetToModelConverter(dataBindingContext 474 .createConverter(targetType, modelType)); 475 } 476 } 477 478 ValidationError fireBindingEvent(BindingEvent event) { 479 ValidationError result = null; 480 for (Iterator bindingEventIter = bindingEventListeners.iterator(); bindingEventIter 481 .hasNext();) { 482 IBindingListener listener = (IBindingListener) bindingEventIter 483 .next(); 484 result = listener.bindingEvent(event); 485 if (result != null) 486 break; 487 } 488 return result; 489 } 490 491 497 public IObservableList getBindings() { 498 return bindings; 499 } 500 501 507 public IObservableValue getPartialValidationError() { 508 return partialValidationError; 509 } 510 511 518 public IObservableValue getValidationError() { 519 return validationError; 520 } 521 522 528 public IObservableList getValidationErrors() { 529 return validationErrors; 530 } 531 532 537 public boolean isAssignableFromTo(Object fromType, Object toType) { 538 for (int i = bindSupportFactories.size() - 1; i >= 0; i--) { 539 BindSupportFactory bindSupportFactory = (BindSupportFactory) bindSupportFactories 540 .get(i); 541 Boolean result = bindSupportFactory.isAssignableFromTo(fromType, 542 toType); 543 if (result != null) { 544 return result.booleanValue(); 545 } 546 } 547 if (parent != null) { 548 return parent.isAssignableFromTo(fromType, toType); 549 } 550 return true; 552 } 553 554 563 public void registerForDispose(IObservable observable) { 564 createdObservables.add(observable); 565 } 566 567 575 public void removeBindingEventListener(IBindingListener listener) { 576 bindingEventListeners.remove(listener); 577 } 578 579 584 public void updateModels() { 585 Assert.isTrue(false, "updateModels is not yet implemented"); } 587 588 593 public void updateTargets() { 594 Assert.isTrue(false, "updateTargets is not yet implemented"); } 596 597 } 598 | Popular Tags |