Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 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 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.core.databinding.editing; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
import java.util.List; |
16 |
|
17 |
import org.eclipse.core.databinding.UpdateListStrategy; |
18 |
import org.eclipse.core.databinding.UpdateSetStrategy; |
19 |
import org.eclipse.core.databinding.UpdateValueStrategy; |
20 |
import org.eclipse.core.databinding.conversion.IConverter; |
21 |
import org.eclipse.core.databinding.validation.IValidator; |
22 |
import org.eclipse.core.internal.databinding.validation.CompositeValidator; |
23 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.core.runtime.MultiStatus; |
25 |
|
26 |
/** |
27 |
* @since 1.3 |
28 |
*/ |
29 |
public class Editing { |
30 |
|
31 |
private final List targetValidators = new ArrayList(); |
32 |
|
33 |
private IConverter targetConverter; |
34 |
|
35 |
private final List modelValidators = new ArrayList(); |
36 |
|
37 |
private final List beforeSetModelValidators = new ArrayList(); |
38 |
|
39 |
private IConverter modelConverter; |
40 |
|
41 |
/** |
42 |
* Adds a custom target validator. |
43 |
* |
44 |
* @param validator |
45 |
* The custom target validator to add. |
46 |
* @return This editing instance for method chaining. |
47 |
*/ |
48 |
public final Editing addTargetValidator(IValidator validator) { |
49 |
targetValidators.add(validator); |
50 |
return this; |
51 |
} |
52 |
|
53 |
/** |
54 |
* Adds a custom model validator. |
55 |
* |
56 |
* @param validator |
57 |
* The custom model validator to add. |
58 |
* @return This editing instance for method chaining. |
59 |
*/ |
60 |
public final Editing addModelValidator(IValidator validator) { |
61 |
modelValidators.add(validator); |
62 |
return this; |
63 |
} |
64 |
|
65 |
/** |
66 |
* Adds a custom before-set model validator. |
67 |
* |
68 |
* @param validator |
69 |
* The custom before-set model validator to add. |
70 |
* @return This editing instance for method chaining. |
71 |
*/ |
72 |
public final Editing addBeforeSetModelValidator(IValidator validator) { |
73 |
beforeSetModelValidators.add(validator); |
74 |
return this; |
75 |
} |
76 |
|
77 |
protected final void setTargetConverter(IConverter converter) { |
78 |
this.targetConverter = converter; |
79 |
} |
80 |
|
81 |
protected final void setModelConverter(IConverter converter) { |
82 |
this.modelConverter = converter; |
83 |
} |
84 |
|
85 |
/** |
86 |
* Creates a new target-to-model {@link UpdateValueStrategy} with a default |
87 |
* update policy configured by the current state of this editing object. |
88 |
* |
89 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
90 |
* the current state of this editing object. |
91 |
* |
92 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
93 |
*/ |
94 |
public UpdateValueStrategy createTargetValueStrategy() { |
95 |
return applyToTargetValueStrategy(new UpdateValueStrategy()); |
96 |
} |
97 |
|
98 |
/** |
99 |
* Creates a new target-to-model {@link UpdateValueStrategy} with the given |
100 |
* update policy configured by the current state of this editing object. |
101 |
* |
102 |
* @param updatePolicy |
103 |
* The update policy to use. |
104 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
105 |
* the current state of this editing object. |
106 |
* |
107 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
108 |
*/ |
109 |
public UpdateValueStrategy createTargetValueStrategy(int updatePolicy) { |
110 |
return applyToTargetValueStrategy(new UpdateValueStrategy(updatePolicy)); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Creates a new model-to-target {@link UpdateValueStrategy} with a default |
115 |
* update policy configured by the current state of this editing object. |
116 |
* |
117 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
118 |
* the current state of this editing object. |
119 |
* |
120 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
121 |
*/ |
122 |
public UpdateValueStrategy createModelValueStrategy() { |
123 |
return applyToModelValueStrategy(new UpdateValueStrategy()); |
124 |
} |
125 |
|
126 |
/** |
127 |
* Creates a new model-to-target {@link UpdateValueStrategy} with the given |
128 |
* update policy configured by the current state of this editing object. |
129 |
* |
130 |
* @param updatePolicy |
131 |
* The update policy to use. |
132 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
133 |
* the current state of this editing object. |
134 |
* |
135 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
136 |
*/ |
137 |
public UpdateValueStrategy createModelValueStrategy(int updatePolicy) { |
138 |
return applyToModelValueStrategy(new UpdateValueStrategy(updatePolicy)); |
139 |
} |
140 |
|
141 |
/** |
142 |
* Creates a new target-to-model {@link UpdateListStrategy} with a default |
143 |
* update policy configured by the current state of this editing object. |
144 |
* |
145 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
146 |
* the current state of this editing object. |
147 |
* |
148 |
* @see UpdateListStrategy#UpdateListStrategy() |
149 |
*/ |
150 |
public UpdateListStrategy createTargetListStrategy() { |
151 |
return applyToTargetListStrategy(new UpdateListStrategy()); |
152 |
} |
153 |
|
154 |
/** |
155 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
156 |
* update policy configured by the current state of this editing object. |
157 |
* |
158 |
* @param updatePolicy |
159 |
* The update policy to use. |
160 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
161 |
* the current state of this editing object. |
162 |
* |
163 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
164 |
*/ |
165 |
public UpdateListStrategy createTargetListStrategy(int updatePolicy) { |
166 |
return applyToTargetListStrategy(new UpdateListStrategy(updatePolicy)); |
167 |
} |
168 |
|
169 |
/** |
170 |
* Creates a new model-to-target {@link UpdateListStrategy} with a default |
171 |
* update policy configured by the current state of this editing object. |
172 |
* |
173 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
174 |
* the current state of this editing object. |
175 |
* |
176 |
* @see UpdateListStrategy#UpdateListStrategy() |
177 |
*/ |
178 |
public UpdateListStrategy createModelListStrategy() { |
179 |
return applyToModelListStrategy(new UpdateListStrategy()); |
180 |
} |
181 |
|
182 |
/** |
183 |
* Creates a new model-to-target {@link UpdateListStrategy} with the given |
184 |
* update policy configured by the current state of this editing object. |
185 |
* |
186 |
* @param updatePolicy |
187 |
* The update policy to use. |
188 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
189 |
* the current state of this editing object. |
190 |
* |
191 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
192 |
*/ |
193 |
public UpdateListStrategy createModelListStrategy(int updatePolicy) { |
194 |
return applyToModelListStrategy(new UpdateListStrategy(updatePolicy)); |
195 |
} |
196 |
|
197 |
/** |
198 |
* Creates a new target-to-model {@link UpdateSetStrategy} with a default |
199 |
* update policy configured by the current state of this editing object. |
200 |
* |
201 |
* @return A new target-to-model {@link UpdateSetStrategy} configured by the |
202 |
* current state of this editing object. |
203 |
* |
204 |
* @see UpdateListStrategy#UpdateListStrategy() |
205 |
*/ |
206 |
public UpdateSetStrategy createTargetSetStrategy() { |
207 |
return applyToTargetSetStrategy(new UpdateSetStrategy()); |
208 |
} |
209 |
|
210 |
/** |
211 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
212 |
* update policy configured by the current state of this editing object. |
213 |
* |
214 |
* @param updatePolicy |
215 |
* The update policy to use. |
216 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
217 |
* the current state of this editing object. |
218 |
* |
219 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
220 |
*/ |
221 |
public UpdateSetStrategy createTargetSetStrategy(int updatePolicy) { |
222 |
return applyToTargetSetStrategy(new UpdateSetStrategy(updatePolicy)); |
223 |
} |
224 |
|
225 |
/** |
226 |
* Creates a new model-to-target {@link UpdateSetStrategy} with a default |
227 |
* update policy configured by the current state of this editing object. |
228 |
* |
229 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
230 |
* current state of this editing object. |
231 |
* |
232 |
* @see UpdateSetStrategy#UpdateSetStrategy() |
233 |
*/ |
234 |
public UpdateSetStrategy createModelSetStrategy() { |
235 |
return applyToModelSetStrategy(new UpdateSetStrategy()); |
236 |
} |
237 |
|
238 |
/** |
239 |
* Creates a new model-to-target {@link UpdateSetStrategy} with the given |
240 |
* update policy configured by the current state of this editing object. |
241 |
* |
242 |
* @param updatePolicy |
243 |
* The update policy to use. |
244 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
245 |
* current state of this editing object. |
246 |
* |
247 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
248 |
*/ |
249 |
public UpdateSetStrategy createModelSetStrategy(int updatePolicy) { |
250 |
return applyToModelSetStrategy(new UpdateSetStrategy(updatePolicy)); |
251 |
} |
252 |
|
253 |
/** |
254 |
* Configures an existing target-to-model {@link UpdateValueStrategy} with |
255 |
* the current state of this editing object. |
256 |
* |
257 |
* @param updateStrategy |
258 |
* The {@link UpdateValueStrategy} to configure. |
259 |
* @return The passed-in, configured target-to-model |
260 |
* {@link UpdateValueStrategy}. |
261 |
*/ |
262 |
public UpdateValueStrategy applyToTargetValueStrategy( |
263 |
UpdateValueStrategy updateStrategy) { |
264 |
updateStrategy.setAfterGetValidator(createTargetValidator()); |
265 |
updateStrategy.setConverter(targetConverter); |
266 |
updateStrategy.setAfterConvertValidator(createModelValidator()); |
267 |
updateStrategy.setBeforeSetValidator(createBeforeSetModelValidator()); |
268 |
return updateStrategy; |
269 |
} |
270 |
|
271 |
/** |
272 |
* Configures an existing model-to-target {@link UpdateValueStrategy} with |
273 |
* the current state of this editing object. |
274 |
* |
275 |
* @param updateStrategy |
276 |
* The {@link UpdateValueStrategy} to configure. |
277 |
* @return The passed-in, configured model-to-target |
278 |
* {@link UpdateValueStrategy}. |
279 |
*/ |
280 |
public UpdateValueStrategy applyToModelValueStrategy( |
281 |
UpdateValueStrategy updateStrategy) { |
282 |
updateStrategy.setConverter(modelConverter); |
283 |
return updateStrategy; |
284 |
} |
285 |
|
286 |
/** |
287 |
* Configures an existing target-to-model {@link UpdateListStrategy} with |
288 |
* the current state of this editing object. |
289 |
* |
290 |
* @param updateStrategy |
291 |
* The {@link UpdateListStrategy} to configure. |
292 |
* @return The passed-in, configured target-to-model |
293 |
* {@link UpdateListStrategy}. |
294 |
*/ |
295 |
public UpdateListStrategy applyToTargetListStrategy( |
296 |
UpdateListStrategy updateStrategy) { |
297 |
updateStrategy.setConverter(targetConverter); |
298 |
return updateStrategy; |
299 |
} |
300 |
|
301 |
/** |
302 |
* Configures an existing model-to-target {@link UpdateListStrategy} with |
303 |
* the current state of this editing object. |
304 |
* |
305 |
* @param updateStrategy |
306 |
* The {@link UpdateListStrategy} to configure. |
307 |
* @return The passed-in, configured model-to-target |
308 |
* {@link UpdateListStrategy}. |
309 |
*/ |
310 |
public UpdateListStrategy applyToModelListStrategy( |
311 |
UpdateListStrategy updateStrategy) { |
312 |
updateStrategy.setConverter(modelConverter); |
313 |
return updateStrategy; |
314 |
} |
315 |
|
316 |
/** |
317 |
* Configures an existing target-to-model {@link UpdateListStrategy} with |
318 |
* the current state of this editing object. |
319 |
* |
320 |
* @param updateStrategy |
321 |
* The {@link UpdateSetStrategy} to configure. |
322 |
* @return The passed-in, configured target-to-model |
323 |
* {@link UpdateSetStrategy}. |
324 |
*/ |
325 |
public UpdateSetStrategy applyToTargetSetStrategy( |
326 |
UpdateSetStrategy updateStrategy) { |
327 |
updateStrategy.setConverter(targetConverter); |
328 |
return updateStrategy; |
329 |
} |
330 |
|
331 |
/** |
332 |
* Configures an existing model-to-target {@link UpdateSetStrategy} with the |
333 |
* current state of this editing object. |
334 |
* |
335 |
* @param updateStrategy |
336 |
* The {@link UpdateSetStrategy} to configure. |
337 |
* @return The passed-in, configured model-to-target |
338 |
* {@link UpdateSetStrategy}. |
339 |
*/ |
340 |
public UpdateSetStrategy applyToModelSetStrategy( |
341 |
UpdateSetStrategy updateStrategy) { |
342 |
updateStrategy.setConverter(modelConverter); |
343 |
return updateStrategy; |
344 |
} |
345 |
|
346 |
/** |
347 |
* Converts a target value to a model value by performing the following |
348 |
* steps: |
349 |
* <ul> |
350 |
* <li> |
351 |
* Applying all the currently accumulated |
352 |
* {@link #addTargetValidator(IValidator) target validators} to the given |
353 |
* target value.</li> |
354 |
* <li> |
355 |
* {@link #setTargetConverter(IConverter) Converting} the target value to |
356 |
* the model value.</li> |
357 |
* <li> |
358 |
* Applying all the currently accumulated |
359 |
* {@link #addModelValidator(IValidator) model validators} to the converted |
360 |
* model value.</li> |
361 |
* <li> |
362 |
* Applying all the currently accumulated |
363 |
* {@link #addBeforeSetModelValidator(IValidator) before-set model |
364 |
* validators} to the converted model value.</li> |
365 |
* </ul> |
366 |
* |
367 |
* <p> |
368 |
* The conversion process will be aborted by returning <code>null</code> |
369 |
* whenever any of the applied validators produces a {@link IStatus |
370 |
* validation status} having {@link IStatus#getSeverity() severity} |
371 |
* <code>IStatus.ERROR</code> or <code>IStatus.CANCEL</code>. During the |
372 |
* conversion process, any validation status whose severity is different |
373 |
* from <code>IStatus.OK</code> will be {@link MultiStatus#add(IStatus) |
374 |
* added} to the given aggregate <code>validationStatus</code>. |
375 |
* </p> |
376 |
* |
377 |
* @param targetValue |
378 |
* The target value to be converted to a model value. |
379 |
* @param validationStatus |
380 |
* Aggregate validation status to which to add the validations |
381 |
* produced during the conversion process. |
382 |
* @return The converted model value or <code>null</code> in case the |
383 |
* conversion has been aborted due to a validation error. |
384 |
*/ |
385 |
public final Object convertToModel(Object targetValue, |
386 |
MultiStatus validationStatus) { |
387 |
// TODO: Cache the target/model validators. |
388 |
IValidator targetValidator = createTargetValidator(); |
389 |
if (!applyValidator(targetValidator, targetValue, validationStatus)) { |
390 |
return null; |
391 |
} |
392 |
|
393 |
Object modelValue = (targetConverter != null) ? targetConverter |
394 |
.convert(targetValue) : targetValue; |
395 |
|
396 |
IValidator modelValidator = createModelValidator(); |
397 |
if (!applyValidator(modelValidator, modelValue, validationStatus)) { |
398 |
return null; |
399 |
} |
400 |
|
401 |
IValidator beforeSetModelValidator = createBeforeSetModelValidator(); |
402 |
if (!applyValidator(beforeSetModelValidator, modelValue, |
403 |
validationStatus)) { |
404 |
return null; |
405 |
} |
406 |
|
407 |
return modelValue; |
408 |
} |
409 |
|
410 |
/** |
411 |
* {@link #setModelConverter(IConverter) Converts} a model value to a target |
412 |
* value. |
413 |
* |
414 |
* @param modelValue |
415 |
* The target value to be converted to a model value. |
416 |
* @return The converted target value. |
417 |
*/ |
418 |
public final Object convertToTarget(Object modelValue) { |
419 |
return (modelConverter != null) ? modelConverter.convert(modelValue) |
420 |
: modelValue; |
421 |
} |
422 |
|
423 |
private IValidator createTargetValidator() { |
424 |
if (!targetValidators.isEmpty()) { |
425 |
CompositeValidator targetValidator = new CompositeValidator( |
426 |
(IValidator[]) targetValidators |
427 |
.toArray(new IValidator[targetValidators.size()])); |
428 |
return targetValidator; |
429 |
} |
430 |
return null; |
431 |
} |
432 |
|
433 |
private IValidator createModelValidator() { |
434 |
if (!modelValidators.isEmpty()) { |
435 |
return new CompositeValidator((IValidator[]) modelValidators |
436 |
.toArray(new IValidator[modelValidators.size()])); |
437 |
} |
438 |
return null; |
439 |
} |
440 |
|
441 |
private IValidator createBeforeSetModelValidator() { |
442 |
if (!beforeSetModelValidators.isEmpty()) { |
443 |
return new CompositeValidator( |
444 |
(IValidator[]) beforeSetModelValidators |
445 |
.toArray(new IValidator[beforeSetModelValidators |
446 |
.size()])); |
447 |
} |
448 |
return null; |
449 |
} |
450 |
|
451 |
private static boolean applyValidator(IValidator validator, Object value, |
452 |
MultiStatus aggregateStatus) { |
453 |
if (validator != null) { |
454 |
IStatus validationStatus = validator.validate(value); |
455 |
if (!validationStatus.isOK()) { |
456 |
aggregateStatus.add(validationStatus); |
457 |
} |
458 |
return isValid(validationStatus); |
459 |
} |
460 |
return true; |
461 |
} |
462 |
|
463 |
private static boolean isValid(IStatus status) { |
464 |
return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); |
465 |
} |
466 |
} |