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 |
* Convenience method which {@link Constraints#addValidator(IValidator) |
196 |
* adds} the given validator to the set of {@link #targetConstraints()}. |
197 |
* |
198 |
* @param validator |
199 |
* The validator to add to the target constraints. |
200 |
* @return This editing instance for method chaining. |
201 |
* |
202 |
* @see Constraints#addValidator(IValidator) |
203 |
* @see #targetConstraints() |
204 |
*/ |
205 |
public final Editing addTargetValidator(IValidator validator) { |
206 |
targetConstraints().addValidator(validator); |
207 |
return this; |
208 |
} |
209 |
|
210 |
/** |
211 |
* Convenience method which {@link Constraints#addValidator(IValidator) |
212 |
* adds} the given validator to the set of {@link #modelConstraints()}. |
213 |
* |
214 |
* @param validator |
215 |
* The validator to add to the model constraints. |
216 |
* @return This editing instance for method chaining. |
217 |
* |
218 |
* @see Constraints#addValidator(IValidator) |
219 |
* @see #modelConstraints() |
220 |
*/ |
221 |
public final Editing addModelValidator(IValidator validator) { |
222 |
modelConstraints().addValidator(validator); |
223 |
return this; |
224 |
} |
225 |
|
226 |
/** |
227 |
* Convenience method which {@link Constraints#addValidator(IValidator) |
228 |
* adds} the given validator to the set of |
229 |
* {@link #beforeSetModelConstraints()}. |
230 |
* |
231 |
* @param validator |
232 |
* The validator to add to the before-set model constraints. |
233 |
* @return This editing instance for method chaining. |
234 |
* |
235 |
* @see Constraints#addValidator(IValidator) |
236 |
* @see #beforeSetModelConstraints |
237 |
*/ |
238 |
public final Editing addBeforeSetModelValidator(IValidator validator) { |
239 |
beforeSetModelConstraints().addValidator(validator); |
240 |
return this; |
241 |
} |
242 |
|
243 |
/** |
244 |
* Sets the target-to-model converter for this editing instance. |
245 |
* |
246 |
* @param converter |
247 |
* The target-to-model converter to set. |
248 |
*/ |
249 |
protected final void setTargetConverter(IConverter converter) { |
250 |
this.targetConverter = converter; |
251 |
} |
252 |
|
253 |
/** |
254 |
* Sets the model-to-target converter for this editing instance. |
255 |
* |
256 |
* @param converter |
257 |
* The model-to-target converter to set. |
258 |
*/ |
259 |
protected final void setModelConverter(IConverter converter) { |
260 |
this.modelConverter = converter; |
261 |
} |
262 |
|
263 |
/** |
264 |
* Creates a new target-to-model {@link UpdateValueStrategy} with a default |
265 |
* update policy configured by the current state of this editing object. |
266 |
* |
267 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
268 |
* the current state of this editing object. |
269 |
* |
270 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
271 |
* @see #applyToT2MValueStrategy(UpdateValueStrategy) |
272 |
*/ |
273 |
public final UpdateValueStrategy createT2MValueStrategy() { |
274 |
return applyToT2MValueStrategy(new UpdateValueStrategy()); |
275 |
} |
276 |
|
277 |
/** |
278 |
* Creates a new target-to-model {@link UpdateValueStrategy} with the given |
279 |
* update policy configured by the current state of this editing object. |
280 |
* |
281 |
* @param updatePolicy |
282 |
* The update policy to use. |
283 |
* @return A new target-to-model {@link UpdateValueStrategy} configured by |
284 |
* the current state of this editing object. |
285 |
* |
286 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
287 |
* @see #applyToT2MValueStrategy(UpdateValueStrategy) |
288 |
*/ |
289 |
public final UpdateValueStrategy createT2MValueStrategy(int updatePolicy) { |
290 |
return applyToT2MValueStrategy(new UpdateValueStrategy(updatePolicy)); |
291 |
} |
292 |
|
293 |
/** |
294 |
* Creates a new model-to-target {@link UpdateValueStrategy} with a default |
295 |
* update policy configured by the current state of this editing object. |
296 |
* |
297 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
298 |
* the current state of this editing object. |
299 |
* |
300 |
* @see UpdateValueStrategy#UpdateValueStrategy() |
301 |
* @see #applyToM2TValueStrategy(UpdateValueStrategy) |
302 |
*/ |
303 |
public final UpdateValueStrategy createM2TValueStrategy() { |
304 |
return applyToM2TValueStrategy(new UpdateValueStrategy()); |
305 |
} |
306 |
|
307 |
/** |
308 |
* Creates a new model-to-target {@link UpdateValueStrategy} with the given |
309 |
* update policy configured by the current state of this editing object. |
310 |
* |
311 |
* @param updatePolicy |
312 |
* The update policy to use. |
313 |
* @return A new model-to-target {@link UpdateValueStrategy} configured by |
314 |
* the current state of this editing object. |
315 |
* |
316 |
* @see UpdateValueStrategy#UpdateValueStrategy(int) |
317 |
* @see #applyToM2TValueStrategy(UpdateValueStrategy) |
318 |
*/ |
319 |
public final UpdateValueStrategy createM2TValueStrategy(int updatePolicy) { |
320 |
return applyToM2TValueStrategy(new UpdateValueStrategy(updatePolicy)); |
321 |
} |
322 |
|
323 |
/** |
324 |
* Creates a new target-to-model {@link UpdateListStrategy} with a default |
325 |
* update policy configured by the current state of this editing object. |
326 |
* |
327 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
328 |
* the current state of this editing object. |
329 |
* |
330 |
* @see UpdateListStrategy#UpdateListStrategy() |
331 |
* @see #applyToT2MListStrategy(UpdateListStrategy) |
332 |
*/ |
333 |
public final UpdateListStrategy createT2MListStrategy() { |
334 |
return applyToT2MListStrategy(new UpdateListStrategy()); |
335 |
} |
336 |
|
337 |
/** |
338 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
339 |
* update policy configured by the current state of this editing object. |
340 |
* |
341 |
* @param updatePolicy |
342 |
* The update policy to use. |
343 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
344 |
* the current state of this editing object. |
345 |
* |
346 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
347 |
* @see #applyToT2MListStrategy(UpdateListStrategy) |
348 |
*/ |
349 |
public final UpdateListStrategy createT2MListStrategy(int updatePolicy) { |
350 |
return applyToT2MListStrategy(new UpdateListStrategy(updatePolicy)); |
351 |
} |
352 |
|
353 |
/** |
354 |
* Creates a new model-to-target {@link UpdateListStrategy} with a default |
355 |
* update policy configured by the current state of this editing object. |
356 |
* |
357 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
358 |
* the current state of this editing object. |
359 |
* |
360 |
* @see UpdateListStrategy#UpdateListStrategy() |
361 |
* @see #applyToM2TListStrategy(UpdateListStrategy) |
362 |
*/ |
363 |
public final UpdateListStrategy createM2TListStrategy() { |
364 |
return applyToM2TListStrategy(new UpdateListStrategy()); |
365 |
} |
366 |
|
367 |
/** |
368 |
* Creates a new model-to-target {@link UpdateListStrategy} with the given |
369 |
* update policy configured by the current state of this editing object. |
370 |
* |
371 |
* @param updatePolicy |
372 |
* The update policy to use. |
373 |
* @return A new model-to-target {@link UpdateListStrategy} configured by |
374 |
* the current state of this editing object. |
375 |
* |
376 |
* @see UpdateListStrategy#UpdateListStrategy(int) |
377 |
* @see #applyToM2TListStrategy(UpdateListStrategy) |
378 |
*/ |
379 |
public final UpdateListStrategy createM2TListStrategy(int updatePolicy) { |
380 |
return applyToM2TListStrategy(new UpdateListStrategy(updatePolicy)); |
381 |
} |
382 |
|
383 |
/** |
384 |
* Creates a new target-to-model {@link UpdateSetStrategy} with a default |
385 |
* update policy configured by the current state of this editing object. |
386 |
* |
387 |
* @return A new target-to-model {@link UpdateSetStrategy} configured by the |
388 |
* current state of this editing object. |
389 |
* |
390 |
* @see UpdateListStrategy#UpdateListStrategy() |
391 |
* @see #applyToT2MSetStrategy(UpdateSetStrategy) |
392 |
*/ |
393 |
public final UpdateSetStrategy createT2MSetStrategy() { |
394 |
return applyToT2MSetStrategy(new UpdateSetStrategy()); |
395 |
} |
396 |
|
397 |
/** |
398 |
* Creates a new target-to-model {@link UpdateListStrategy} with the given |
399 |
* update policy configured by the current state of this editing object. |
400 |
* |
401 |
* @param updatePolicy |
402 |
* The update policy to use. |
403 |
* @return A new target-to-model {@link UpdateListStrategy} configured by |
404 |
* the current state of this editing object. |
405 |
* |
406 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
407 |
* @see #applyToT2MSetStrategy(UpdateSetStrategy) |
408 |
*/ |
409 |
public final UpdateSetStrategy createT2MSetStrategy(int updatePolicy) { |
410 |
return applyToT2MSetStrategy(new UpdateSetStrategy(updatePolicy)); |
411 |
} |
412 |
|
413 |
/** |
414 |
* Creates a new model-to-target {@link UpdateSetStrategy} with a default |
415 |
* update policy configured by the current state of this editing object. |
416 |
* |
417 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
418 |
* current state of this editing object. |
419 |
* |
420 |
* @see UpdateSetStrategy#UpdateSetStrategy() |
421 |
* @see #applyToM2TSetStrategy(UpdateSetStrategy) |
422 |
*/ |
423 |
public final UpdateSetStrategy createM2TSetStrategy() { |
424 |
return applyToM2TSetStrategy(new UpdateSetStrategy()); |
425 |
} |
426 |
|
427 |
/** |
428 |
* Creates a new model-to-target {@link UpdateSetStrategy} with the given |
429 |
* update policy configured by the current state of this editing object. |
430 |
* |
431 |
* @param updatePolicy |
432 |
* The update policy to use. |
433 |
* @return A new model-to-target {@link UpdateSetStrategy} configured by the |
434 |
* current state of this editing object. |
435 |
* |
436 |
* @see UpdateSetStrategy#UpdateSetStrategy(int) |
437 |
* @see #applyToM2TSetStrategy(UpdateSetStrategy) |
438 |
*/ |
439 |
public final UpdateSetStrategy createM2TSetStrategy(int updatePolicy) { |
440 |
return applyToM2TSetStrategy(new UpdateSetStrategy(updatePolicy)); |
441 |
} |
442 |
|
443 |
/** |
444 |
* Configures an existing target-to-model {@link UpdateValueStrategy} with |
445 |
* the current state of this editing object. |
446 |
* |
447 |
* <p> |
448 |
* The configuration is done as follows: |
449 |
* <ul> |
450 |
* <li> |
451 |
* The {@link Constraints#createValidator() validator} of the |
452 |
* {@link #targetConstraints() target constraints} is set as the |
453 |
* {@link UpdateValueStrategy#setAfterGetValidator(IValidator) after-get |
454 |
* validator} of the update strategy.</li> |
455 |
* <li>The {@link #setTargetConverter(IConverter) target converter} is set |
456 |
* as the {@link UpdateValueStrategy#setConverter(IConverter) converter} of |
457 |
* the update strategy.</li> |
458 |
* <li> |
459 |
* The {@link Constraints#createValidator() validator} of the |
460 |
* {@link #modelConstraints() model constraints} is set as the |
461 |
* {@link UpdateValueStrategy#setAfterConvertValidator(IValidator) |
462 |
* after-convert validator} of the update strategy.</li> |
463 |
* <li>The {@link Constraints#createValidator() validator} of the |
464 |
* {@link #beforeSetModelConstraints() before-set model constraints} is set |
465 |
* as the {@link UpdateValueStrategy#setBeforeSetValidator(IValidator) |
466 |
* before-set validator} of the update strategy.</li> |
467 |
* </ul> |
468 |
* </p> |
469 |
* |
470 |
* @param updateStrategy |
471 |
* The {@link UpdateValueStrategy} to configure. |
472 |
* @return The passed-in, configured target-to-model |
473 |
* {@link UpdateValueStrategy}. |
474 |
* |
475 |
* @see UpdateValueStrategy#setAfterGetValidator(IValidator) |
476 |
* @see UpdateValueStrategy#setConverter(IConverter) |
477 |
* @see UpdateValueStrategy#setAfterConvertValidator(IValidator) |
478 |
* @see UpdateValueStrategy#setBeforeSetValidator(IValidator) |
479 |
*/ |
480 |
public final UpdateValueStrategy applyToT2MValueStrategy( |
481 |
UpdateValueStrategy updateStrategy) { |
482 |
updateStrategy.setAfterGetValidator(createValidator(targetConstraints)); |
483 |
updateStrategy.setConverter(targetConverter); |
484 |
updateStrategy |
485 |
.setAfterConvertValidator(createValidator(modelConstraints)); |
486 |
updateStrategy |
487 |
.setBeforeSetValidator(createValidator(beforeSetModelConstraints)); |
488 |
return updateStrategy; |
489 |
} |
490 |
|
491 |
/** |
492 |
* Configures an existing model-to-target {@link UpdateValueStrategy} with |
493 |
* the current state of this editing object by setting the |
494 |
* {@link #setModelConverter(IConverter) model converter} as the |
495 |
* {@link UpdateValueStrategy#setConverter(IConverter) converter} of the |
496 |
* update strategy. |
497 |
* |
498 |
* @param updateStrategy |
499 |
* The {@link UpdateValueStrategy} to configure. |
500 |
* @return The passed-in, configured model-to-target |
501 |
* {@link UpdateValueStrategy}. |
502 |
* |
503 |
* @see UpdateValueStrategy#setConverter(IConverter) |
504 |
*/ |
505 |
public final UpdateValueStrategy applyToM2TValueStrategy( |
506 |
UpdateValueStrategy 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 UpdateListStrategy#setConverter(IConverter) converter} of the |
516 |
* update strategy. |
517 |
* |
518 |
* @param updateStrategy |
519 |
* The {@link UpdateListStrategy} to configure. |
520 |
* @return The passed-in, configured target-to-model |
521 |
* {@link UpdateListStrategy}. |
522 |
* |
523 |
* @see UpdateListStrategy#setConverter(IConverter) |
524 |
*/ |
525 |
public final UpdateListStrategy applyToT2MListStrategy( |
526 |
UpdateListStrategy updateStrategy) { |
527 |
updateStrategy.setConverter(targetConverter); |
528 |
return updateStrategy; |
529 |
} |
530 |
|
531 |
/** |
532 |
* Configures an existing model-to-target {@link UpdateListStrategy} with |
533 |
* the current state of this editing object by setting the |
534 |
* {@link #setModelConverter(IConverter) model converter} as the |
535 |
* {@link UpdateListStrategy#setConverter(IConverter) converter} of the |
536 |
* update strategy. |
537 |
* |
538 |
* @param updateStrategy |
539 |
* The {@link UpdateListStrategy} to configure. |
540 |
* @return The passed-in, configured model-to-target |
541 |
* {@link UpdateListStrategy}. |
542 |
* |
543 |
* @see UpdateListStrategy#setConverter(IConverter) |
544 |
*/ |
545 |
public final UpdateListStrategy applyToM2TListStrategy( |
546 |
UpdateListStrategy updateStrategy) { |
547 |
updateStrategy.setConverter(modelConverter); |
548 |
return updateStrategy; |
549 |
} |
550 |
|
551 |
/** |
552 |
* Configures an existing target-to-model {@link UpdateListStrategy} with |
553 |
* the current state of this editing object by setting the |
554 |
* {@link #setTargetConverter(IConverter) target converter} as the |
555 |
* {@link UpdateSetStrategy#setConverter(IConverter) converter} of the |
556 |
* update strategy. |
557 |
* |
558 |
* @param updateStrategy |
559 |
* The {@link UpdateSetStrategy} to configure. |
560 |
* @return The passed-in, configured target-to-model |
561 |
* {@link UpdateSetStrategy}. |
562 |
* |
563 |
* @see UpdateSetStrategy#setConverter(IConverter) |
564 |
*/ |
565 |
public final UpdateSetStrategy applyToT2MSetStrategy( |
566 |
UpdateSetStrategy updateStrategy) { |
567 |
updateStrategy.setConverter(targetConverter); |
568 |
return updateStrategy; |
569 |
} |
570 |
|
571 |
/** |
572 |
* Configures an existing model-to-target {@link UpdateSetStrategy} with the |
573 |
* current state of this editing object by setting the |
574 |
* {@link #setModelConverter(IConverter) model converter} as the |
575 |
* {@link UpdateSetStrategy#setConverter(IConverter) converter} of the |
576 |
* update strategy. |
577 |
* |
578 |
* @param updateStrategy |
579 |
* The {@link UpdateSetStrategy} to configure. |
580 |
* @return The passed-in, configured model-to-target |
581 |
* {@link UpdateSetStrategy}. |
582 |
* |
583 |
* @see UpdateSetStrategy#setConverter(IConverter) |
584 |
*/ |
585 |
public final UpdateSetStrategy applyToM2TSetStrategy( |
586 |
UpdateSetStrategy updateStrategy) { |
587 |
updateStrategy.setConverter(modelConverter); |
588 |
return updateStrategy; |
589 |
} |
590 |
|
591 |
/** |
592 |
* Converts a target value to a model value by performing the following |
593 |
* steps: |
594 |
* <ul> |
595 |
* <li> |
596 |
* Applying all the {@link #targetConstraints() target constraints} to the |
597 |
* given target value.</li> |
598 |
* <li> |
599 |
* {@link #setTargetConverter(IConverter) Converting} the target value to |
600 |
* the model value.</li> |
601 |
* <li> |
602 |
* Applying all the {@link #modelConstraints() model constraints} to the |
603 |
* converted model value.</li> |
604 |
* <li> |
605 |
* Applying all the {@link #beforeSetModelConstraints() before-set model |
606 |
* constraints} to the converted model value.</li> |
607 |
* </ul> |
608 |
* |
609 |
* <p> |
610 |
* The conversion process will be aborted by returning <code>null</code> |
611 |
* whenever any of the applied validators produces a {@link IStatus |
612 |
* validation status} having {@link IStatus#getSeverity() severity} |
613 |
* <code>IStatus.ERROR</code> or <code>IStatus.CANCEL</code>. During the |
614 |
* conversion process, any validation status whose severity is different |
615 |
* from <code>IStatus.OK</code> will be {@link MultiStatus#merge(IStatus) |
616 |
* aggregated} on the given <code>validationStatus</code>. |
617 |
* </p> |
618 |
* |
619 |
* @param targetValue |
620 |
* The target value to be converted to a model value. |
621 |
* @param validationStatus |
622 |
* Aggregate validation status to which to add the validations |
623 |
* produced during the conversion process. |
624 |
* @return The converted model value or <code>null</code> in case the |
625 |
* conversion has been aborted due to a validation error. |
626 |
*/ |
627 |
public final Object convertToModel(Object targetValue, |
628 |
MultiStatus validationStatus) { |
629 |
IValidator targetValidator = createValidator(targetConstraints); |
630 |
if (!applyValidator(targetValidator, targetValue, validationStatus)) { |
631 |
return null; |
632 |
} |
633 |
|
634 |
Object modelValue = (targetConverter != null) ? targetConverter |
635 |
.convert(targetValue) : targetValue; |
636 |
|
637 |
IValidator modelValidator = createValidator(modelConstraints); |
638 |
if (!applyValidator(modelValidator, modelValue, validationStatus)) { |
639 |
return null; |
640 |
} |
641 |
|
642 |
IValidator beforeSetModelValidator = createValidator(beforeSetModelConstraints); |
643 |
if (!applyValidator(beforeSetModelValidator, modelValue, |
644 |
validationStatus)) { |
645 |
return null; |
646 |
} |
647 |
|
648 |
return modelValue; |
649 |
} |
650 |
|
651 |
/** |
652 |
* {@link #setModelConverter(IConverter) Converts} a model value to a target |
653 |
* value. |
654 |
* |
655 |
* @param modelValue |
656 |
* The model value to be converted to a target value. |
657 |
* @return The converted target value. |
658 |
*/ |
659 |
public final Object convertToTarget(Object modelValue) { |
660 |
return (modelConverter != null) ? modelConverter.convert(modelValue) |
661 |
: modelValue; |
662 |
} |
663 |
|
664 |
/** |
665 |
* Creates a binding between a target and model observable value on the |
666 |
* given binding context by creating new update strategies which will be |
667 |
* both configured with the state of this editing object before passing them |
668 |
* to the binding. |
669 |
* |
670 |
* <p> |
671 |
* The target-to-model and model-to-target update strategies for the binding |
672 |
* will be created by the methods {@link #createT2MValueStrategy()} and |
673 |
* {@link #createM2TValueStrategy()}, respectively. |
674 |
* </p> |
675 |
* |
676 |
* @param dbc |
677 |
* The binding context on which to create the value binding. |
678 |
* @param targetObservableValue |
679 |
* The target observable value of the binding. |
680 |
* @param modelObservableValue |
681 |
* The model observable value of the binding. |
682 |
* @return The new value binding. |
683 |
* |
684 |
* @see #createT2MValueStrategy() |
685 |
* @see #createM2TValueStrategy() |
686 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
687 |
* UpdateValueStrategy, UpdateValueStrategy) |
688 |
*/ |
689 |
public final Binding bindValue(DataBindingContext dbc, |
690 |
IObservableValue targetObservableValue, |
691 |
IObservableValue modelObservableValue) { |
692 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
693 |
createT2MValueStrategy(), createM2TValueStrategy()); |
694 |
} |
695 |
|
696 |
/** |
697 |
* Creates a binding between a target and model observable value on the |
698 |
* given binding context by creating new update strategies with the provided |
699 |
* update policies which will be both configured with the state of this |
700 |
* editing object before passing them to the binding. |
701 |
* |
702 |
* @param dbc |
703 |
* The binding context on which to create the value binding. |
704 |
* @param targetObservableValue |
705 |
* The target observable value of the binding. |
706 |
* @param modelObservableValue |
707 |
* The model observable value of the binding. |
708 |
* @param t2mUpdatePolicy |
709 |
* The update policy for the target-to-model |
710 |
* {@link UpdateValueStrategy} which is |
711 |
* {@link #createT2MValueStrategy(int) created} and passed to the |
712 |
* new binding. |
713 |
* @param m2tUpdatePolicy |
714 |
* The update policy for the model-to-target |
715 |
* {@link UpdateValueStrategy} which is |
716 |
* {@link #createM2TValueStrategy(int) created} and passed to the |
717 |
* new binding. |
718 |
* @return The new value binding. |
719 |
* |
720 |
* @see #createT2MValueStrategy(int) |
721 |
* @see #createM2TValueStrategy(int) |
722 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
723 |
* UpdateValueStrategy, UpdateValueStrategy) |
724 |
*/ |
725 |
public final Binding bindValue(DataBindingContext dbc, |
726 |
IObservableValue targetObservableValue, |
727 |
IObservableValue modelObservableValue, int t2mUpdatePolicy, |
728 |
int m2tUpdatePolicy) { |
729 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
730 |
createT2MValueStrategy(t2mUpdatePolicy), |
731 |
createM2TValueStrategy(m2tUpdatePolicy)); |
732 |
} |
733 |
|
734 |
/** |
735 |
* Creates a binding between a target and model observable value on the |
736 |
* given binding context by using the provided update strategies which will |
737 |
* be both configured with the state of this editing object before passing |
738 |
* them to the binding. |
739 |
* |
740 |
* @param dbc |
741 |
* The binding context on which to create the value binding. |
742 |
* @param targetObservableValue |
743 |
* The target observable value of the binding. |
744 |
* @param modelObservableValue |
745 |
* The model observable value of the binding. |
746 |
* @param t2mUpdateStrategy |
747 |
* The target-to-model {@link UpdateValueStrategy} of the binding |
748 |
* to be {@link #applyToT2MValueStrategy(UpdateValueStrategy) |
749 |
* configured} with the state of this editing object before |
750 |
* passing it to the binding. |
751 |
* @param m2tUpdateStrategy |
752 |
* The model-to-target {@link UpdateValueStrategy} of the binding |
753 |
* to be {@link #applyToM2TValueStrategy(UpdateValueStrategy) |
754 |
* configured} with the state of this editing object before |
755 |
* passing it to the binding. |
756 |
* @return The new value binding. |
757 |
* |
758 |
* @see #applyToT2MValueStrategy(UpdateValueStrategy) |
759 |
* @see #applyToM2TValueStrategy(UpdateValueStrategy) |
760 |
* @see DataBindingContext#bindValue(IObservableValue, IObservableValue, |
761 |
* UpdateValueStrategy, UpdateValueStrategy) |
762 |
*/ |
763 |
public final Binding bindValue(DataBindingContext dbc, |
764 |
IObservableValue targetObservableValue, |
765 |
IObservableValue modelObservableValue, |
766 |
UpdateValueStrategy t2mUpdateStrategy, |
767 |
UpdateValueStrategy m2tUpdateStrategy) { |
768 |
return dbc.bindValue(targetObservableValue, modelObservableValue, |
769 |
applyToT2MValueStrategy(t2mUpdateStrategy), |
770 |
applyToM2TValueStrategy(m2tUpdateStrategy)); |
771 |
} |
772 |
|
773 |
/** |
774 |
* Creates a binding between a target and model observable list on the given |
775 |
* binding context by creating new update strategies which will be both |
776 |
* configured with the state of this editing object before passing them to |
777 |
* the binding. |
778 |
* |
779 |
* <p> |
780 |
* The target-to-model and model-to-target update strategies for the binding |
781 |
* will be created by the methods {@link #createT2MListStrategy()} and |
782 |
* {@link #createM2TListStrategy()}, respectively. |
783 |
* </p> |
784 |
* |
785 |
* @param dbc |
786 |
* The binding context on which to create the list binding. |
787 |
* @param targetObservableList |
788 |
* The target observable list of the binding. |
789 |
* @param modelObservableList |
790 |
* The model observable list of the binding. |
791 |
* @return The new list binding. |
792 |
* |
793 |
* @see #createT2MListStrategy() |
794 |
* @see #createM2TListStrategy() |
795 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
796 |
* UpdateListStrategy, UpdateListStrategy) |
797 |
*/ |
798 |
public final Binding bindList(DataBindingContext dbc, |
799 |
IObservableList targetObservableList, |
800 |
IObservableList modelObservableList) { |
801 |
return dbc.bindList(targetObservableList, modelObservableList, |
802 |
createT2MListStrategy(), createM2TListStrategy()); |
803 |
} |
804 |
|
805 |
/** |
806 |
* Creates a binding between a target and model observable list on the given |
807 |
* binding context by creating new update strategies with the provided |
808 |
* update policies which will be both configured with the state of this |
809 |
* editing object before passing them to the binding. |
810 |
* |
811 |
* @param dbc |
812 |
* The binding context on which to create the list binding. |
813 |
* @param targetObservableList |
814 |
* The target observable list of the binding. |
815 |
* @param modelObservableList |
816 |
* The model observable list of the binding. |
817 |
* @param t2mUpdatePolicy |
818 |
* The update policy for the target-to-model |
819 |
* {@link UpdateListStrategy} which is |
820 |
* {@link #createT2MListStrategy(int) created} and passed to the |
821 |
* new binding. |
822 |
* @param m2tUpdatePolicy |
823 |
* The update policy for the model-to-target |
824 |
* {@link UpdateListStrategy} which is |
825 |
* {@link #createM2TListStrategy(int) created} and passed to the |
826 |
* new binding. |
827 |
* @return The new list binding. |
828 |
* |
829 |
* @see #createT2MListStrategy(int) |
830 |
* @see #createM2TListStrategy(int) |
831 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
832 |
* UpdateListStrategy, UpdateListStrategy) |
833 |
*/ |
834 |
public final Binding bindList(DataBindingContext dbc, |
835 |
IObservableList targetObservableList, |
836 |
IObservableList modelObservableList, int t2mUpdatePolicy, |
837 |
int m2tUpdatePolicy) { |
838 |
return dbc.bindList(targetObservableList, modelObservableList, |
839 |
createT2MListStrategy(t2mUpdatePolicy), |
840 |
createM2TListStrategy(m2tUpdatePolicy)); |
841 |
} |
842 |
|
843 |
/** |
844 |
* Creates a binding between a target and model observable list on the given |
845 |
* binding context by using the provided update strategies which will be |
846 |
* both configured with the state of this editing object before passing them |
847 |
* to the binding. |
848 |
* |
849 |
* @param dbc |
850 |
* The binding context on which to create the list binding. |
851 |
* @param targetObservableList |
852 |
* The target observable list of the binding. |
853 |
* @param modelObservableList |
854 |
* The model observable list of the binding. |
855 |
* @param t2mUpdateStrategy |
856 |
* The target-to-model {@link UpdateListStrategy} of the binding |
857 |
* to be {@link #applyToT2MListStrategy(UpdateListStrategy) |
858 |
* configured} with the state of this editing object before |
859 |
* passing it to the binding. |
860 |
* @param m2tUpdateStrategy |
861 |
* The model-to-target {@link UpdateListStrategy} of the binding |
862 |
* to be {@link #applyToM2TListStrategy(UpdateListStrategy) |
863 |
* configured} with the state of this editing object before |
864 |
* passing it to the binding. |
865 |
* @return The new list binding. |
866 |
* |
867 |
* @see #applyToT2MListStrategy(UpdateListStrategy) |
868 |
* @see #applyToM2TListStrategy(UpdateListStrategy) |
869 |
* @see DataBindingContext#bindList(IObservableList, IObservableList, |
870 |
* UpdateListStrategy, UpdateListStrategy) |
871 |
*/ |
872 |
public final Binding bindList(DataBindingContext dbc, |
873 |
IObservableList targetObservableList, |
874 |
IObservableList modelObservableList, |
875 |
UpdateListStrategy t2mUpdateStrategy, |
876 |
UpdateListStrategy m2tUpdateStrategy) { |
877 |
return dbc.bindList(targetObservableList, modelObservableList, |
878 |
applyToT2MListStrategy(t2mUpdateStrategy), |
879 |
applyToM2TListStrategy(m2tUpdateStrategy)); |
880 |
} |
881 |
|
882 |
/** |
883 |
* Creates a binding between a target and model observable set on the given |
884 |
* binding context by creating new update strategies which will be both |
885 |
* configured with the state of this editing object before passing them to |
886 |
* the binding. |
887 |
* |
888 |
* <p> |
889 |
* The target-to-model and model-to-target update strategies for the binding |
890 |
* will be created by the methods {@link #createT2MSetStrategy()} and |
891 |
* {@link #createM2TSetStrategy()}, respectively. |
892 |
* </p> |
893 |
* |
894 |
* @param dbc |
895 |
* The binding context on which to create the set binding. |
896 |
* @param targetObservableSet |
897 |
* The target observable set of the binding. |
898 |
* @param modelObservableSet |
899 |
* The model observable set of the binding. |
900 |
* @return The new set binding. |
901 |
* |
902 |
* @see #createT2MSetStrategy() |
903 |
* @see #createM2TSetStrategy() |
904 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
905 |
* UpdateSetStrategy, UpdateSetStrategy) |
906 |
*/ |
907 |
public final Binding bindSet(DataBindingContext dbc, |
908 |
IObservableSet targetObservableSet, |
909 |
IObservableSet modelObservableSet) { |
910 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
911 |
createT2MSetStrategy(), createM2TSetStrategy()); |
912 |
} |
913 |
|
914 |
/** |
915 |
* Creates a binding between a target and model observable set on the given |
916 |
* binding context by creating new update strategies with the provided |
917 |
* update policies which will be both configured with the state of this |
918 |
* editing object before passing them to the binding. |
919 |
* |
920 |
* @param dbc |
921 |
* The binding context on which to create the set binding. |
922 |
* @param targetObservableSet |
923 |
* The target observable set of the binding. |
924 |
* @param modelObservableSet |
925 |
* The model observable set of the binding. |
926 |
* @param t2mUpdatePolicy |
927 |
* The update policy for the target-to-model |
928 |
* {@link UpdateSetStrategy} which is |
929 |
* {@link #createT2MSetStrategy(int) created} and passed to the |
930 |
* new binding. |
931 |
* @param m2tUpdatePolicy |
932 |
* The update policy for the model-to-target |
933 |
* {@link UpdateSetStrategy} which is |
934 |
* {@link #createM2TSetStrategy(int) created} and passed to the |
935 |
* new binding. |
936 |
* @return The new set binding. |
937 |
* |
938 |
* @see #createT2MSetStrategy(int) |
939 |
* @see #createM2TSetStrategy(int) |
940 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
941 |
* UpdateSetStrategy, UpdateSetStrategy) |
942 |
*/ |
943 |
public final Binding bindSet(DataBindingContext dbc, |
944 |
IObservableSet targetObservableSet, |
945 |
IObservableSet modelObservableSet, int t2mUpdatePolicy, |
946 |
int m2tUpdatePolicy) { |
947 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
948 |
createT2MSetStrategy(t2mUpdatePolicy), |
949 |
createM2TSetStrategy(m2tUpdatePolicy)); |
950 |
} |
951 |
|
952 |
/** |
953 |
* Creates a binding between a target and model observable set on the given |
954 |
* binding context by using the provided update strategies which will be |
955 |
* both configured with the state of this editing object before passing them |
956 |
* to the binding. |
957 |
* |
958 |
* @param dbc |
959 |
* The binding context on which to create the set binding. |
960 |
* @param targetObservableSet |
961 |
* The target observable set of the binding. |
962 |
* @param modelObservableSet |
963 |
* The model observable set of the binding. |
964 |
* @param t2mUpdateStrategy |
965 |
* The target-to-model {@link UpdateSetStrategy} of the binding |
966 |
* to be {@link #applyToT2MSetStrategy(UpdateSetStrategy) |
967 |
* configured} with the state of this editing object before |
968 |
* passing it to the binding. |
969 |
* @param m2tUpdateStrategy |
970 |
* The model-to-target {@link UpdateSetStrategy} of the binding |
971 |
* to be {@link #applyToM2TSetStrategy(UpdateSetStrategy) |
972 |
* configured} with the state of this editing object before |
973 |
* passing it to the binding. |
974 |
* @return The new set binding. |
975 |
* |
976 |
* @see #applyToT2MSetStrategy(UpdateSetStrategy) |
977 |
* @see #applyToM2TSetStrategy(UpdateSetStrategy) |
978 |
* @see DataBindingContext#bindSet(IObservableSet, IObservableSet, |
979 |
* UpdateSetStrategy, UpdateSetStrategy) |
980 |
*/ |
981 |
public final Binding bindSet(DataBindingContext dbc, |
982 |
IObservableSet targetObservableSet, |
983 |
IObservableSet modelObservableSet, |
984 |
UpdateSetStrategy t2mUpdateStrategy, |
985 |
UpdateSetStrategy m2tUpdateStrategy) { |
986 |
return dbc.bindSet(targetObservableSet, modelObservableSet, |
987 |
applyToT2MSetStrategy(t2mUpdateStrategy), |
988 |
applyToM2TSetStrategy(m2tUpdateStrategy)); |
989 |
} |
990 |
|
991 |
private static IValidator createValidator(Constraints constraints) { |
992 |
return constraints != null ? constraints.createValidator() : null; |
993 |
} |
994 |
|
995 |
private static boolean applyValidator(IValidator validator, Object value, |
996 |
MultiStatus aggregateStatus) { |
997 |
if (validator != null) { |
998 |
IStatus validationStatus = validator.validate(value); |
999 |
if (!validationStatus.isOK()) { |
1000 |
aggregateStatus.merge(validationStatus); |
1001 |
} |
1002 |
return isValid(validationStatus); |
1003 |
} |
1004 |
return true; |
1005 |
} |
1006 |
|
1007 |
private static boolean isValid(IStatus status) { |
1008 |
return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); |
1009 |
} |
1010 |
} |