Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Ovidio Mallo 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 |
* Ovidio Mallo - initial API and implementation (bug 183055) |
10 |
******************************************************************************/ |
11 |
|
12 |
package org.eclipse.core.databinding.editing; |
13 |
|
14 |
import org.eclipse.core.databinding.Binding; |
15 |
import org.eclipse.core.databinding.DataBindingContext; |
16 |
import org.eclipse.core.databinding.UpdateListStrategy; |
17 |
import org.eclipse.core.databinding.UpdateSetStrategy; |
18 |
import org.eclipse.core.databinding.UpdateValueStrategy; |
19 |
import org.eclipse.core.databinding.conversion.IConverter; |
20 |
import org.eclipse.core.databinding.observable.list.IObservableList; |
21 |
import org.eclipse.core.databinding.observable.set.IObservableSet; |
22 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
23 |
import org.eclipse.core.databinding.validation.IValidator; |
24 |
import org.eclipse.core.databinding.validation.constraint.Constraints; |
25 |
import org.eclipse.core.runtime.IStatus; |
26 |
import org.eclipse.core.runtime.MultiStatus; |
27 |
|
28 |
/** |
29 |
* @param <T> |
30 |
* @param <M> |
31 |
* |
32 |
* @since 1.3 |
33 |
*/ |
34 |
public class Editing<T, M> { |
35 |
|
36 |
private Constraints<T> targetConstraints; |
37 |
|
38 |
private Constraints<M> modelConstraints; |
39 |
|
40 |
private Constraints<M> beforeSetModelConstraints; |
41 |
|
42 |
private IConverter targetConverter; |
43 |
|
44 |
private IConverter modelConverter; |
45 |
|
46 |
/** |
47 |
* Default constructor which creates an unconfigured editing instance. |
48 |
*/ |
49 |
protected Editing() { |
50 |
// empty editing instance |
51 |
} |
52 |
|
53 |
/** |
54 |
* Creates an unconfigured editing instance. |
55 |
* |
56 |
* @return A new, unconfigured editing instance. |
57 |
* |
58 |
* @param <MT> |
59 |
* @param <MM> |
60 |
*/ |
61 |
public static <MT, MM> Editing<MT, MM> create() { |
62 |
return new Editing<MT, MM>(); |
63 |
} |
64 |
|
65 |
/** |
66 |
* Creates an editing instance with the given target-to-model and |
67 |
* model-to-target converters. |
68 |
* |
69 |
* @param t2mConverter |
70 |
* The target-to-model converter to set. May be <code>null</code> |
71 |
* . |
72 |
* @param m2tConverter |
73 |
* The model-to-target converter to set. May be <code>null</code> |
74 |
* . |
75 |
* @return A new editing instance with the given target-to-model and |
76 |
* model-to-target converters. |
77 |
* |
78 |
* @param <MM> |
79 |
* @param <MT> |
80 |
*/ |
81 |
public static <MT, MM> Editing<MT, MM> withConverters( |
82 |
IConverter t2mConverter, IConverter m2tConverter) { |
83 |
Editing<MT, MM> editing = new Editing<MT, MM>(); |
84 |
editing.setTargetConverter(t2mConverter); |
85 |
editing.setModelConverter(m2tConverter); |
86 |
return editing; |
87 |
} |
88 |
|
89 |
/** |
90 |
* Returns the target constraints to apply. |
91 |
* |
92 |
* @return The target constraints to apply. |
93 |
* |
94 |
* @see #createTargetConstraints() |
95 |
*/ |
96 |
public Constraints<T> targetConstraints() { |
97 |
if (targetConstraints == null) { |
98 |
targetConstraints = createTargetConstraints(); |
99 |
} |
100 |
return targetConstraints; |
101 |
} |
102 |
|
103 |
/** |
104 |
* Returns the model constraints to apply. |
105 |
* |
106 |
* @return The model constraints to apply. |
107 |
* |
108 |
* @see #createModelConstraints() |
109 |
*/ |
110 |
public Constraints<M> modelConstraints() { |
111 |
if (modelConstraints == null) { |
112 |
modelConstraints = createModelConstraints(); |
113 |
} |
114 |
return modelConstraints; |
115 |
} |
116 |
|
117 |
/** |
118 |
* Returns the before-set model constraints to apply. |
119 |
* |
120 |
* @return The before-set model constraints to apply. |
121 |
* |
122 |
* @see #createBeforeSetModelConstraints() |
123 |
*/ |
124 |
public Constraints<M> beforeSetModelConstraints() { |
125 |
if (beforeSetModelConstraints == null) { |
126 |
beforeSetModelConstraints = createBeforeSetModelConstraints(); |
127 |
} |
128 |
return beforeSetModelConstraints; |
129 |
} |
130 |
|
131 |
/** |
132 |
* Creates the target constraints to apply. |
133 |
* |
134 |
* <p> |
135 |
* The constraints object created by this method will be exposed as the |
136 |
* {@link #targetConstraints()} of this editing object. By default, this |
137 |
* method returns a plain {@link Constraints} object. |
138 |
* </p> |
139 |
* |
140 |
* <p> |
141 |
* This method will be called lazily the first time the target constraints |
142 |
* are accessed. Subclasses may overwrite this method in order to create a |
143 |
* custom constraints object which will then be typically exposed in the |
144 |
* subclass as API in order to allow for a typesafe access. |
145 |
* </p> |
146 |
* |
147 |
* @return The target constraints to apply. |
148 |
* |
149 |
* @see #targetConstraints() |
150 |
*/ |
151 |
protected Constraints<T> createTargetConstraints() { |
152 |
return new Constraints<T>(); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Creates the model constraints to apply. |
157 |
* |
158 |
* <p> |
159 |
* The constraints object created by this method will be exposed as the |
160 |
* {@link #modelConstraints()} of this editing object. By default, this |
161 |
* method returns a plain {@link Constraints} object. |
162 |
* </p> |
163 |
* |
164 |
* <p> |
165 |
* This method will be called lazily the first time the model constraints |
166 |
* are accessed. Subclasses may overwrite this method in order to create a |
167 |
* custom constraints object which will then be typically exposed in the |
168 |
* subclass as API in order to allow for a typesafe access. |
169 |
* </p> |
170 |
* |
171 |
* @return The model constraints to apply. |
172 |
* |
173 |
* @see #modelConstraints() |
174 |
*/ |
175 |
protected Constraints<M> createModelConstraints() { |
176 |
return new Constraints<M>(); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Creates the before-set model constraints to apply. |
181 |
* |
182 |
* <p> |
183 |
* The constraints object created by this method will be exposed as the |
184 |
* {@link #beforeSetModelConstraints()} of this editing object. By default, |
185 |
* this method returns a plain {@link Constraints} object. |
186 |
* </p> |
187 |
* |
188 |
* <p> |
189 |
* This method will be called lazily the first time the before-set model |
190 |
* constraints are accessed. Subclasses may overwrite this method in order |
191 |
* to create a custom constraints object which will then be typically |
192 |
* exposed in the subclass as API in order to allow for a typesafe access. |
193 |
* </p> |
194 |
* |
195 |
* @return The before-set model constraints to apply. |
196 |
* |
197 |
* @see #beforeSetModelConstraints() |
198 |
*/ |
199 |
protected Constraints<M> createBeforeSetModelConstraints() { |
200 |
return new Constraints<M>(); |
201 |
} |
202 |
|
203 |
/** |
204 |
* Sets the target-to-model converter for this editing instance. |
205 |
* |
206 |
* @param converter |
207 |
* The target-to-model converter to set. |
208 |
*/ |
209 |
protected final void setTargetConverter(IConverter converter) { |
210 |
this.targetConverter = converter; |
211 |
} |
212 |
|
213 |
/** |
214 |
* Sets the model-to-target converter for this editing instance. |
215 |
* |
216 |
* @param converter |
217 |
* The model-to-target converter to set. |
218 |
*/ |
219 |
protected final void setModelConverter(IConverter converter) { |
220 |
this.modelConverter = converter; |
221 |
} |
222 |
|
223 |
/** |
224 |
* Creates a new target-to-model {@link UpdateValueStrategy} with a default |
225 |
* update policy configured by the current state of this editing object. |
226 |
* |
227 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
228 |
* the current state of this editing object. |
229 |
* |
230 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
231 |
* @see #adaptT2MValueStrategy(UpdateValueStrategy) |
232 |
*/ |
233 |
public final UpdateValueStrategy createT2MValueStrategy() { |
234 |
return adaptT2MValueStrategy(new UpdateValueStrategy()); |
235 |
} |
236 |
|
237 |
/** |
238 |
* Creates a new target-to-model {@link UpdateValueStrategy} with the given |
239 |
* update policy configured by the current state of this editing object. |
240 |
* |
241 |
* @param updatePolicy |
242 |
* The update policy to use. |
243 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
244 |
* the current state of this editing object. |
245 |
* |
246 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
247 |
* @see #adaptT2MValueStrategy(UpdateValueStrategy) |
248 |
*/ |
249 |
public final UpdateValueStrategy createT2MValueStrategy(int updatePolicy) { |
250 |
return adaptT2MValueStrategy(new UpdateValueStrategy(updatePolicy)); |
251 |
} |
252 |
|
253 |
/** |
254 |
* Creates a new model-to-target {@link UpdateValueStrategy} with a default |
255 |
* update policy configured by the current state of this editing object. |
256 |
* |
257 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
258 |
* the current state of this editing object. |
259 |
* |
260 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
261 |
* @see #adaptM2TValueStrategy(UpdateValueStrategy) |
262 |
*/ |
263 |
public final UpdateValueStrategy createM2TValueStrategy() { |
264 |
return adaptM2TValueStrategy(new UpdateValueStrategy()); |
265 |
} |
266 |
|
267 |
/** |
268 |
* Creates a new model-to-target {@link UpdateValueStrategy} with the given |
269 |
* update policy configured by the current state of this editing object. |
270 |
* |
271 |
* @param updatePolicy |
272 |
* The update policy to use. |
273 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
274 |
* the current state of this editing object. |
275 |
* |
276 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
277 |
* @see #adaptM2TValueStrategy(UpdateValueStrategy) |
278 |
*/ |
279 |
public final UpdateValueStrategy createM2TValueStrategy(int updatePolicy) { |
280 |
return adaptM2TValueStrategy(new UpdateValueStrategy(updatePolicy)); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Creates a new target-to-model {@link UpdateListStrategy} with a default |
285 |
* update policy configured by the current state of this editing object. |
286 |
* |
287 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
288 |
* the current state of this editing object. |
289 |
* |
290 |
* @see UpdateListStrategy#UpdateListStrategy() |
291 |
* @see #adaptT2MListStrategy(UpdateListStrategy) |
292 |
*/ |
293 |
public final UpdateListStrategy createT2MListStrategy() { |
294 |
return adaptT2MListStrategy(new UpdateListStrategy()); |
295 |
} |
296 |
|
297 |
/** |
298 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
299 |
* update policy configured by the current state of this editing object. |
300 |
* |
301 |
* @param updatePolicy |
302 |
* The update policy to use. |
303 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
304 |
* the current state of this editing object. |
305 |
* |
306 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
307 |
* @see #adaptT2MListStrategy(UpdateListStrategy) |
308 |
*/ |
309 |
public final UpdateListStrategy createT2MListStrategy(int updatePolicy) { |
310 |
return adaptT2MListStrategy(new UpdateListStrategy(updatePolicy)); |
311 |
} |
312 |
|
313 |
/** |
314 |
* Creates a new model-to-target {@link UpdateListStrategy} with a default |
315 |
* update policy configured by the current state of this editing object. |
316 |
* |
317 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
318 |
* the current state of this editing object. |
319 |
* |
320 |
* @see UpdateListStrategy#UpdateListStrategy() |
321 |
* @see #adaptM2TListStrategy(UpdateListStrategy) |
322 |
*/ |
323 |
public final UpdateListStrategy createM2TListStrategy() { |
324 |
return adaptM2TListStrategy(new UpdateListStrategy()); |
325 |
} |
326 |
|
327 |
/** |
328 |
* Creates a new model-to-target {@link UpdateListStrategy} with the given |
329 |
* update policy configured by the current state of this editing object. |
330 |
* |
331 |
* @param updatePolicy |
332 |
* The update policy to use. |
333 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
334 |
* the current state of this editing object. |
335 |
* |
336 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
337 |
* @see #adaptM2TListStrategy(UpdateListStrategy) |
338 |
*/ |
339 |
public final UpdateListStrategy createM2TListStrategy(int updatePolicy) { |
340 |
return adaptM2TListStrategy(new UpdateListStrategy(updatePolicy)); |
341 |
} |
342 |
|
343 |
/** |
344 |
* Creates a new target-to-model {@link UpdateSetStrategy} with a default |
345 |
* update policy configured by the current state of this editing object. |
346 |
* |
347 |
* @return A new target-to-model {@link UpdateSetStrategy} configured by the |
348 |
* current state of this editing object. |
349 |
* |
350 |
* @see UpdateListStrategy#UpdateListStrategy() |
351 |
* @see #adaptT2MSetStrategy(UpdateSetStrategy) |
352 |
*/ |
353 |
public final UpdateSetStrategy createT2MSetStrategy() { |
354 |
return adaptT2MSetStrategy(new UpdateSetStrategy()); |
355 |
} |
356 |
|
357 |
/** |
358 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
359 |
* update policy configured by the current state of this editing object. |
360 |
* |
361 |
* @param updatePolicy |
362 |
* The update policy to use. |
363 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
364 |
* the current state of this editing object. |
365 |
* |
366 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
367 |
* @see #adaptT2MSetStrategy(UpdateSetStrategy) |
368 |
*/ |
369 |
public final UpdateSetStrategy createT2MSetStrategy(int updatePolicy) { |
370 |
return adaptT2MSetStrategy(new UpdateSetStrategy(updatePolicy)); |
371 |
} |
372 |
|
373 |
/** |
374 |
* Creates a new model-to-target {@link UpdateSetStrategy} with a default |
375 |
* update policy configured by the current state of this editing object. |
376 |
* |
377 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
378 |
* current state of this editing object. |
379 |
* |
380 |
* @see UpdateSetStrategy#UpdateSetStrategy() |
381 |
* @see #adaptM2TSetStrategy(UpdateSetStrategy) |
382 |
*/ |
383 |
public final UpdateSetStrategy createM2TSetStrategy() { |
384 |
return adaptM2TSetStrategy(new UpdateSetStrategy()); |
385 |
} |
386 |
|
387 |
/** |
388 |
* Creates a new model-to-target {@link UpdateSetStrategy} with the given |
389 |
* update policy configured by the current state of this editing object. |
390 |
* |
391 |
* @param updatePolicy |
392 |
* The update policy to use. |
393 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
394 |
* current state of this editing object. |
395 |
* |
396 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
397 |
* @see #adaptM2TSetStrategy(UpdateSetStrategy) |
398 |
*/ |
399 |
public final UpdateSetStrategy createM2TSetStrategy(int updatePolicy) { |
400 |
return adaptM2TSetStrategy(new UpdateSetStrategy(updatePolicy)); |
401 |
} |
402 |
|
403 |
/** |
404 |
* Configures an existing target-to-model {@link UpdateValueStrategy} with |
405 |
* the current state of this editing object. |
406 |
* |
407 |
* <p> |
408 |
* The configuration is done as follows: |
409 |
* <ul> |
410 |
* <li> |
411 |
* The {@link Constraints#createValidator() validator} of the |
412 |
* {@link #targetConstraints() target constraints} is set as the |
413 |
* {@link UpdateValueStrategy#setAfterGetValidator(IValidator) after-get |
414 |
* validator} of the update strategy.</li> |
415 |
* <li>The {@link #setTargetConverter(IConverter) target converter} is set |
416 |
* as the {@link UpdateValueStrategy#setConverter(IConverter) converter} of |
417 |
* the update strategy.</li> |
418 |
* <li> |
419 |
* The {@link Constraints#createValidator() validator} of the |
420 |
* {@link #modelConstraints() model constraints} is set as the |
421 |
* {@link UpdateValueStrategy#setAfterConvertValidator(IValidator) |
422 |
* after-convert validator} of the update strategy.</li> |
423 |
* <li>The {@link Constraints#createValidator() validator} of the |
424 |
* {@link #beforeSetModelConstraints() before-set model constraints} is set |
425 |
* as the {@link UpdateValueStrategy#setBeforeSetValidator(IValidator) |
426 |
* before-set validator} of the update strategy.</li> |
427 |
* </ul> |
428 |
* </p> |
429 |
* |
430 |
* @param updateStrategy |
431 |
* The {@link UpdateValueStrategy} to configure. |
432 |
* @return The passed-in, configured target-to-model |
433 |
* {@link UpdateValueStrategy}. |
434 |
* |
435 |
* @see UpdateValueStrategy#setAfterGetValidator(IValidator) |
436 |
* @see UpdateValueStrategy#setConverter(IConverter) |
437 |
* @see UpdateValueStrategy#setAfterConvertValidator(IValidator) |
438 |
* @see UpdateValueStrategy#setBeforeSetValidator(IValidator) |
439 |
*/ |
440 |
public final UpdateValueStrategy adaptT2MValueStrategy( |
441 |
UpdateValueStrategy updateStrategy) { |
442 |
updateStrategy.setAfterGetValidator(createValidator(targetConstraints)); |
443 |
updateStrategy.setConverter(targetConverter); |
444 |
updateStrategy |
445 |
.setAfterConvertValidator(createValidator(modelConstraints)); |
446 |
updateStrategy |
447 |
.setBeforeSetValidator(createValidator(beforeSetModelConstraints)); |
448 |
return updateStrategy; |
449 |
} |
450 |
|
451 |
/** |
452 |
* Configures an existing model-to-target {@link UpdateValueStrategy} with |
453 |
* the current state of this editing object by setting the |
454 |
* {@link #setModelConverter(IConverter) model converter} as the |
455 |
* {@link UpdateValueStrategy#setConverter(IConverter) converter} of the |
456 |
* update strategy. |
457 |
* |
458 |
* @param updateStrategy |
459 |
* The {@link UpdateValueStrategy} to configure. |
460 |
* @return The passed-in, configured model-to-target |
461 |
* {@link UpdateValueStrategy}. |
462 |
* |
463 |
* @see UpdateValueStrategy#setConverter(IConverter) |
464 |
*/ |
465 |
public final UpdateValueStrategy adaptM2TValueStrategy( |
466 |
UpdateValueStrategy updateStrategy) { |
467 |
updateStrategy.setConverter(modelConverter); |
468 |
return updateStrategy; |
469 |
} |
470 |
|
471 |
/** |
472 |
* Configures an existing target-to-model {@link UpdateListStrategy} with |
473 |
* the current state of this editing object by setting the |
474 |
* {@link #setTargetConverter(IConverter) target converter} as the |
475 |
* {@link UpdateListStrategy#setConverter(IConverter) converter} of the |
476 |
* update strategy. |
477 |
* |
478 |
* @param updateStrategy |
479 |
* The {@link UpdateListStrategy} to configure. |
480 |
* @return The passed-in, configured target-to-model |
481 |
* {@link UpdateListStrategy}. |
482 |
* |
483 |
* @see UpdateListStrategy#setConverter(IConverter) |
484 |
*/ |
485 |
public final UpdateListStrategy adaptT2MListStrategy( |
486 |
UpdateListStrategy updateStrategy) { |
487 |
updateStrategy.setConverter(targetConverter); |
488 |
return updateStrategy; |
489 |
} |
490 |
|
491 |
/** |
492 |
* Configures an existing model-to-target {@link UpdateListStrategy} with |
493 |
* the current state of this editing object by setting the |
494 |
* {@link #setModelConverter(IConverter) model converter} as the |
495 |
* {@link UpdateListStrategy#setConverter(IConverter) converter} of the |
496 |
* update strategy. |
497 |
* |
498 |
* @param updateStrategy |
499 |
* The {@link UpdateListStrategy} to configure. |
500 |
* @return The passed-in, configured model-to-target |
501 |
* {@link UpdateListStrategy}. |
502 |
* |
503 |
* @see UpdateListStrategy#setConverter(IConverter) |
504 |
*/ |
505 |
public final UpdateListStrategy adaptM2TListStrategy( |
506 |
UpdateListStrategy updateStrategy) { |
507 |
updateStrategy.setConverter(modelConverter); |
508 |
return updateStrategy; |
509 |
} |
510 |
|
511 |
/** |
512 |
* Configures an existing target-to-model {@link UpdateListStrategy} with |
513 |
* the current state of this editing object by setting the |
514 |
* {@link #setTargetConverter(IConverter) target converter} as the |
515 |
* {@link UpdateSetStrategy#setConverter(IConverter) converter} of the |
516 |
* update strategy. |
517 |
* |
518 |
* @param updateStrategy |
519 |
* The {@link UpdateSetStrategy} to configure. |
520 |
* @return The passed-in, configured target-to-model |
521 |
* {@link UpdateSetStrategy}. |
522 |
* |
523 |
* @see UpdateSetStrategy#setConverter(IConverter) |
524 |
*/ |
525 |
public final UpdateSetStrategy adaptT2MSetStrategy( |
526 |
UpdateSetStrategy updateStrategy) { |
527 |
updateStrategy.setConverter(targetConverter); |
528 |
return updateStrategy; |
529 |
} |
530 |
|
531 |
/** |
532 |
* Configures an existing model-to-target {@link UpdateSetStrategy} with the |
533 |
* current state of this editing object by setting the |
534 |
* {@link #setModelConverter(IConverter) model converter} as the |
535 |
* {@link UpdateSetStrategy#setConverter(IConverter) converter} of the |
536 |
* update strategy. |
537 |
* |
538 |
* @param updateStrategy |
539 |
* The {@link UpdateSetStrategy} to configure. |
540 |
* @return The passed-in, configured model-to-target |
541 |
* {@link UpdateSetStrategy}. |
542 |
* |
543 |
* @see UpdateSetStrategy#setConverter(IConverter) |
544 |
*/ |
545 |
public final UpdateSetStrategy adaptM2TSetStrategy( |
546 |
UpdateSetStrategy updateStrategy) { |
547 |
updateStrategy.setConverter(modelConverter); |
548 |
return updateStrategy; |
549 |
} |
550 |
|
551 |
/** |
552 |
* Converts a target value to a model value by performing the following |
553 |
* steps: |
554 |
* <ul> |
555 |
* <li> |
556 |
* Applying all the {@link #targetConstraints() target constraints} to the |
557 |
* given target value.</li> |
558 |
* <li> |
559 |
* {@link #setTargetConverter(IConverter) Converting} the target value to |
560 |
* the model value.</li> |
561 |
* <li> |
562 |
* Applying all the {@link #modelConstraints() model constraints} to the |
563 |
* converted model value.</li> |
564 |
* <li> |
565 |
* Applying all the {@link #beforeSetModelConstraints() before-set model |
566 |
* constraints} to the converted model value.</li> |
567 |
* </ul> |
568 |
* |
569 |
* <p> |
570 |
* The conversion process will be aborted by returning <code>null</code> |
571 |
* whenever any of the applied validators produces a {@link IStatus |
572 |
* validation status} having {@link IStatus#getSeverity() severity} |
573 |
* <code>IStatus.ERROR</code> or <code>IStatus.CANCEL</code>. During the |
574 |
* conversion process, any validation status whose severity is different |
575 |
* from <code>IStatus.OK</code> will be {@link MultiStatus#merge(IStatus) |
576 |
* aggregated} on the given <code>validationStatus</code>. |
577 |
* </p> |
578 |
* |
579 |
* @param targetValue |
580 |
* The target value to be converted to a model value. |
581 |
* @param validationStatus |
582 |
* Aggregate validation status to which to add the validations |
583 |
* produced during the conversion process. May be |
584 |
* <code>null</code>. |
585 |
* @return The converted model value or <code>null</code> in case the |
586 |
* conversion has been aborted due to a validation error. |
587 |
*/ |
588 |
public final M convertToModel(T targetValue, MultiStatus validationStatus) { |
589 |
if (!applyConstraints(targetConstraints, targetValue, validationStatus)) { |
590 |
return null; |
591 |
} |
592 |
|
593 |
M modelValue = (M) applyConverter(targetConverter, targetValue); |
594 |
|
595 |
if (!applyConstraints(modelConstraints, modelValue, validationStatus)) { |
596 |
return null; |
597 |
} |
598 |
|
599 |
if (!applyConstraints(beforeSetModelConstraints, modelValue, |
600 |
validationStatus)) { |
601 |
return null; |
602 |
} |
603 |
|
604 |
return modelValue; |
605 |
} |
606 |
|
607 |
/** |
608 |
* {@link #setModelConverter(IConverter) Converts} a model value to a target |
609 |
* value. |
610 |
* |
611 |
* @param modelValue |
612 |
* The model value to be converted to a target value. |
613 |
* @return The converted target value. |
614 |
*/ |
615 |
public final T convertToTarget(M modelValue) { |
616 |
return (T) applyConverter(modelConverter, modelValue); |
617 |
} |
618 |
|
619 |
/** |
620 |
* Creates a binding between a target and model observable value on the |
621 |
* given binding context by creating new update strategies which will be |
622 |
* both configured with the state of this editing object before passing them |
623 |
* to the binding. |
624 |
* |
625 |
* <p> |
626 |
* The target-to-model and model-to-target update strategies for the binding |
627 |
* will be created by the methods {@link #createT2MValueStrategy()} and |
628 |
* {@link #createM2TValueStrategy()}, respectively. |
629 |
* </p> |
630 |
* |
631 |
* @param dbc |
632 |
* The binding context on which to create the value binding. |
633 |
* @param targetObservableValue |
634 |
* The target observable value of the binding. |
635 |
* @param modelObservableValue |
636 |
* The model observable value of the binding. |
637 |
* @return The new value binding. |
638 |
* |
639 |
* @see #createT2MValueStrategy() |
640 |
* @see #createM2TValueStrategy() |
641 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
642 |
* UpdateValueStrategy, UpdateValueStrategy) |
643 |
*/ |
644 |
public final Binding bindValue(DataBindingContext dbc, |
645 |
IObservableValue targetObservableValue, |
646 |
IObservableValue modelObservableValue) { |
647 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
648 |
createT2MValueStrategy(), createM2TValueStrategy()); |
649 |
} |
650 |
|
651 |
/** |
652 |
* Creates a binding between a target and model observable value on the |
653 |
* given binding context by creating new update strategies with the provided |
654 |
* update policies which will be both configured with the state of this |
655 |
* editing object before passing them to the binding. |
656 |
* |
657 |
* @param dbc |
658 |
* The binding context on which to create the value binding. |
659 |
* @param targetObservableValue |
660 |
* The target observable value of the binding. |
661 |
* @param modelObservableValue |
662 |
* The model observable value of the binding. |
663 |
* @param t2mUpdatePolicy |
664 |
* The update policy for the target-to-model |
665 |
* {@link UpdateValueStrategy} which is |
666 |
* {@link #createT2MValueStrategy(int) created} and passed to the |
667 |
* new binding. |
668 |
* @param m2tUpdatePolicy |
669 |
* The update policy for the model-to-target |
670 |
* {@link UpdateValueStrategy} which is |
671 |
* {@link #createM2TValueStrategy(int) created} and passed to the |
672 |
* new binding. |
673 |
* @return The new value binding. |
674 |
* |
675 |
* @see #createT2MValueStrategy(int) |
676 |
* @see #createM2TValueStrategy(int) |
677 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
678 |
* UpdateValueStrategy, UpdateValueStrategy) |
679 |
*/ |
680 |
public final Binding bindValue(DataBindingContext dbc, |
681 |
IObservableValue targetObservableValue, |
682 |
IObservableValue modelObservableValue, int t2mUpdatePolicy, |
683 |
int m2tUpdatePolicy) { |
684 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
685 |
createT2MValueStrategy(t2mUpdatePolicy), |
686 |
createM2TValueStrategy(m2tUpdatePolicy)); |
687 |
} |
688 |
|
689 |
/** |
690 |
* Creates a binding between a target and model observable value on the |
691 |
* given binding context by using the provided update strategies which will |
692 |
* be both configured with the state of this editing object before passing |
693 |
* them to the binding. |
694 |
* |
695 |
* @param dbc |
696 |
* The binding context on which to create the value binding. |
697 |
* @param targetObservableValue |
698 |
* The target observable value of the binding. |
699 |
* @param modelObservableValue |
700 |
* The model observable value of the binding. |
701 |
* @param t2mUpdateStrategy |
702 |
* The target-to-model {@link UpdateValueStrategy} of the binding |
703 |
* to be {@link #adaptT2MValueStrategy(UpdateValueStrategy) |
704 |
* configured} with the state of this editing object before |
705 |
* passing it to the binding. |
706 |
* @param m2tUpdateStrategy |
707 |
* The model-to-target {@link UpdateValueStrategy} of the binding |
708 |
* to be {@link #adaptM2TValueStrategy(UpdateValueStrategy) |
709 |
* configured} with the state of this editing object before |
710 |
* passing it to the binding. |
711 |
* @return The new value binding. |
712 |
* |
713 |
* @see #adaptT2MValueStrategy(UpdateValueStrategy) |
714 |
* @see #adaptM2TValueStrategy(UpdateValueStrategy) |
715 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
716 |
* UpdateValueStrategy, UpdateValueStrategy) |
717 |
*/ |
718 |
public final Binding bindValue(DataBindingContext dbc, |
719 |
IObservableValue targetObservableValue, |
720 |
IObservableValue modelObservableValue, |
721 |
UpdateValueStrategy t2mUpdateStrategy, |
722 |
UpdateValueStrategy m2tUpdateStrategy) { |
723 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
724 |
adaptT2MValueStrategy(t2mUpdateStrategy), |
725 |
adaptM2TValueStrategy(m2tUpdateStrategy)); |
726 |
} |
727 |
|
728 |
/** |
729 |
* Creates a binding between a target and model observable list on the given |
730 |
* binding context by creating new update strategies which will be both |
731 |
* configured with the state of this editing object before passing them to |
732 |
* the binding. |
733 |
* |
734 |
* <p> |
735 |
* The target-to-model and model-to-target update strategies for the binding |
736 |
* will be created by the methods {@link #createT2MListStrategy()} and |
737 |
* {@link #createM2TListStrategy()}, respectively. |
738 |
* </p> |
739 |
* |
740 |
* @param dbc |
741 |
* The binding context on which to create the list binding. |
742 |
* @param targetObservableList |
743 |
* The target observable list of the binding. |
744 |
* @param modelObservableList |
745 |
* The model observable list of the binding. |
746 |
* @return The new list binding. |
747 |
* |
748 |
* @see #createT2MListStrategy() |
749 |
* @see #createM2TListStrategy() |
750 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
751 |
* UpdateListStrategy, UpdateListStrategy) |
752 |
*/ |
753 |
public final Binding bindList(DataBindingContext dbc, |
754 |
IObservableList targetObservableList, |
755 |
IObservableList modelObservableList) { |
756 |
return dbc.bindList(targetObservableList, modelObservableList, |
757 |
createT2MListStrategy(), createM2TListStrategy()); |
758 |
} |
759 |
|
760 |
/** |
761 |
* Creates a binding between a target and model observable list on the given |
762 |
* binding context by creating new update strategies with the provided |
763 |
* update policies which will be both configured with the state of this |
764 |
* editing object before passing them to the binding. |
765 |
* |
766 |
* @param dbc |
767 |
* The binding context on which to create the list binding. |
768 |
* @param targetObservableList |
769 |
* The target observable list of the binding. |
770 |
* @param modelObservableList |
771 |
* The model observable list of the binding. |
772 |
* @param t2mUpdatePolicy |
773 |
* The update policy for the target-to-model |
774 |
* {@link UpdateListStrategy} which is |
775 |
* {@link #createT2MListStrategy(int) created} and passed to the |
776 |
* new binding. |
777 |
* @param m2tUpdatePolicy |
778 |
* The update policy for the model-to-target |
779 |
* {@link UpdateListStrategy} which is |
780 |
* {@link #createM2TListStrategy(int) created} and passed to the |
781 |
* new binding. |
782 |
* @return The new list binding. |
783 |
* |
784 |
* @see #createT2MListStrategy(int) |
785 |
* @see #createM2TListStrategy(int) |
786 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
787 |
* UpdateListStrategy, UpdateListStrategy) |
788 |
*/ |
789 |
public final Binding bindList(DataBindingContext dbc, |
790 |
IObservableList targetObservableList, |
791 |
IObservableList modelObservableList, int t2mUpdatePolicy, |
792 |
int m2tUpdatePolicy) { |
793 |
return dbc.bindList(targetObservableList, modelObservableList, |
794 |
createT2MListStrategy(t2mUpdatePolicy), |
795 |
createM2TListStrategy(m2tUpdatePolicy)); |
796 |
} |
797 |
|
798 |
/** |
799 |
* Creates a binding between a target and model observable list on the given |
800 |
* binding context by using the provided update strategies which will be |
801 |
* both configured with the state of this editing object before passing them |
802 |
* to the binding. |
803 |
* |
804 |
* @param dbc |
805 |
* The binding context on which to create the list binding. |
806 |
* @param targetObservableList |
807 |
* The target observable list of the binding. |
808 |
* @param modelObservableList |
809 |
* The model observable list of the binding. |
810 |
* @param t2mUpdateStrategy |
811 |
* The target-to-model {@link UpdateListStrategy} of the binding |
812 |
* to be {@link #adaptT2MListStrategy(UpdateListStrategy) |
813 |
* configured} with the state of this editing object before |
814 |
* passing it to the binding. |
815 |
* @param m2tUpdateStrategy |
816 |
* The model-to-target {@link UpdateListStrategy} of the binding |
817 |
* to be {@link #adaptM2TListStrategy(UpdateListStrategy) |
818 |
* configured} with the state of this editing object before |
819 |
* passing it to the binding. |
820 |
* @return The new list binding. |
821 |
* |
822 |
* @see #adaptT2MListStrategy(UpdateListStrategy) |
823 |
* @see #adaptM2TListStrategy(UpdateListStrategy) |
824 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
825 |
* UpdateListStrategy, UpdateListStrategy) |
826 |
*/ |
827 |
public final Binding bindList(DataBindingContext dbc, |
828 |
IObservableList targetObservableList, |
829 |
IObservableList modelObservableList, |
830 |
UpdateListStrategy t2mUpdateStrategy, |
831 |
UpdateListStrategy m2tUpdateStrategy) { |
832 |
return dbc.bindList(targetObservableList, modelObservableList, |
833 |
adaptT2MListStrategy(t2mUpdateStrategy), |
834 |
adaptM2TListStrategy(m2tUpdateStrategy)); |
835 |
} |
836 |
|
837 |
/** |
838 |
* Creates a binding between a target and model observable set on the given |
839 |
* binding context by creating new update strategies which will be both |
840 |
* configured with the state of this editing object before passing them to |
841 |
* the binding. |
842 |
* |
843 |
* <p> |
844 |
* The target-to-model and model-to-target update strategies for the binding |
845 |
* will be created by the methods {@link #createT2MSetStrategy()} and |
846 |
* {@link #createM2TSetStrategy()}, respectively. |
847 |
* </p> |
848 |
* |
849 |
* @param dbc |
850 |
* The binding context on which to create the set binding. |
851 |
* @param targetObservableSet |
852 |
* The target observable set of the binding. |
853 |
* @param modelObservableSet |
854 |
* The model observable set of the binding. |
855 |
* @return The new set binding. |
856 |
* |
857 |
* @see #createT2MSetStrategy() |
858 |
* @see #createM2TSetStrategy() |
859 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
860 |
* UpdateSetStrategy, UpdateSetStrategy) |
861 |
*/ |
862 |
public final Binding bindSet(DataBindingContext dbc, |
863 |
IObservableSet targetObservableSet, |
864 |
IObservableSet modelObservableSet) { |
865 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
866 |
createT2MSetStrategy(), createM2TSetStrategy()); |
867 |
} |
868 |
|
869 |
/** |
870 |
* Creates a binding between a target and model observable set on the given |
871 |
* binding context by creating new update strategies with the provided |
872 |
* update policies which will be both configured with the state of this |
873 |
* editing object before passing them to the binding. |
874 |
* |
875 |
* @param dbc |
876 |
* The binding context on which to create the set binding. |
877 |
* @param targetObservableSet |
878 |
* The target observable set of the binding. |
879 |
* @param modelObservableSet |
880 |
* The model observable set of the binding. |
881 |
* @param t2mUpdatePolicy |
882 |
* The update policy for the target-to-model |
883 |
* {@link UpdateSetStrategy} which is |
884 |
* {@link #createT2MSetStrategy(int) created} and passed to the |
885 |
* new binding. |
886 |
* @param m2tUpdatePolicy |
887 |
* The update policy for the model-to-target |
888 |
* {@link UpdateSetStrategy} which is |
889 |
* {@link #createM2TSetStrategy(int) created} and passed to the |
890 |
* new binding. |
891 |
* @return The new set binding. |
892 |
* |
893 |
* @see #createT2MSetStrategy(int) |
894 |
* @see #createM2TSetStrategy(int) |
895 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
896 |
* UpdateSetStrategy, UpdateSetStrategy) |
897 |
*/ |
898 |
public final Binding bindSet(DataBindingContext dbc, |
899 |
IObservableSet targetObservableSet, |
900 |
IObservableSet modelObservableSet, int t2mUpdatePolicy, |
901 |
int m2tUpdatePolicy) { |
902 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
903 |
createT2MSetStrategy(t2mUpdatePolicy), |
904 |
createM2TSetStrategy(m2tUpdatePolicy)); |
905 |
} |
906 |
|
907 |
/** |
908 |
* Creates a binding between a target and model observable set on the given |
909 |
* binding context by using the provided update strategies which will be |
910 |
* both configured with the state of this editing object before passing them |
911 |
* to the binding. |
912 |
* |
913 |
* @param dbc |
914 |
* The binding context on which to create the set binding. |
915 |
* @param targetObservableSet |
916 |
* The target observable set of the binding. |
917 |
* @param modelObservableSet |
918 |
* The model observable set of the binding. |
919 |
* @param t2mUpdateStrategy |
920 |
* The target-to-model {@link UpdateSetStrategy} of the binding |
921 |
* to be {@link #adaptT2MSetStrategy(UpdateSetStrategy) |
922 |
* configured} with the state of this editing object before |
923 |
* passing it to the binding. |
924 |
* @param m2tUpdateStrategy |
925 |
* The model-to-target {@link UpdateSetStrategy} of the binding |
926 |
* to be {@link #adaptM2TSetStrategy(UpdateSetStrategy) |
927 |
* configured} with the state of this editing object before |
928 |
* passing it to the binding. |
929 |
* @return The new set binding. |
930 |
* |
931 |
* @see #adaptT2MSetStrategy(UpdateSetStrategy) |
932 |
* @see #adaptM2TSetStrategy(UpdateSetStrategy) |
933 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
934 |
* UpdateSetStrategy, UpdateSetStrategy) |
935 |
*/ |
936 |
public final Binding bindSet(DataBindingContext dbc, |
937 |
IObservableSet targetObservableSet, |
938 |
IObservableSet modelObservableSet, |
939 |
UpdateSetStrategy t2mUpdateStrategy, |
940 |
UpdateSetStrategy m2tUpdateStrategy) { |
941 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
942 |
adaptT2MSetStrategy(t2mUpdateStrategy), |
943 |
adaptM2TSetStrategy(m2tUpdateStrategy)); |
944 |
} |
945 |
|
946 |
private static IValidator createValidator(Constraints<?> constraints) { |
947 |
return constraints != null ? constraints.createValidator() : null; |
948 |
} |
949 |
|
950 |
private static boolean applyConstraints(Constraints<?> constraints, |
951 |
Object value, MultiStatus aggregateStatus) { |
952 |
IValidator validator = createValidator(constraints); |
953 |
if (validator != null) { |
954 |
IStatus validationStatus = validator.validate(value); |
955 |
if (aggregateStatus != null && !validationStatus.isOK()) { |
956 |
aggregateStatus.merge(validationStatus); |
957 |
} |
958 |
return isValid(validationStatus); |
959 |
} |
960 |
return true; |
961 |
} |
962 |
|
963 |
private static Object applyConverter(IConverter converter, Object value) { |
964 |
return converter != null ? converter.convert(value) : value; |
965 |
} |
966 |
|
967 |
private static boolean isValid(IStatus status) { |
968 |
return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); |
969 |
} |
970 |
} |