1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
|
/*
* Copyright 2011 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.data.fieldgroup;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.logging.Logger;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.TransactionalPropertyWrapper;
import com.vaadin.tools.ReflectTools;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
/**
* FieldGroup provides an easy way of binding fields to data and handling
* commits of these fields.
* <p>
* The functionality of FieldGroup is similar to {@link Form} but
* {@link FieldGroup} does not handle layouts in any way. The typical use case
* is to create a layout outside the FieldGroup and then use FieldGroup to bind
* the fields to a data source.
* </p>
* <p>
* {@link FieldGroup} is not a UI component so it cannot be added to a layout.
* Using the buildAndBind methods {@link FieldGroup} can create fields for you
* using a FieldGroupFieldFactory but you still have to add them to the correct
* position in your layout.
* </p>
*
* @author Vaadin Ltd
* @since 7.0
*/
public class FieldGroup implements Serializable {
private static final Logger logger = Logger.getLogger(FieldGroup.class
.getName());
private Item itemDataSource;
private boolean buffered = true;
private boolean enabled = true;
private boolean readOnly = false;
private HashMap<Object, Field<?>> propertyIdToField = new HashMap<Object, Field<?>>();
private LinkedHashMap<Field<?>, Object> fieldToPropertyId = new LinkedHashMap<Field<?>, Object>();
private List<CommitHandler> commitHandlers = new ArrayList<CommitHandler>();
/**
* The field factory used by builder methods.
*/
private FieldGroupFieldFactory fieldFactory = new DefaultFieldGroupFieldFactory();
/**
* Constructs a field binder. Use {@link #setItemDataSource(Item)} to set a
* data source for the field binder.
*
*/
public FieldGroup() {
}
/**
* Constructs a field binder that uses the given data source.
*
* @param itemDataSource
* The data source to bind the fields to
*/
public FieldGroup(Item itemDataSource) {
setItemDataSource(itemDataSource);
}
/**
* Updates the item that is used by this FieldBinder. Rebinds all fields to
* the properties in the new item.
*
* @param itemDataSource
* The new item to use
*/
public void setItemDataSource(Item itemDataSource) {
this.itemDataSource = itemDataSource;
for (Field<?> f : fieldToPropertyId.keySet()) {
bind(f, fieldToPropertyId.get(f));
}
}
/**
* Gets the item used by this FieldBinder. Note that you must call
* {@link #commit()} for the item to be updated unless buffered mode has
* been switched off.
*
* @see #setBuffered(boolean)
* @see #commit()
*
* @return The item used by this FieldBinder
*/
public Item getItemDataSource() {
return itemDataSource;
}
/**
* Checks the buffered mode for the bound fields.
* <p>
*
* @see #setBuffered(boolean) for more details on buffered mode
*
* @see Field#isBuffered()
* @return true if buffered mode is on, false otherwise
*
*/
public boolean isBuffered() {
return buffered;
}
/**
* Sets the buffered mode for the bound fields.
* <p>
* When buffered mode is on the item will not be updated until
* {@link #commit()} is called. If buffered mode is off the item will be
* updated once the fields are updated.
* </p>
* <p>
* The default is to use buffered mode.
* </p>
*
* @see Field#setBuffered(boolean)
* @param buffered
* true to turn on buffered mode, false otherwise
*/
public void setBuffered(boolean buffered) {
if (buffered == this.buffered) {
return;
}
this.buffered = buffered;
for (Field<?> field : getFields()) {
field.setBuffered(buffered);
}
}
/**
* Returns the enabled status for the fields.
* <p>
* Note that this will not accurately represent the enabled status of all
* fields if you change the enabled status of the fields through some other
* method than {@link #setEnabled(boolean)}.
*
* @return true if the fields are enabled, false otherwise
*/
public boolean isEnabled() {
return enabled;
}
/**
* Updates the enabled state of all bound fields.
*
* @param fieldsEnabled
* true to enable all bound fields, false to disable them
*/
public void setEnabled(boolean fieldsEnabled) {
enabled = fieldsEnabled;
for (Field<?> field : getFields()) {
field.setEnabled(fieldsEnabled);
}
}
/**
* Returns the read only status for the fields.
* <p>
* Note that this will not accurately represent the read only status of all
* fields if you change the read only status of the fields through some
* other method than {@link #setReadOnly(boolean)}.
*
* @return true if the fields are set to read only, false otherwise
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* Updates the read only state of all bound fields.
*
* @param fieldsReadOnly
* true to set all bound fields to read only, false to set them
* to read write
*/
public void setReadOnly(boolean fieldsReadOnly) {
readOnly = fieldsReadOnly;
}
/**
* Returns a collection of all fields that have been bound.
* <p>
* The fields are not returned in any specific order.
* </p>
*
* @return A collection with all bound Fields
*/
public Collection<Field<?>> getFields() {
return fieldToPropertyId.keySet();
}
/**
* Binds the field with the given propertyId from the current item. If an
* item has not been set then the binding is postponed until the item is set
* using {@link #setItemDataSource(Item)}.
* <p>
* This method also adds validators when applicable.
* </p>
*
* @param field
* The field to bind
* @param propertyId
* The propertyId to bind to the field
* @throws BindException
* If the property id is already bound to another field by this
* field binder
*/
public void bind(Field<?> field, Object propertyId) throws BindException {
if (propertyIdToField.containsKey(propertyId)
&& propertyIdToField.get(propertyId) != field) {
throw new BindException("Property id " + propertyId
+ " is already bound to another field");
}
fieldToPropertyId.put(field, propertyId);
propertyIdToField.put(propertyId, field);
if (itemDataSource == null) {
// Will be bound when data source is set
return;
}
field.setPropertyDataSource(wrapInTransactionalProperty(getItemProperty(propertyId)));
configureField(field);
}
private <T> Property.Transactional<T> wrapInTransactionalProperty(
Property<T> itemProperty) {
return new TransactionalPropertyWrapper<T>(itemProperty);
}
/**
* Gets the property with the given property id from the item.
*
* @param propertyId
* The id if the property to find
* @return The property with the given id from the item
* @throws BindException
* If the property was not found in the item or no item has been
* set
*/
protected Property<?> getItemProperty(Object propertyId)
throws BindException {
Item item = getItemDataSource();
if (item == null) {
throw new BindException("Could not lookup property with id "
+ propertyId + " as no item has been set");
}
Property<?> p = item.getItemProperty(propertyId);
if (p == null) {
throw new BindException("A property with id " + propertyId
+ " was not found in the item");
}
return p;
}
/**
* Detaches the field from its property id and removes it from this
* FieldBinder.
* <p>
* Note that the field is not detached from its property data source if it
* is no longer connected to the same property id it was bound to using this
* FieldBinder.
*
* @param field
* The field to detach
* @throws BindException
* If the field is not bound by this field binder or not bound
* to the correct property id
*/
public void unbind(Field<?> field) throws BindException {
Object propertyId = fieldToPropertyId.get(field);
if (propertyId == null) {
throw new BindException(
"The given field is not part of this FieldBinder");
}
Property fieldDataSource = field.getPropertyDataSource();
if (fieldDataSource instanceof TransactionalPropertyWrapper) {
fieldDataSource = ((TransactionalPropertyWrapper) fieldDataSource)
.getWrappedProperty();
}
if (fieldDataSource == getItemProperty(propertyId)) {
field.setPropertyDataSource(null);
}
fieldToPropertyId.remove(field);
propertyIdToField.remove(propertyId);
}
/**
* Configures a field with the settings set for this FieldBinder.
* <p>
* By default this updates the buffered, read only and enabled state of the
* field. Also adds validators when applicable.
*
* @param field
* The field to update
*/
protected void configureField(Field<?> field) {
field.setBuffered(isBuffered());
field.setEnabled(isEnabled());
field.setReadOnly(isReadOnly());
}
/**
* Gets the type of the property with the given property id.
*
* @param propertyId
* The propertyId. Must be find
* @return The type of the property
*/
protected Class<?> getPropertyType(Object propertyId) throws BindException {
if (getItemDataSource() == null) {
throw new BindException(
"Property type for '"
+ propertyId
+ "' could not be determined. No item data source has been set.");
}
Property<?> p = getItemDataSource().getItemProperty(propertyId);
if (p == null) {
throw new BindException(
"Property type for '"
+ propertyId
+ "' could not be determined. No property with that id was found.");
}
return p.getType();
}
/**
* Returns a collection of all property ids that have been bound to fields.
* <p>
* Note that this will return property ids even before the item has been
* set. In that case it returns the property ids that will be bound once the
* item is set.
* </p>
* <p>
* No guarantee is given for the order of the property ids
* </p>
*
* @return A collection of bound property ids
*/
public Collection<Object> getBoundPropertyIds() {
return Collections.unmodifiableCollection(propertyIdToField.keySet());
}
/**
* Returns a collection of all property ids that exist in the item set using
* {@link #setItemDataSource(Item)} but have not been bound to fields.
* <p>
* Will always return an empty collection before an item has been set using
* {@link #setItemDataSource(Item)}.
* </p>
* <p>
* No guarantee is given for the order of the property ids
* </p>
*
* @return A collection of property ids that have not been bound to fields
*/
public Collection<Object> getUnboundPropertyIds() {
if (getItemDataSource() == null) {
return new ArrayList<Object>();
}
List<Object> unboundPropertyIds = new ArrayList<Object>();
unboundPropertyIds.addAll(getItemDataSource().getItemPropertyIds());
unboundPropertyIds.removeAll(propertyIdToField.keySet());
return unboundPropertyIds;
}
/**
* Commits all changes done to the bound fields.
* <p>
* Calls all {@link CommitHandler}s before and after committing the field
* changes to the item data source. The whole commit is aborted and state is
* restored to what it was before commit was called if any
* {@link CommitHandler} throws a CommitException or there is a problem
* committing the fields
*
* @throws CommitException
* If the commit was aborted
*/
public void commit() throws CommitException {
if (!isBuffered()) {
// Not using buffered mode, nothing to do
return;
}
for (Field<?> f : fieldToPropertyId.keySet()) {
((Property.Transactional<?>) f.getPropertyDataSource())
.startTransaction();
}
try {
firePreCommitEvent();
// Commit the field values to the properties
for (Field<?> f : fieldToPropertyId.keySet()) {
f.commit();
}
firePostCommitEvent();
// Commit the properties
for (Field<?> f : fieldToPropertyId.keySet()) {
((Property.Transactional<?>) f.getPropertyDataSource())
.commit();
}
} catch (Exception e) {
for (Field<?> f : fieldToPropertyId.keySet()) {
try {
((Property.Transactional<?>) f.getPropertyDataSource())
.rollback();
} catch (Exception rollbackException) {
// FIXME: What to do ?
}
}
throw new CommitException("Commit failed", e);
}
}
/**
* Sends a preCommit event to all registered commit handlers
*
* @throws CommitException
* If the commit should be aborted
*/
private void firePreCommitEvent() throws CommitException {
CommitHandler[] handlers = commitHandlers
.toArray(new CommitHandler[commitHandlers.size()]);
for (CommitHandler handler : handlers) {
handler.preCommit(new CommitEvent(this));
}
}
/**
* Sends a postCommit event to all registered commit handlers
*
* @throws CommitException
* If the commit should be aborted
*/
private void firePostCommitEvent() throws CommitException {
CommitHandler[] handlers = commitHandlers
.toArray(new CommitHandler[commitHandlers.size()]);
for (CommitHandler handler : handlers) {
handler.postCommit(new CommitEvent(this));
}
}
/**
* Discards all changes done to the bound fields.
* <p>
* Only has effect if buffered mode is used.
*
*/
public void discard() {
for (Field<?> f : fieldToPropertyId.keySet()) {
try {
f.discard();
} catch (Exception e) {
// TODO: handle exception
// What can we do if discard fails other than try to discard all
// other fields?
}
}
}
/**
* Returns the field that is bound to the given property id
*
* @param propertyId
* The property id to use to lookup the field
* @return The field that is bound to the property id or null if no field is
* bound to that property id
*/
public Field<?> getField(Object propertyId) {
return propertyIdToField.get(propertyId);
}
/**
* Returns the property id that is bound to the given field
*
* @param field
* The field to use to lookup the property id
* @return The property id that is bound to the field or null if the field
* is not bound to any property id by this FieldBinder
*/
public Object getPropertyId(Field<?> field) {
return fieldToPropertyId.get(field);
}
/**
* Adds a commit handler.
* <p>
* The commit handler is called before the field values are committed to the
* item ( {@link CommitHandler#preCommit(CommitEvent)}) and after the item
* has been updated ({@link CommitHandler#postCommit(CommitEvent)}). If a
* {@link CommitHandler} throws a CommitException the whole commit is
* aborted and the fields retain their old values.
*
* @param commitHandler
* The commit handler to add
*/
public void addCommitHandler(CommitHandler commitHandler) {
commitHandlers.add(commitHandler);
}
/**
* Removes the given commit handler.
*
* @see #addCommitHandler(CommitHandler)
*
* @param commitHandler
* The commit handler to remove
*/
public void removeCommitHandler(CommitHandler commitHandler) {
commitHandlers.remove(commitHandler);
}
/**
* Returns a list of all commit handlers for this {@link FieldGroup}.
* <p>
* Use {@link #addCommitHandler(CommitHandler)} and
* {@link #removeCommitHandler(CommitHandler)} to register or unregister a
* commit handler.
*
* @return A collection of commit handlers
*/
protected Collection<CommitHandler> getCommitHandlers() {
return Collections.unmodifiableCollection(commitHandlers);
}
/**
* CommitHandlers are used by {@link FieldGroup#commit()} as part of the
* commit transactions. CommitHandlers can perform custom operations as part
* of the commit and cause the commit to be aborted by throwing a
* {@link CommitException}.
*/
public interface CommitHandler extends Serializable {
/**
* Called before changes are committed to the field and the item is
* updated.
* <p>
* Throw a {@link CommitException} to abort the commit.
*
* @param commitEvent
* An event containing information regarding the commit
* @throws CommitException
* if the commit should be aborted
*/
public void preCommit(CommitEvent commitEvent) throws CommitException;
/**
* Called after changes are committed to the fields and the item is
* updated..
* <p>
* Throw a {@link CommitException} to abort the commit.
*
* @param commitEvent
* An event containing information regarding the commit
* @throws CommitException
* if the commit should be aborted
*/
public void postCommit(CommitEvent commitEvent) throws CommitException;
}
/**
* FIXME javadoc
*
*/
public static class CommitEvent implements Serializable {
private FieldGroup fieldBinder;
private CommitEvent(FieldGroup fieldBinder) {
this.fieldBinder = fieldBinder;
}
/**
* Returns the field binder that this commit relates to
*
* @return The FieldBinder that is being committed.
*/
public FieldGroup getFieldBinder() {
return fieldBinder;
}
}
/**
* Checks the validity of the bound fields.
* <p>
* Call the {@link Field#validate()} for the fields to get the individual
* error messages.
*
* @return true if all bound fields are valid, false otherwise.
*/
public boolean isValid() {
try {
for (Field<?> field : getFields()) {
field.validate();
}
return true;
} catch (InvalidValueException e) {
return false;
}
}
/**
* Checks if any bound field has been modified.
*
* @return true if at least on field has been modified, false otherwise
*/
public boolean isModified() {
for (Field<?> field : getFields()) {
if (field.isModified()) {
return true;
}
}
return false;
}
/**
* Gets the field factory for the {@link FieldGroup}. The field factory is
* only used when {@link FieldGroup} creates a new field.
*
* @return The field factory in use
*
*/
public FieldGroupFieldFactory getFieldFactory() {
return fieldFactory;
}
/**
* Sets the field factory for the {@link FieldGroup}. The field factory is
* only used when {@link FieldGroup} creates a new field.
*
* @param fieldFactory
* The field factory to use
*/
public void setFieldFactory(FieldGroupFieldFactory fieldFactory) {
this.fieldFactory = fieldFactory;
}
/**
* Binds member fields found in the given object.
* <p>
* This method processes all (Java) member fields whose type extends
* {@link Field} and that can be mapped to a property id. Property id
* mapping is done based on the field name or on a @{@link PropertyId}
* annotation on the field. All non-null fields for which a property id can
* be determined are bound to the property id.
* </p>
* <p>
* For example:
*
* <pre>
* public class MyForm extends VerticalLayout {
* private TextField firstName = new TextField("First name");
* @PropertyId("last")
* private TextField lastName = new TextField("Last name");
* private TextField age = new TextField("Age"); ... }
*
* MyForm myForm = new MyForm();
* ...
* fieldGroup.bindMemberFields(myForm);
* </pre>
*
* </p>
* This binds the firstName TextField to a "firstName" property in the item,
* lastName TextField to a "last" property and the age TextField to a "age"
* property.
*
* @param objectWithMemberFields
* The object that contains (Java) member fields to bind
* @throws BindException
* If there is a problem binding a field
*/
public void bindMemberFields(Object objectWithMemberFields)
throws BindException {
buildAndBindMemberFields(objectWithMemberFields, false);
}
/**
* Binds member fields found in the given object and builds member fields
* that have not been initialized.
* <p>
* This method processes all (Java) member fields whose type extends
* {@link Field} and that can be mapped to a property id. Property id
* mapping is done based on the field name or on a @{@link PropertyId}
* annotation on the field. Fields that are not initialized (null) are built
* using the field factory. All non-null fields for which a property id can
* be determined are bound to the property id.
* </p>
* <p>
* For example:
*
* <pre>
* public class MyForm extends VerticalLayout {
* private TextField firstName = new TextField("First name");
* @PropertyId("last")
* private TextField lastName = new TextField("Last name");
* private TextField age;
*
* MyForm myForm = new MyForm();
* ...
* fieldGroup.buildAndBindMemberFields(myForm);
* </pre>
*
* </p>
* <p>
* This binds the firstName TextField to a "firstName" property in the item,
* lastName TextField to a "last" property and builds an age TextField using
* the field factory and then binds it to the "age" property.
* </p>
*
* @param objectWithMemberFields
* The object that contains (Java) member fields to build and
* bind
* @throws BindException
* If there is a problem binding or building a field
*/
public void buildAndBindMemberFields(Object objectWithMemberFields)
throws BindException {
buildAndBindMemberFields(objectWithMemberFields, true);
}
/**
* Binds member fields found in the given object and optionally builds
* member fields that have not been initialized.
* <p>
* This method processes all (Java) member fields whose type extends
* {@link Field} and that can be mapped to a property id. Property id
* mapping is done based on the field name or on a @{@link PropertyId}
* annotation on the field. Fields that are not initialized (null) are built
* using the field factory is buildFields is true. All non-null fields for
* which a property id can be determined are bound to the property id.
* </p>
*
* @param objectWithMemberFields
* The object that contains (Java) member fields to build and
* bind
* @throws BindException
* If there is a problem binding or building a field
*/
protected void buildAndBindMemberFields(Object objectWithMemberFields,
boolean buildFields) throws BindException {
Class<?> objectClass = objectWithMemberFields.getClass();
for (java.lang.reflect.Field memberField : objectClass
.getDeclaredFields()) {
if (!Field.class.isAssignableFrom(memberField.getType())) {
// Process next field
continue;
}
PropertyId propertyIdAnnotation = memberField
.getAnnotation(PropertyId.class);
Class<? extends Field> fieldType = (Class<? extends Field>) memberField
.getType();
Object propertyId = null;
if (propertyIdAnnotation != null) {
// @PropertyId(propertyId) always overrides property id
propertyId = propertyIdAnnotation.value();
} else {
propertyId = memberField.getName();
}
// Ensure that the property id exists
Class<?> propertyType;
try {
propertyType = getPropertyType(propertyId);
} catch (BindException e) {
// Property id was not found, skip this field
continue;
}
Field<?> field;
try {
// Get the field from the object
field = (Field<?>) ReflectTools.getJavaFieldValue(
objectWithMemberFields, memberField);
} catch (Exception e) {
// If we cannot determine the value, just skip the field and try
// the next one
continue;
}
if (field == null && buildFields) {
Caption captionAnnotation = memberField
.getAnnotation(Caption.class);
String caption;
if (captionAnnotation != null) {
caption = captionAnnotation.value();
} else {
caption = DefaultFieldFactory
.createCaptionByPropertyId(propertyId);
}
// Create the component (Field)
field = build(caption, propertyType, fieldType);
// Store it in the field
try {
ReflectTools.setJavaFieldValue(objectWithMemberFields,
memberField, field);
} catch (IllegalArgumentException e) {
throw new BindException("Could not assign value to field '"
+ memberField.getName() + "'", e);
} catch (IllegalAccessException e) {
throw new BindException("Could not assign value to field '"
+ memberField.getName() + "'", e);
} catch (InvocationTargetException e) {
throw new BindException("Could not assign value to field '"
+ memberField.getName() + "'", e);
}
}
if (field != null) {
// Bind it to the property id
bind(field, propertyId);
}
}
}
public static class CommitException extends Exception {
public CommitException() {
super();
// TODO Auto-generated constructor stub
}
public CommitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CommitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public CommitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
public static class BindException extends RuntimeException {
public BindException(String message) {
super(message);
}
public BindException(String message, Throwable t) {
super(message, t);
}
}
/**
* Builds a field and binds it to the given property id using the field
* binder.
*
* @param propertyId
* The property id to bind to. Must be present in the field
* finder.
* @throws BindException
* If there is a problem while building or binding
* @return The created and bound field
*/
public Field<?> buildAndBind(Object propertyId) throws BindException {
String caption = DefaultFieldFactory
.createCaptionByPropertyId(propertyId);
return buildAndBind(caption, propertyId);
}
/**
* Builds a field using the given caption and binds it to the given property
* id using the field binder.
*
* @param caption
* The caption for the field
* @param propertyId
* The property id to bind to. Must be present in the field
* finder.
* @throws BindException
* If there is a problem while building or binding
* @return The created and bound field. Can be any type of {@link Field}.
*/
public Field<?> buildAndBind(String caption, Object propertyId)
throws BindException {
Class<?> type = getPropertyType(propertyId);
return buildAndBind(caption, propertyId, Field.class);
}
/**
* Builds a field using the given caption and binds it to the given property
* id using the field binder. Ensures the new field is of the given type.
*
* @param caption
* The caption for the field
* @param propertyId
* The property id to bind to. Must be present in the field
* finder.
* @throws BindException
* If the field could not be created
* @return The created and bound field. Can be any type of {@link Field}.
*/
public <T extends Field> T buildAndBind(String caption, Object propertyId,
Class<T> fieldType) throws BindException {
Class<?> type = getPropertyType(propertyId);
T field = build(caption, type, fieldType);
bind(field, propertyId);
return field;
}
/**
* Creates a field based on the given data type.
* <p>
* The data type is the type that we want to edit using the field. The field
* type is the type of field we want to create, can be {@link Field} if any
* Field is good.
* </p>
*
* @param caption
* The caption for the new field
* @param dataType
* The data model type that we want to edit using the field
* @param fieldType
* The type of field that we want to create
* @return A Field capable of editing the given type
* @throws BindException
* If the field could not be created
*/
protected <T extends Field> T build(String caption, Class<?> dataType,
Class<T> fieldType) throws BindException {
T field = getFieldFactory().createField(dataType, fieldType);
if (field == null) {
throw new BindException("Unable to build a field of type "
+ fieldType.getName() + " for editing "
+ dataType.getName());
}
field.setCaption(caption);
return field;
}
}
|