1 11 12 package org.eclipse.core.databinding; 13 14 import org.eclipse.core.databinding.observable.value.IObservableValue; 15 import org.eclipse.core.databinding.observable.value.IValueChangeListener; 16 import org.eclipse.core.databinding.observable.value.ValueChangeEvent; 17 import org.eclipse.core.databinding.observable.value.WritableValue; 18 import org.eclipse.core.databinding.util.Policy; 19 import org.eclipse.core.internal.databinding.BindingStatus; 20 import org.eclipse.core.internal.databinding.Util; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.MultiStatus; 23 import org.eclipse.core.runtime.Status; 24 25 29 class ValueBinding extends Binding { 30 private final UpdateValueStrategy targetToModel; 31 private final UpdateValueStrategy modelToTarget; 32 private WritableValue validationStatusObservable; 33 private IObservableValue target; 34 private IObservableValue model; 35 36 private boolean updatingTarget; 37 private boolean updatingModel; 38 private IValueChangeListener targetChangeListener = new IValueChangeListener() { 39 public void handleValueChange(ValueChangeEvent event) { 40 if (!updatingTarget && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) { 41 doUpdate(target, model, targetToModel, false, false); 42 } 43 } 44 }; 45 private IValueChangeListener modelChangeListener = new IValueChangeListener() { 46 public void handleValueChange(ValueChangeEvent event) { 47 if (!updatingModel && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) { 48 doUpdate(model, target, modelToTarget, false, false); 49 } 50 } 51 }; 52 53 59 public ValueBinding(IObservableValue targetObservableValue, 60 IObservableValue modelObservableValue, 61 UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) { 62 super(targetObservableValue, modelObservableValue); 63 this.target = targetObservableValue; 64 this.model = modelObservableValue; 65 this.targetToModel = targetToModel; 66 this.modelToTarget = modelToTarget; 67 if ((targetToModel.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) != 0) { 68 target.addValueChangeListener(targetChangeListener); 69 } else { 70 targetChangeListener = null; 71 } 72 if ((modelToTarget.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) != 0) { 73 model.addValueChangeListener(modelChangeListener); 74 } else { 75 modelChangeListener = null; 76 } 77 } 78 79 protected void preInit() { 80 validationStatusObservable = new WritableValue(context 81 .getValidationRealm(), Status.OK_STATUS, IStatus.class); 82 } 83 84 protected void postInit() { 85 if (modelToTarget.getUpdatePolicy() == UpdateValueStrategy.POLICY_UPDATE) { 86 updateModelToTarget(); 87 } 88 if (targetToModel.getUpdatePolicy() != UpdateValueStrategy.POLICY_NEVER) { 89 validateTargetToModel(); 90 } 91 } 92 93 public IObservableValue getValidationStatus() { 94 return validationStatusObservable; 95 } 96 97 public void updateTargetToModel() { 98 doUpdate(target, model, targetToModel, true, false); 99 } 100 101 public void updateModelToTarget() { 102 doUpdate(model, target, modelToTarget, true, false); 103 } 104 105 113 boolean mergeStatus(MultiStatus multiStatus, IStatus newStatus) { 114 if (!newStatus.isOK()) { 115 multiStatus.add(newStatus); 116 return multiStatus.getSeverity() < IStatus.ERROR; 117 } 118 return true; 119 } 120 121 126 private void doUpdate(final IObservableValue source, 127 final IObservableValue destination, 128 final UpdateValueStrategy updateValueStrategy, 129 final boolean explicit, final boolean validateOnly) { 130 final int policy = updateValueStrategy.getUpdatePolicy(); 131 final MultiStatus[] statusHolder = { BindingStatus.ok() }; 132 if (policy != UpdateValueStrategy.POLICY_NEVER) { 133 if (policy != UpdateValueStrategy.POLICY_ON_REQUEST || explicit) { 134 source.getRealm().exec(new Runnable () { 135 public void run() { 136 boolean destinationRealmReached = false; 137 try { 138 Object value = source.getValue(); 139 IStatus status = updateValueStrategy 140 .validateAfterGet(value); 141 if (mergeStatus(statusHolder[0], status)) { 142 final Object convertedValue = updateValueStrategy 143 .convert(value); 144 status = updateValueStrategy 145 .validateAfterConvert(convertedValue); 146 if (mergeStatus(statusHolder[0], status)) { 147 if (policy == UpdateValueStrategy.POLICY_CONVERT 148 && !explicit) { 149 } else { 150 status = updateValueStrategy 151 .validateBeforeSet(convertedValue); 152 if (mergeStatus(statusHolder[0], status)) { 153 if (!validateOnly) { 154 destinationRealmReached = true; 155 destination.getRealm().exec( 156 new Runnable () { 157 public void run() { 158 if (destination == target) { 159 updatingTarget = true; 160 } else { 161 updatingModel = true; 162 } 163 try { 164 IStatus setterStatus = updateValueStrategy 165 .doSet( 166 destination, 167 convertedValue); 168 169 mergeStatus( 170 statusHolder[0], 171 setterStatus); 172 } finally { 173 if (destination == target) { 174 updatingTarget = false; 175 } else { 176 updatingModel = false; 177 } 178 setValidationStatus(statusHolder[0]); 179 } 180 } 181 }); 182 } 183 } 184 } 185 } 186 } 187 } catch (Exception ex) { 188 String message = (ex.getMessage() != null) ? ex 191 .getMessage() : ""; 193 mergeStatus(statusHolder[0], new Status( 194 IStatus.ERROR, Policy.JFACE_DATABINDING, 195 IStatus.ERROR, message, ex)); 196 } finally { 197 if (!destinationRealmReached) { 198 setValidationStatus(statusHolder[0]); 199 } 200 201 } 202 } 203 }); 204 } 205 } 206 } 207 208 public void validateModelToTarget() { 209 doUpdate(model, target, modelToTarget, true, true); 210 } 211 212 public void validateTargetToModel() { 213 doUpdate(target, model, targetToModel, true, true); 214 } 215 216 private void setValidationStatus(final IStatus status) { 217 validationStatusObservable.getRealm().exec(new Runnable () { 218 public void run() { 219 validationStatusObservable.setValue(status); 220 } 221 }); 222 } 223 224 public void dispose() { 225 if (targetChangeListener != null) { 226 target.removeValueChangeListener(targetChangeListener); 227 targetChangeListener = null; 228 } 229 if (modelChangeListener != null) { 230 model.removeValueChangeListener(modelChangeListener); 231 modelChangeListener = null; 232 } 233 target = null; 234 model = null; 235 super.dispose(); 236 } 237 238 } 239 | Popular Tags |