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.4 |
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 |
@SuppressWarnings("unchecked") |
594 |
M modelValue = (M) applyConverter(targetConverter, targetValue); |
595 |
|
596 |
if (!applyConstraints(modelConstraints, modelValue, validationStatus)) { |
597 |
return null; |
598 |
} |
599 |
|
600 |
if (!applyConstraints(beforeSetModelConstraints, modelValue, |
601 |
validationStatus)) { |
602 |
return null; |
603 |
} |
604 |
|
605 |
return modelValue; |
606 |
} |
607 |
|
608 |
/** |
609 |
* {@link #setModelConverter(IConverter) Converts} a model value to a target |
610 |
* value. |
611 |
* |
612 |
* @param modelValue |
613 |
* The model value to be converted to a target value. |
614 |
* @return The converted target value. |
615 |
*/ |
616 |
@SuppressWarnings("unchecked") |
617 |
public final T convertToTarget(M modelValue) { |
618 |
return (T) applyConverter(modelConverter, modelValue); |
619 |
} |
620 |
|
621 |
/** |
622 |
* Creates a binding between a target and model observable value on the |
623 |
* given binding context by creating new update strategies which will be |
624 |
* both configured with the state of this editing object before passing them |
625 |
* to the binding. |
626 |
* |
627 |
* <p> |
628 |
* The target-to-model and model-to-target update strategies for the binding |
629 |
* will be created by the methods {@link #createT2MValueStrategy()} and |
630 |
* {@link #createM2TValueStrategy()}, respectively. |
631 |
* </p> |
632 |
* |
633 |
* @param dbc |
634 |
* The binding context on which to create the value binding. |
635 |
* @param targetObservableValue |
636 |
* The target observable value of the binding. |
637 |
* @param modelObservableValue |
638 |
* The model observable value of the binding. |
639 |
* @return The new value binding. |
640 |
* |
641 |
* @see #createT2MValueStrategy() |
642 |
* @see #createM2TValueStrategy() |
643 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
644 |
* UpdateValueStrategy, UpdateValueStrategy) |
645 |
*/ |
646 |
public final Binding bindValue(DataBindingContext dbc, |
647 |
IObservableValue targetObservableValue, |
648 |
IObservableValue modelObservableValue) { |
649 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
650 |
createT2MValueStrategy(), createM2TValueStrategy()); |
651 |
} |
652 |
|
653 |
/** |
654 |
* Creates a binding between a target and model observable value on the |
655 |
* given binding context by creating new update strategies with the provided |
656 |
* update policies which will be both configured with the state of this |
657 |
* editing object before passing them to the binding. |
658 |
* |
659 |
* @param dbc |
660 |
* The binding context on which to create the value binding. |
661 |
* @param targetObservableValue |
662 |
* The target observable value of the binding. |
663 |
* @param modelObservableValue |
664 |
* The model observable value of the binding. |
665 |
* @param t2mUpdatePolicy |
666 |
* The update policy for the target-to-model |
667 |
* {@link UpdateValueStrategy} which is |
668 |
* {@link #createT2MValueStrategy(int) created} and passed to the |
669 |
* new binding. |
670 |
* @param m2tUpdatePolicy |
671 |
* The update policy for the model-to-target |
672 |
* {@link UpdateValueStrategy} which is |
673 |
* {@link #createM2TValueStrategy(int) created} and passed to the |
674 |
* new binding. |
675 |
* @return The new value binding. |
676 |
* |
677 |
* @see #createT2MValueStrategy(int) |
678 |
* @see #createM2TValueStrategy(int) |
679 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
680 |
* UpdateValueStrategy, UpdateValueStrategy) |
681 |
*/ |
682 |
public final Binding bindValue(DataBindingContext dbc, |
683 |
IObservableValue targetObservableValue, |
684 |
IObservableValue modelObservableValue, int t2mUpdatePolicy, |
685 |
int m2tUpdatePolicy) { |
686 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
687 |
createT2MValueStrategy(t2mUpdatePolicy), |
688 |
createM2TValueStrategy(m2tUpdatePolicy)); |
689 |
} |
690 |
|
691 |
/** |
692 |
* Creates a binding between a target and model observable value on the |
693 |
* given binding context by using the provided update strategies which will |
694 |
* be both configured with the state of this editing object before passing |
695 |
* them to the binding. |
696 |
* |
697 |
* @param dbc |
698 |
* The binding context on which to create the value binding. |
699 |
* @param targetObservableValue |
700 |
* The target observable value of the binding. |
701 |
* @param modelObservableValue |
702 |
* The model observable value of the binding. |
703 |
* @param t2mUpdateStrategy |
704 |
* The target-to-model {@link UpdateValueStrategy} of the binding |
705 |
* to be {@link #adaptT2MValueStrategy(UpdateValueStrategy) |
706 |
* configured} with the state of this editing object before |
707 |
* passing it to the binding. |
708 |
* @param m2tUpdateStrategy |
709 |
* The model-to-target {@link UpdateValueStrategy} of the binding |
710 |
* to be {@link #adaptM2TValueStrategy(UpdateValueStrategy) |
711 |
* configured} with the state of this editing object before |
712 |
* passing it to the binding. |
713 |
* @return The new value binding. |
714 |
* |
715 |
* @see #adaptT2MValueStrategy(UpdateValueStrategy) |
716 |
* @see #adaptM2TValueStrategy(UpdateValueStrategy) |
717 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
718 |
* UpdateValueStrategy, UpdateValueStrategy) |
719 |
*/ |
720 |
public final Binding bindValue(DataBindingContext dbc, |
721 |
IObservableValue targetObservableValue, |
722 |
IObservableValue modelObservableValue, |
723 |
UpdateValueStrategy t2mUpdateStrategy, |
724 |
UpdateValueStrategy m2tUpdateStrategy) { |
725 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
726 |
adaptT2MValueStrategy(t2mUpdateStrategy), |
727 |
adaptM2TValueStrategy(m2tUpdateStrategy)); |
728 |
} |
729 |
|
730 |
/** |
731 |
* Creates a binding between a target and model observable list on the given |
732 |
* binding context by creating new update strategies which will be both |
733 |
* configured with the state of this editing object before passing them to |
734 |
* the binding. |
735 |
* |
736 |
* <p> |
737 |
* The target-to-model and model-to-target update strategies for the binding |
738 |
* will be created by the methods {@link #createT2MListStrategy()} and |
739 |
* {@link #createM2TListStrategy()}, respectively. |
740 |
* </p> |
741 |
* |
742 |
* @param dbc |
743 |
* The binding context on which to create the list binding. |
744 |
* @param targetObservableList |
745 |
* The target observable list of the binding. |
746 |
* @param modelObservableList |
747 |
* The model observable list of the binding. |
748 |
* @return The new list binding. |
749 |
* |
750 |
* @see #createT2MListStrategy() |
751 |
* @see #createM2TListStrategy() |
752 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
753 |
* UpdateListStrategy, UpdateListStrategy) |
754 |
*/ |
755 |
public final Binding bindList(DataBindingContext dbc, |
756 |
IObservableList targetObservableList, |
757 |
IObservableList modelObservableList) { |
758 |
return dbc.bindList(targetObservableList, modelObservableList, |
759 |
createT2MListStrategy(), createM2TListStrategy()); |
760 |
} |
761 |
|
762 |
/** |
763 |
* Creates a binding between a target and model observable list on the given |
764 |
* binding context by creating new update strategies with the provided |
765 |
* update policies which will be both configured with the state of this |
766 |
* editing object before passing them to the binding. |
767 |
* |
768 |
* @param dbc |
769 |
* The binding context on which to create the list binding. |
770 |
* @param targetObservableList |
771 |
* The target observable list of the binding. |
772 |
* @param modelObservableList |
773 |
* The model observable list of the binding. |
774 |
* @param t2mUpdatePolicy |
775 |
* The update policy for the target-to-model |
776 |
* {@link UpdateListStrategy} which is |
777 |
* {@link #createT2MListStrategy(int) created} and passed to the |
778 |
* new binding. |
779 |
* @param m2tUpdatePolicy |
780 |
* The update policy for the model-to-target |
781 |
* {@link UpdateListStrategy} which is |
782 |
* {@link #createM2TListStrategy(int) created} and passed to the |
783 |
* new binding. |
784 |
* @return The new list binding. |
785 |
* |
786 |
* @see #createT2MListStrategy(int) |
787 |
* @see #createM2TListStrategy(int) |
788 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
789 |
* UpdateListStrategy, UpdateListStrategy) |
790 |
*/ |
791 |
public final Binding bindList(DataBindingContext dbc, |
792 |
IObservableList targetObservableList, |
793 |
IObservableList modelObservableList, int t2mUpdatePolicy, |
794 |
int m2tUpdatePolicy) { |
795 |
return dbc.bindList(targetObservableList, modelObservableList, |
796 |
createT2MListStrategy(t2mUpdatePolicy), |
797 |
createM2TListStrategy(m2tUpdatePolicy)); |
798 |
} |
799 |
|
800 |
/** |
801 |
* Creates a binding between a target and model observable list on the given |
802 |
* binding context by using the provided update strategies which will be |
803 |
* both configured with the state of this editing object before passing them |
804 |
* to the binding. |
805 |
* |
806 |
* @param dbc |
807 |
* The binding context on which to create the list binding. |
808 |
* @param targetObservableList |
809 |
* The target observable list of the binding. |
810 |
* @param modelObservableList |
811 |
* The model observable list of the binding. |
812 |
* @param t2mUpdateStrategy |
813 |
* The target-to-model {@link UpdateListStrategy} of the binding |
814 |
* to be {@link #adaptT2MListStrategy(UpdateListStrategy) |
815 |
* configured} with the state of this editing object before |
816 |
* passing it to the binding. |
817 |
* @param m2tUpdateStrategy |
818 |
* The model-to-target {@link UpdateListStrategy} of the binding |
819 |
* to be {@link #adaptM2TListStrategy(UpdateListStrategy) |
820 |
* configured} with the state of this editing object before |
821 |
* passing it to the binding. |
822 |
* @return The new list binding. |
823 |
* |
824 |
* @see #adaptT2MListStrategy(UpdateListStrategy) |
825 |
* @see #adaptM2TListStrategy(UpdateListStrategy) |
826 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
827 |
* UpdateListStrategy, UpdateListStrategy) |
828 |
*/ |
829 |
public final Binding bindList(DataBindingContext dbc, |
830 |
IObservableList targetObservableList, |
831 |
IObservableList modelObservableList, |
832 |
UpdateListStrategy t2mUpdateStrategy, |
833 |
UpdateListStrategy m2tUpdateStrategy) { |
834 |
return dbc.bindList(targetObservableList, modelObservableList, |
835 |
adaptT2MListStrategy(t2mUpdateStrategy), |
836 |
adaptM2TListStrategy(m2tUpdateStrategy)); |
837 |
} |
838 |
|
839 |
/** |
840 |
* Creates a binding between a target and model observable set on the given |
841 |
* binding context by creating new update strategies which will be both |
842 |
* configured with the state of this editing object before passing them to |
843 |
* the binding. |
844 |
* |
845 |
* <p> |
846 |
* The target-to-model and model-to-target update strategies for the binding |
847 |
* will be created by the methods {@link #createT2MSetStrategy()} and |
848 |
* {@link #createM2TSetStrategy()}, respectively. |
849 |
* </p> |
850 |
* |
851 |
* @param dbc |
852 |
* The binding context on which to create the set binding. |
853 |
* @param targetObservableSet |
854 |
* The target observable set of the binding. |
855 |
* @param modelObservableSet |
856 |
* The model observable set of the binding. |
857 |
* @return The new set binding. |
858 |
* |
859 |
* @see #createT2MSetStrategy() |
860 |
* @see #createM2TSetStrategy() |
861 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
862 |
* UpdateSetStrategy, UpdateSetStrategy) |
863 |
*/ |
864 |
public final Binding bindSet(DataBindingContext dbc, |
865 |
IObservableSet targetObservableSet, |
866 |
IObservableSet modelObservableSet) { |
867 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
868 |
createT2MSetStrategy(), createM2TSetStrategy()); |
869 |
} |
870 |
|
871 |
/** |
872 |
* Creates a binding between a target and model observable set on the given |
873 |
* binding context by creating new update strategies with the provided |
874 |
* update policies which will be both configured with the state of this |
875 |
* editing object before passing them to the binding. |
876 |
* |
877 |
* @param dbc |
878 |
* The binding context on which to create the set binding. |
879 |
* @param targetObservableSet |
880 |
* The target observable set of the binding. |
881 |
* @param modelObservableSet |
882 |
* The model observable set of the binding. |
883 |
* @param t2mUpdatePolicy |
884 |
* The update policy for the target-to-model |
885 |
* {@link UpdateSetStrategy} which is |
886 |
* {@link #createT2MSetStrategy(int) created} and passed to the |
887 |
* new binding. |
888 |
* @param m2tUpdatePolicy |
889 |
* The update policy for the model-to-target |
890 |
* {@link UpdateSetStrategy} which is |
891 |
* {@link #createM2TSetStrategy(int) created} and passed to the |
892 |
* new binding. |
893 |
* @return The new set binding. |
894 |
* |
895 |
* @see #createT2MSetStrategy(int) |
896 |
* @see #createM2TSetStrategy(int) |
897 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
898 |
* UpdateSetStrategy, UpdateSetStrategy) |
899 |
*/ |
900 |
public final Binding bindSet(DataBindingContext dbc, |
901 |
IObservableSet targetObservableSet, |
902 |
IObservableSet modelObservableSet, int t2mUpdatePolicy, |
903 |
int m2tUpdatePolicy) { |
904 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
905 |
createT2MSetStrategy(t2mUpdatePolicy), |
906 |
createM2TSetStrategy(m2tUpdatePolicy)); |
907 |
} |
908 |
|
909 |
/** |
910 |
* Creates a binding between a target and model observable set on the given |
911 |
* binding context by using the provided update strategies which will be |
912 |
* both configured with the state of this editing object before passing them |
913 |
* to the binding. |
914 |
* |
915 |
* @param dbc |
916 |
* The binding context on which to create the set binding. |
917 |
* @param targetObservableSet |
918 |
* The target observable set of the binding. |
919 |
* @param modelObservableSet |
920 |
* The model observable set of the binding. |
921 |
* @param t2mUpdateStrategy |
922 |
* The target-to-model {@link UpdateSetStrategy} of the binding |
923 |
* to be {@link #adaptT2MSetStrategy(UpdateSetStrategy) |
924 |
* configured} with the state of this editing object before |
925 |
* passing it to the binding. |
926 |
* @param m2tUpdateStrategy |
927 |
* The model-to-target {@link UpdateSetStrategy} of the binding |
928 |
* to be {@link #adaptM2TSetStrategy(UpdateSetStrategy) |
929 |
* configured} with the state of this editing object before |
930 |
* passing it to the binding. |
931 |
* @return The new set binding. |
932 |
* |
933 |
* @see #adaptT2MSetStrategy(UpdateSetStrategy) |
934 |
* @see #adaptM2TSetStrategy(UpdateSetStrategy) |
935 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
936 |
* UpdateSetStrategy, UpdateSetStrategy) |
937 |
*/ |
938 |
public final Binding bindSet(DataBindingContext dbc, |
939 |
IObservableSet targetObservableSet, |
940 |
IObservableSet modelObservableSet, |
941 |
UpdateSetStrategy t2mUpdateStrategy, |
942 |
UpdateSetStrategy m2tUpdateStrategy) { |
943 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
944 |
adaptT2MSetStrategy(t2mUpdateStrategy), |
945 |
adaptM2TSetStrategy(m2tUpdateStrategy)); |
946 |
} |
947 |
|
948 |
private static IValidator createValidator(Constraints<?> constraints) { |
949 |
return constraints != null ? constraints.createValidator() : null; |
950 |
} |
951 |
|
952 |
private static boolean applyConstraints(Constraints<?> constraints, |
953 |
Object value, MultiStatus aggregateStatus) { |
954 |
IValidator validator = createValidator(constraints); |
955 |
if (validator != null) { |
956 |
IStatus validationStatus = validator.validate(value); |
957 |
if (aggregateStatus != null && !validationStatus.isOK()) { |
958 |
aggregateStatus.merge(validationStatus); |
959 |
} |
960 |
return isValid(validationStatus); |
961 |
} |
962 |
return true; |
963 |
} |
964 |
|
965 |
private static Object applyConverter(IConverter converter, Object value) { |
966 |
return converter != null ? converter.convert(value) : value; |
967 |
} |
968 |
|
969 |
private static boolean isValid(IStatus status) { |
970 |
return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); |
971 |
} |
972 |
} |