78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Matthew Hall - bug 220700
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.core.databinding.ValueBinding;
|
81
|
14 import org.eclipse.core.databinding.UpdateValueStrategy;
|
|
15 import org.eclipse.core.databinding.Binding;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
|
20 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
21 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
22 import org.eclipse.core.databinding.observable.value.WritableValue;
|
|
23 import org.eclipse.core.databinding.util.Policy;
|
|
24 import org.eclipse.core.internal.databinding.BindingStatus;
|
|
25 import org.eclipse.core.internal.databinding.Util;
|
|
26 import org.eclipse.core.runtime.IStatus;
|
|
27 import org.eclipse.core.runtime.MultiStatus;
|
|
28 import org.eclipse.core.runtime.Status;
|
|
29
|
|
30 /**
|
|
31 * @since 1.0
|
|
32 *
|
|
33 */
|
|
34 class ValueBinding : Binding {
|
|
35 private final UpdateValueStrategy targetToModel;
|
|
36 private final UpdateValueStrategy modelToTarget;
|
|
37 private WritableValue validationStatusObservable;
|
|
38 private IObservableValue target;
|
|
39 private IObservableValue model;
|
|
40
|
|
41 private bool updatingTarget;
|
|
42 private bool updatingModel;
|
|
43 private IValueChangeListener targetChangeListener = new class() IValueChangeListener {
|
|
44 public void handleValueChange(ValueChangeEvent event) {
|
|
45 if (!updatingTarget && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) {
|
|
46 doUpdate(target, model, targetToModel, false, false);
|
|
47 }
|
|
48 }
|
|
49 };
|
|
50 private IValueChangeListener modelChangeListener = new class() IValueChangeListener {
|
|
51 public void handleValueChange(ValueChangeEvent event) {
|
|
52 if (!updatingModel && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) {
|
|
53 doUpdate(model, target, modelToTarget, false, false);
|
|
54 }
|
|
55 }
|
|
56 };
|
|
57
|
|
58 /**
|
|
59 * @param targetObservableValue
|
|
60 * @param modelObservableValue
|
|
61 * @param targetToModel
|
|
62 * @param modelToTarget
|
|
63 */
|
|
64 public this(IObservableValue targetObservableValue,
|
|
65 IObservableValue modelObservableValue,
|
|
66 UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) {
|
|
67 super(targetObservableValue, modelObservableValue);
|
|
68 this.target = targetObservableValue;
|
|
69 this.model = modelObservableValue;
|
|
70 this.targetToModel = targetToModel;
|
|
71 this.modelToTarget = modelToTarget;
|
|
72 if ((targetToModel.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) !is 0) {
|
|
73 target.addValueChangeListener(targetChangeListener);
|
|
74 } else {
|
|
75 targetChangeListener = null;
|
|
76 }
|
|
77 if ((modelToTarget.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) !is 0) {
|
|
78 model.addValueChangeListener(modelChangeListener);
|
|
79 } else {
|
|
80 modelChangeListener = null;
|
|
81 }
|
|
82 }
|
|
83
|
|
84 protected void preInit() {
|
|
85 validationStatusObservable = new WritableValue(context
|
|
86 .getValidationRealm(), Status.OK_STATUS, IStatus.classinfo);
|
|
87 }
|
|
88
|
|
89 protected void postInit() {
|
|
90 if (modelToTarget.getUpdatePolicy() is UpdateValueStrategy.POLICY_UPDATE) {
|
|
91 updateModelToTarget();
|
|
92 }
|
|
93 if (targetToModel.getUpdatePolicy() !is UpdateValueStrategy.POLICY_NEVER) {
|
|
94 validateTargetToModel();
|
|
95 }
|
|
96 }
|
|
97
|
|
98 public IObservableValue getValidationStatus() {
|
|
99 return validationStatusObservable;
|
|
100 }
|
|
101
|
|
102 public void updateTargetToModel() {
|
|
103 doUpdate(target, model, targetToModel, true, false);
|
|
104 }
|
|
105
|
|
106 public void updateModelToTarget() {
|
|
107 doUpdate(model, target, modelToTarget, true, false);
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Incorporates the provided <code>newStats</code> into the
|
|
112 * <code>multieStatus</code>.
|
|
113 *
|
|
114 * @param multiStatus
|
|
115 * @param newStatus
|
|
116 * @return <code>true</code> if the update should proceed
|
|
117 */
|
|
118 /* package */bool mergeStatus(MultiStatus multiStatus, IStatus newStatus) {
|
|
119 if (!newStatus.isOK()) {
|
|
120 multiStatus.add(newStatus);
|
|
121 return multiStatus.getSeverity() < IStatus.ERROR;
|
|
122 }
|
|
123 return true;
|
|
124 }
|
|
125
|
|
126 /*
|
|
127 * This method may be moved to UpdateValueStrategy in the future if clients
|
|
128 * need more control over how the source value is copied to the destination
|
|
129 * observable.
|
|
130 */
|
|
131 private void doUpdate(IObservableValue source,
|
|
132 IObservableValue destination,
|
|
133 UpdateValueStrategy updateValueStrategy,
|
|
134 bool explicit, bool validateOnly) {
|
|
135
|
|
136 final int policy = updateValueStrategy.getUpdatePolicy();
|
|
137 if (policy is UpdateValueStrategy.POLICY_NEVER)
|
|
138 return;
|
|
139 if (policy is UpdateValueStrategy.POLICY_ON_REQUEST && !explicit)
|
|
140 return;
|
|
141
|
|
142 source.getRealm().exec(dgRunnable((
|
|
143 IObservableValue source_,
|
|
144 IObservableValue destination_,
|
|
145 UpdateValueStrategy updateValueStrategy_,
|
|
146 bool explicit_, bool validateOnly_) {
|
|
147 bool destinationRealmReached = false;
|
|
148 final MultiStatus multiStatus = BindingStatus.ok();
|
|
149 try {
|
|
150 // Get value
|
|
151 Object value = source_.getValue();
|
|
152
|
|
153 // Validate after get
|
|
154 IStatus status = updateValueStrategy_
|
|
155 .validateAfterGet(value);
|
|
156 if (!mergeStatus(multiStatus, status))
|
|
157 return;
|
|
158
|
|
159 // Convert value
|
|
160 final Object convertedValue = updateValueStrategy_
|
|
161 .convert(value);
|
|
162
|
|
163 // Validate after convert
|
|
164 status = updateValueStrategy_
|
|
165 .validateAfterConvert(convertedValue);
|
|
166 if (!mergeStatus(multiStatus, status))
|
|
167 return;
|
|
168 if (policy is UpdateValueStrategy.POLICY_CONVERT
|
|
169 && !explicit_)
|
|
170 return;
|
|
171
|
|
172 // Validate before set
|
|
173 status = updateValueStrategy_
|
|
174 .validateBeforeSet(convertedValue);
|
|
175 if (!mergeStatus(multiStatus, status))
|
|
176 return;
|
|
177 if (validateOnly_)
|
|
178 return;
|
|
179
|
|
180 // Set value
|
|
181 destinationRealmReached = true;
|
|
182 destination_.getRealm().exec(dgRunnable((
|
|
183 IObservableValue destination__,
|
|
184 UpdateValueStrategy updateValueStrategy__) {
|
|
185 if (destination__ is target) {
|
|
186 updatingTarget = true;
|
|
187 } else {
|
|
188 updatingModel = true;
|
|
189 }
|
|
190 try {
|
|
191 IStatus setterStatus = updateValueStrategy__
|
|
192 .doSet(destination__, convertedValue);
|
|
193
|
|
194 mergeStatus(multiStatus, setterStatus);
|
|
195 } finally {
|
|
196 if (destination__ is target) {
|
|
197 updatingTarget = false;
|
|
198 } else {
|
|
199 updatingModel = false;
|
|
200 }
|
|
201 setValidationStatus(multiStatus);
|
|
202 }
|
|
203 }, destination_, updateValueStrategy_ ));
|
|
204 } catch (Exception ex) {
|
|
205 // This check is necessary as in 3.2.2 Status
|
|
206 // doesn't accept a null message (bug 177264).
|
|
207 String message = (ex.getMessage() !is null) ? ex
|
|
208 .getMessage() : ""; //$NON-NLS-1$
|
|
209
|
|
210 mergeStatus(multiStatus, new Status(IStatus.ERROR,
|
|
211 Policy.JFACE_DATABINDING, IStatus.ERROR, message,
|
|
212 ex));
|
|
213 } finally {
|
|
214 if (!destinationRealmReached) {
|
|
215 setValidationStatus(multiStatus);
|
|
216 }
|
|
217
|
|
218 }
|
|
219 }, source, destination, updateValueStrategy, explicit, validateOnly));
|
|
220 }
|
|
221
|
|
222 public void validateModelToTarget() {
|
|
223 doUpdate(model, target, modelToTarget, true, true);
|
|
224 }
|
|
225
|
|
226 public void validateTargetToModel() {
|
|
227 doUpdate(target, model, targetToModel, true, true);
|
|
228 }
|
|
229
|
|
230 private void setValidationStatus( IStatus status) {
|
|
231 validationStatusObservable.getRealm().exec(dgRunnable((IStatus status_) {
|
|
232 validationStatusObservable.setValue(status_);
|
|
233 }, status));
|
|
234 }
|
|
235
|
|
236 public void dispose() {
|
|
237 if (targetChangeListener !is null) {
|
|
238 target.removeValueChangeListener(targetChangeListener);
|
|
239 targetChangeListener = null;
|
|
240 }
|
|
241 if (modelChangeListener !is null) {
|
|
242 model.removeValueChangeListener(modelChangeListener);
|
|
243 modelChangeListener = null;
|
|
244 }
|
|
245 target = null;
|
|
246 model = null;
|
|
247 super.dispose();
|
|
248 }
|
|
249
|
|
250 }
|