Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/databinding/ValueBinding.d @ 125:c43718956f21 default tip
Updated the snippets status.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 11 Aug 2011 19:55:14 +0200 |
parents | 9e0ab372d5d8 |
children |
rev | line source |
---|---|
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; | |
85 | 43 private IValueChangeListener targetChangeListener; |
44 class TargetChangeListener : IValueChangeListener { | |
78 | 45 public void handleValueChange(ValueChangeEvent event) { |
46 if (!updatingTarget && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) { | |
47 doUpdate(target, model, targetToModel, false, false); | |
48 } | |
49 } | |
50 }; | |
85 | 51 private IValueChangeListener modelChangeListener; |
52 class ModelChangeListener : IValueChangeListener { | |
78 | 53 public void handleValueChange(ValueChangeEvent event) { |
54 if (!updatingModel && !Util.equals(event.diff.getOldValue(), event.diff.getNewValue())) { | |
55 doUpdate(model, target, modelToTarget, false, false); | |
56 } | |
57 } | |
58 }; | |
59 | |
60 /** | |
61 * @param targetObservableValue | |
62 * @param modelObservableValue | |
63 * @param targetToModel | |
64 * @param modelToTarget | |
65 */ | |
66 public this(IObservableValue targetObservableValue, | |
67 IObservableValue modelObservableValue, | |
68 UpdateValueStrategy targetToModel, UpdateValueStrategy modelToTarget) { | |
85 | 69 targetChangeListener = new TargetChangeListener(); |
70 modelChangeListener = new ModelChangeListener(); | |
78 | 71 super(targetObservableValue, modelObservableValue); |
72 this.target = targetObservableValue; | |
73 this.model = modelObservableValue; | |
74 this.targetToModel = targetToModel; | |
75 this.modelToTarget = modelToTarget; | |
76 if ((targetToModel.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) !is 0) { | |
77 target.addValueChangeListener(targetChangeListener); | |
78 } else { | |
79 targetChangeListener = null; | |
80 } | |
81 if ((modelToTarget.getUpdatePolicy() & (UpdateValueStrategy.POLICY_CONVERT | UpdateValueStrategy.POLICY_UPDATE)) !is 0) { | |
82 model.addValueChangeListener(modelChangeListener); | |
83 } else { | |
84 modelChangeListener = null; | |
85 } | |
86 } | |
87 | |
88 protected void preInit() { | |
89 validationStatusObservable = new WritableValue(context | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
90 .getValidationRealm(), cast(Object) Status.OK_STATUS, Class.fromType!(IStatus)); |
78 | 91 } |
92 | |
93 protected void postInit() { | |
94 if (modelToTarget.getUpdatePolicy() is UpdateValueStrategy.POLICY_UPDATE) { | |
95 updateModelToTarget(); | |
96 } | |
97 if (targetToModel.getUpdatePolicy() !is UpdateValueStrategy.POLICY_NEVER) { | |
98 validateTargetToModel(); | |
99 } | |
100 } | |
101 | |
102 public IObservableValue getValidationStatus() { | |
103 return validationStatusObservable; | |
104 } | |
105 | |
106 public void updateTargetToModel() { | |
107 doUpdate(target, model, targetToModel, true, false); | |
108 } | |
109 | |
110 public void updateModelToTarget() { | |
111 doUpdate(model, target, modelToTarget, true, false); | |
112 } | |
113 | |
114 /** | |
115 * Incorporates the provided <code>newStats</code> into the | |
116 * <code>multieStatus</code>. | |
117 * | |
118 * @param multiStatus | |
119 * @param newStatus | |
120 * @return <code>true</code> if the update should proceed | |
121 */ | |
122 /* package */bool mergeStatus(MultiStatus multiStatus, IStatus newStatus) { | |
123 if (!newStatus.isOK()) { | |
124 multiStatus.add(newStatus); | |
125 return multiStatus.getSeverity() < IStatus.ERROR; | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 /* | |
131 * This method may be moved to UpdateValueStrategy in the future if clients | |
132 * need more control over how the source value is copied to the destination | |
133 * observable. | |
134 */ | |
135 private void doUpdate(IObservableValue source, | |
136 IObservableValue destination, | |
137 UpdateValueStrategy updateValueStrategy, | |
138 bool explicit, bool validateOnly) { | |
139 | |
140 final int policy = updateValueStrategy.getUpdatePolicy(); | |
141 if (policy is UpdateValueStrategy.POLICY_NEVER) | |
142 return; | |
143 if (policy is UpdateValueStrategy.POLICY_ON_REQUEST && !explicit) | |
144 return; | |
145 | |
146 source.getRealm().exec(dgRunnable(( | |
147 IObservableValue source_, | |
148 IObservableValue destination_, | |
149 UpdateValueStrategy updateValueStrategy_, | |
150 bool explicit_, bool validateOnly_) { | |
151 bool destinationRealmReached = false; | |
152 final MultiStatus multiStatus = BindingStatus.ok(); | |
153 try { | |
154 // Get value | |
155 Object value = source_.getValue(); | |
156 | |
157 // Validate after get | |
158 IStatus status = updateValueStrategy_ | |
159 .validateAfterGet(value); | |
160 if (!mergeStatus(multiStatus, status)) | |
161 return; | |
162 | |
163 // Convert value | |
164 final Object convertedValue = updateValueStrategy_ | |
165 .convert(value); | |
166 | |
167 // Validate after convert | |
168 status = updateValueStrategy_ | |
169 .validateAfterConvert(convertedValue); | |
170 if (!mergeStatus(multiStatus, status)) | |
171 return; | |
172 if (policy is UpdateValueStrategy.POLICY_CONVERT | |
173 && !explicit_) | |
174 return; | |
175 | |
176 // Validate before set | |
177 status = updateValueStrategy_ | |
178 .validateBeforeSet(convertedValue); | |
179 if (!mergeStatus(multiStatus, status)) | |
180 return; | |
181 if (validateOnly_) | |
182 return; | |
183 | |
184 // Set value | |
185 destinationRealmReached = true; | |
186 destination_.getRealm().exec(dgRunnable(( | |
187 IObservableValue destination__, | |
188 UpdateValueStrategy updateValueStrategy__) { | |
189 if (destination__ is target) { | |
190 updatingTarget = true; | |
191 } else { | |
192 updatingModel = true; | |
193 } | |
194 try { | |
195 IStatus setterStatus = updateValueStrategy__ | |
85 | 196 .doSet_package(destination__, convertedValue); |
78 | 197 |
198 mergeStatus(multiStatus, setterStatus); | |
199 } finally { | |
200 if (destination__ is target) { | |
201 updatingTarget = false; | |
202 } else { | |
203 updatingModel = false; | |
204 } | |
205 setValidationStatus(multiStatus); | |
206 } | |
207 }, destination_, updateValueStrategy_ )); | |
208 } catch (Exception ex) { | |
209 // This check is necessary as in 3.2.2 Status | |
210 // doesn't accept a null message (bug 177264). | |
85 | 211 String message = (ex.msg !is null) ? ex |
212 .msg : ""; //$NON-NLS-1$ | |
78 | 213 |
214 mergeStatus(multiStatus, new Status(IStatus.ERROR, | |
215 Policy.JFACE_DATABINDING, IStatus.ERROR, message, | |
216 ex)); | |
217 } finally { | |
218 if (!destinationRealmReached) { | |
219 setValidationStatus(multiStatus); | |
220 } | |
221 | |
222 } | |
223 }, source, destination, updateValueStrategy, explicit, validateOnly)); | |
224 } | |
225 | |
226 public void validateModelToTarget() { | |
227 doUpdate(model, target, modelToTarget, true, true); | |
228 } | |
229 | |
230 public void validateTargetToModel() { | |
231 doUpdate(target, model, targetToModel, true, true); | |
232 } | |
233 | |
234 private void setValidationStatus( IStatus status) { | |
235 validationStatusObservable.getRealm().exec(dgRunnable((IStatus status_) { | |
85 | 236 validationStatusObservable.setValue(cast(Object)status_); |
78 | 237 }, status)); |
238 } | |
239 | |
240 public void dispose() { | |
241 if (targetChangeListener !is null) { | |
242 target.removeValueChangeListener(targetChangeListener); | |
243 targetChangeListener = null; | |
244 } | |
245 if (modelChangeListener !is null) { | |
246 model.removeValueChangeListener(modelChangeListener); | |
247 modelChangeListener = null; | |
248 } | |
249 target = null; | |
250 model = null; | |
251 super.dispose(); | |
252 } | |
253 | |
254 } |