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