99import com .vaadin .ui .ComboBox ;
1010import com .vaadin .ui .Grid ;
1111
12+ import de .symeda .sormas .api .ReferenceDto ;
1213import de .symeda .sormas .api .campaign .form .CampaignFormMetaReferenceDto ;
1314import de .symeda .sormas .api .i18n .Captions ;
1415import de .symeda .sormas .api .i18n .I18nProperties ;
@@ -31,7 +32,7 @@ public CampaignFormsGridComponent(
3132 protected Button .ClickListener newRowEvent () {
3233 return event -> {
3334 final ArrayList <CampaignFormMetaReferenceDto > gridItems = getItems ();
34- gridItems .add (new CampaignFormMetaReferenceDto (UUID . randomUUID (). toString () , "" ));
35+ gridItems .add (new CampaignFormMetaReferenceDto (null , "" ));
3536 grid .setItems (gridItems );
3637 };
3738 }
@@ -40,24 +41,32 @@ protected Button.ClickListener newRowEvent() {
4041 protected Binder <CampaignFormMetaReferenceDto > addColumnsBinder (List <CampaignFormMetaReferenceDto > allElements ) {
4142 final Binder <CampaignFormMetaReferenceDto > binder = new Binder <>();
4243
44+ // This is a bit hacky: The grid is used here to "select" the whole item instead of editing properties
45+ // This is done by replacing uuid and caption of the item
46+
4347 ComboBox <CampaignFormMetaReferenceDto > formCombo = new ComboBox <>(Strings .entityCampaignDataForm , allElements );
4448
4549 Binder .Binding <CampaignFormMetaReferenceDto , CampaignFormMetaReferenceDto > formBind = binder .forField (formCombo )
46- .withValidator (
47- campaignFormMetaReferenceDto -> campaignFormMetaReferenceDto != null && campaignFormMetaReferenceDto .getUuid () != null ,
48- I18nProperties .getValidationError (Validations .campaignDashboardDataFormValueNull ))
49- .withValidator (campaignFormMetaReferenceDto -> {
50- ArrayList <CampaignFormMetaReferenceDto > items = getItems ();
51- return !items .contains (campaignFormMetaReferenceDto );
52- }, I18nProperties .getValidationError (Validations .campaignDashboardDataFormValueDuplicate ))
53- .bind (campaignFormMetaReferenceDto -> campaignFormMetaReferenceDto , (bindedCampaignFormMeta , selectedCampaignFormMeta ) -> {
54- bindedCampaignFormMeta .setUuid (selectedCampaignFormMeta .getUuid ());
55- bindedCampaignFormMeta .setCaption (selectedCampaignFormMeta .getCaption ());
56- });
50+ .withValidator (
51+ campaignFormMetaReferenceDto -> campaignFormMetaReferenceDto != null && campaignFormMetaReferenceDto .getUuid () != null ,
52+ I18nProperties .getValidationError (Validations .campaignDashboardDataFormValueNull ))
53+ .withValidator (campaignFormMetaReferenceDto -> {
54+ ArrayList <CampaignFormMetaReferenceDto > items = getItems ();
55+ return !items .contains (campaignFormMetaReferenceDto );
56+ }, I18nProperties .getValidationError (Validations .campaignDashboardDataFormValueDuplicate ))
57+ .bind (campaignFormMetaReferenceDto -> new CampaignFormMetaReferenceDto (campaignFormMetaReferenceDto .getUuid (), campaignFormMetaReferenceDto .getCaption ()),
58+ (bindedCampaignFormMeta , selectedCampaignFormMeta ) -> {
59+ bindedCampaignFormMeta .setUuid (selectedCampaignFormMeta .getUuid ());
60+ bindedCampaignFormMeta .setCaption (selectedCampaignFormMeta .getCaption ());
61+ // workarround: grid doesn't refresh itself for unknown reason
62+ grid .getDataProvider ().refreshAll ();
63+ });
5764 formCombo .setEmptySelectionAllowed (false );
65+
5866 Grid .Column <CampaignFormMetaReferenceDto , String > formColumn =
59- grid .addColumn (campaignFormMetaReferenceDto -> campaignFormMetaReferenceDto .getCaption ())
60- .setCaption (I18nProperties .getString (Strings .entityCampaignDataForm ));
67+ grid .addColumn (ReferenceDto ::getCaption )
68+ .setCaption (I18nProperties .getString (Strings .entityCampaignDataForm ));
69+
6170 formColumn .setEditorBinding (formBind );
6271 return binder ;
6372 }
0 commit comments