1 15 package org.eclipse.core.databinding; 16 17 import java.util.Iterator ; 18 19 import org.eclipse.core.databinding.observable.Observables; 20 import org.eclipse.core.databinding.observable.Realm; 21 import org.eclipse.core.databinding.observable.list.IObservableList; 22 import org.eclipse.core.databinding.observable.list.WritableList; 23 import org.eclipse.core.databinding.observable.map.IObservableMap; 24 import org.eclipse.core.databinding.observable.value.IObservableValue; 25 import org.eclipse.core.internal.databinding.ValidationStatusMap; 26 import org.eclipse.core.runtime.Assert; 27 import org.eclipse.core.runtime.IStatus; 28 29 53 public class DataBindingContext { 54 private WritableList bindings; 55 56 59 private IObservableList unmodifiableBindings; 60 61 private IObservableMap validationStatusMap; 62 63 private Realm validationRealm; 64 65 71 public DataBindingContext() { 72 this(Realm.getDefault()); 73 } 74 75 84 public DataBindingContext(Realm validationRealm) { 85 Assert.isNotNull(validationRealm); 86 this.validationRealm = validationRealm; 87 bindings = new WritableList(validationRealm); 88 89 unmodifiableBindings = Observables.unmodifiableObservableList(bindings); 90 validationStatusMap = new ValidationStatusMap(validationRealm, 91 bindings); 92 } 93 94 115 public final Binding bindValue(IObservableValue targetObservableValue, 116 IObservableValue modelObservableValue, 117 UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) { 118 UpdateValueStrategy targetToModelStrategy = targetToModel != null ? targetToModel 119 : createTargetToModelUpdateValueStrategy(targetObservableValue, modelObservableValue); 120 UpdateValueStrategy modelToTargetStrategy = modelToTarget != null ? modelToTarget 121 : createModelToTargetUpdateValueStrategy(modelObservableValue, targetObservableValue); 122 targetToModelStrategy.fillDefaults(targetObservableValue, modelObservableValue); 123 modelToTargetStrategy.fillDefaults(modelObservableValue, targetObservableValue); 124 ValueBinding result = new ValueBinding(targetObservableValue, 125 modelObservableValue, targetToModelStrategy, 126 modelToTargetStrategy); 127 result.init(this); 128 return result; 129 } 130 131 139 protected UpdateValueStrategy createModelToTargetUpdateValueStrategy( 140 IObservableValue fromValue, IObservableValue toValue) { 141 return new UpdateValueStrategy(); 142 } 143 144 152 protected UpdateValueStrategy createTargetToModelUpdateValueStrategy( 153 IObservableValue fromValue, IObservableValue toValue) { 154 return new UpdateValueStrategy(); 155 } 156 157 178 public final Binding bindList(IObservableList targetObservableList, 179 IObservableList modelObservableList, 180 UpdateListStrategy targetToModel, UpdateListStrategy modelToTarget) { 181 UpdateListStrategy targetToModelStrategy = targetToModel != null ? targetToModel 182 : createTargetToModelUpdateListStrategy(targetObservableList, 183 modelObservableList); 184 UpdateListStrategy modelToTargetStrategy = modelToTarget != null ? modelToTarget 185 : createModelToTargetUpdateListStrategy(modelObservableList, 186 targetObservableList); 187 targetToModelStrategy.fillDefaults(targetObservableList, 188 modelObservableList); 189 modelToTargetStrategy.fillDefaults(modelObservableList, 190 targetObservableList); 191 ListBinding result = new ListBinding(targetObservableList, 192 modelObservableList, targetToModelStrategy, 193 modelToTargetStrategy); 194 result.init(this); 195 return result; 196 } 197 198 203 protected UpdateListStrategy createModelToTargetUpdateListStrategy( 204 IObservableList modelObservableList, 205 IObservableList targetObservableList) { 206 return new UpdateListStrategy(); 207 } 208 209 214 protected UpdateListStrategy createTargetToModelUpdateListStrategy( 215 IObservableList targetObservableList, 216 IObservableList modelObservableList) { 217 return new UpdateListStrategy(); 218 } 219 220 224 public final void dispose() { 225 Binding[] bindingArray = (Binding[]) bindings.toArray(new Binding[bindings.size()]); 226 for (int i = 0; i < bindingArray.length; i++) { 227 bindingArray[i].dispose(); 228 } 229 } 230 231 237 public final IObservableList getBindings() { 238 return unmodifiableBindings; 239 } 240 241 249 public final IObservableMap getValidationStatusMap() { 250 return validationStatusMap; 251 } 252 253 259 public void addBinding(Binding binding) { 260 bindings.add(binding); 261 } 262 263 268 public final void updateModels() { 269 for (Iterator it = bindings.iterator(); it.hasNext();) { 270 Binding binding = (Binding) it.next(); 271 binding.updateTargetToModel(); 272 } 273 } 274 275 280 public final void updateTargets() { 281 for (Iterator it = bindings.iterator(); it.hasNext();) { 282 Binding binding = (Binding) it.next(); 283 binding.updateModelToTarget(); 284 } 285 } 286 287 294 public boolean removeBinding(Binding binding) { 295 return bindings.remove(binding); 296 } 297 298 304 public final Realm getValidationRealm() { 305 return validationRealm; 306 } 307 } 308 | Popular Tags |