@@ -204,6 +204,8 @@ public class PersonEditForm extends AbstractEditForm<PersonDto> {
204
204
private TextField nameOfGuardians ;
205
205
private long minimumAdultAge ;
206
206
private long minimumEmancipatedAge ;
207
+ private TextField approximateAgeField ;
208
+ private ComboBox approximateAgeTypeField ;
207
209
//@formatter:on
208
210
209
211
public PersonEditForm (
@@ -336,10 +338,10 @@ protected void addFields() {
336
338
.validateBirthDate ((Integer ) e , (Integer ) birthDateMonth .getValue (), (Integer ) birthDateDay .getValue ()));
337
339
338
340
DateField deathDate = addField (PersonDto .DEATH_DATE , DateField .class );
339
- TextField approximateAgeField = addField (PersonDto .APPROXIMATE_AGE , TextField .class );
341
+ approximateAgeField = addField (PersonDto .APPROXIMATE_AGE , TextField .class );
340
342
approximateAgeField
341
343
.setConversionError (I18nProperties .getValidationError (Validations .onlyIntegerNumbersAllowed , approximateAgeField .getCaption ()));
342
- ComboBox approximateAgeTypeField = addField (PersonDto .APPROXIMATE_AGE_TYPE , ComboBox .class );
344
+ approximateAgeTypeField = addField (PersonDto .APPROXIMATE_AGE_TYPE , ComboBox .class );
343
345
addField (PersonDto .APPROXIMATE_AGE_REFERENCE_DATE , DateField .class );
344
346
345
347
approximateAgeField .addValidator (
@@ -348,6 +350,14 @@ protected void addFields() {
348
350
approximateAgeTypeField ,
349
351
I18nProperties .getValidationError (Validations .softApproximateAgeTooHigh )));
350
352
353
+ addFieldListeners (PersonDto .APPROXIMATE_AGE , e -> {
354
+ updateLegalGuardianSection (false );
355
+ });
356
+
357
+ addFieldListeners (PersonDto .APPROXIMATE_AGE_TYPE , e -> {
358
+ updateLegalGuardianSection (false );
359
+ });
360
+
351
361
TextField tfGestationAgeAtBirth = addField (PersonDto .GESTATION_AGE_AT_BIRTH , TextField .class );
352
362
tfGestationAgeAtBirth
353
363
.setConversionError (I18nProperties .getValidationError (Validations .onlyIntegerNumbersAllowed , tfGestationAgeAtBirth .getCaption ()));
@@ -519,7 +529,7 @@ protected void addFields() {
519
529
addFieldListeners (PersonDto .BIRTH_DATE_YYYY , e -> {
520
530
updateApproximateAge ();
521
531
updateReadyOnlyApproximateAge ();
522
- updateIsIncapacitatedCheckBox (false );
532
+ updateLegalGuardianSection (false );
523
533
});
524
534
525
535
addFieldListeners (PersonDto .DEATH_DATE , e -> updateApproximateAge ());
@@ -651,12 +661,15 @@ protected void addFields() {
651
661
hasGuardian = addCustomField (HAS_GUARDIAN , Boolean .class , CheckBox .class );
652
662
653
663
isIncapacitated = addField (PersonDto .IS_INCAPACITATED , CheckBox .class );
654
- isIncapacitated .addValueChangeListener (e -> updateIsIncapacitatedCheckBox (true ));
664
+ isIncapacitated .addValueChangeListener (e -> updateLegalGuardianSection (true ));
655
665
656
666
isEmancipated = addField (PersonDto .IS_EMANCIPATED , CheckBox .class );
657
667
isEmancipated .addValueChangeListener (e -> onEmancipatedChange ());
668
+ isEmancipated .setVisible (false );
658
669
hasGuardian .setEnabled (false );
659
- minimumAdultAge = FacadeProvider .getConfigFacade ().getMinimumAdultAge ();//2 places
670
+ hasGuardian .setValue (Boolean .TRUE );
671
+ nameOfGuardians .setVisible (true );
672
+ minimumAdultAge = FacadeProvider .getConfigFacade ().getMinimumAdultAge ();
660
673
minimumEmancipatedAge = FacadeProvider .getConfigFacade ().getMinimumEmancipatedAge ();
661
674
}
662
675
@@ -678,38 +691,54 @@ private void onEmancipatedChange() {
678
691
if (isEmancipatedChecked ) {
679
692
nameOfGuardians .setValue ("" );
680
693
}
681
- updateHasGuardianCheckBox ();
694
+ updateHasGuardianCheckBox (isEmancipatedChecked );
682
695
}
683
696
684
- private void updateIsIncapacitatedCheckBox (boolean isInitialized ) {
697
+ private void updateLegalGuardianSection (boolean isInitialized ) {
685
698
boolean isIncapacitatedChecked = (isIncapacitated == null ) || (isIncapacitated .getValue ());
699
+ boolean isEmancipatedChecked = (isEmancipated != null ) && (isEmancipated .getValue ());
686
700
int approximateAge = getApproximateAgeInYears ();
687
- boolean canBeEmancipated ;
688
- if (approximateAge == -1 ) {
689
- canBeEmancipated = false ;
690
- } else {
691
- canBeEmancipated = approximateAge >= minimumEmancipatedAge && approximateAge < minimumAdultAge ;
692
- }
701
+ boolean canBeEmancipated = personCanBeEmancipated (approximateAge , isEmancipatedChecked );
693
702
isEmancipated .setVisible (!isIncapacitatedChecked && canBeEmancipated );
694
703
hasGuardian .setValue (isIncapacitatedChecked || approximateAge < minimumAdultAge );
695
704
if (getApproximateAgeInYears () < minimumAdultAge ) {
696
705
nameOfGuardians .setValue (getValue ().getNamesOfGuardians ());
697
706
}
698
- updateHasGuardianCheckBox ();
707
+ updateHasGuardianCheckBox (false );
699
708
hardResetNameOfGuardians (isInitialized );
700
709
}
701
710
711
+ private boolean personCanBeEmancipated (int approximateAge , boolean change ) {
712
+ boolean canBeEmancipated ;
713
+ if (approximateAge == -1 && (approximateAgeField ).getValue () != null ) {
714
+ canBeEmancipated = false ;
715
+ } else {
716
+ canBeEmancipated = approximateAge >= minimumEmancipatedAge && approximateAge < minimumAdultAge ;
717
+ }
718
+ if (!canBeEmancipated && (approximateAgeField ).getValue () != null ){
719
+ int age = Integer .parseInt (approximateAgeField .getValue ());
720
+ if (approximateAgeTypeField .getValue () == ApproximateAgeType .YEARS ){
721
+ canBeEmancipated = age >= minimumEmancipatedAge && age < minimumAdultAge ;
722
+ if (change ) {
723
+ isEmancipated .setValue (canBeEmancipated );
724
+ }
725
+ }
726
+ }
727
+ return canBeEmancipated ;
728
+ }
729
+
702
730
private void hardResetNameOfGuardians (boolean isInitialized ) {
703
731
boolean isIncapacitatedChecked = (isIncapacitated != null ) && (isIncapacitated .getValue ());
704
732
if (getApproximateAgeInYears () != -1 && getApproximateAgeInYears () >= minimumAdultAge && !isIncapacitatedChecked && isInitialized ) {
705
733
nameOfGuardians .setValue ("" );
706
734
}
707
735
}
708
736
709
- private void updateHasGuardianCheckBox () {
737
+ private void updateHasGuardianCheckBox (boolean onEmancipatedChange ) {
710
738
boolean isIncapacitatedChecked = (isIncapacitated != null ) && (isIncapacitated .getValue ());
711
739
boolean isEmancipatedChecked = (isEmancipated != null ) && (isEmancipated .getValue ());
712
- if ((getApproximateAgeInYears () >= minimumAdultAge || (getApproximateAgeInYears () < minimumEmancipatedAge ) && isEmancipatedChecked )) {
740
+ boolean canBe = personCanBeEmancipated (getApproximateAgeInYears (), onEmancipatedChange );
741
+ if ((!canBe || isIncapacitatedChecked ) && isEmancipatedChecked ) {
713
742
isEmancipatedChecked = false ;
714
743
isEmancipated .setValue (Boolean .FALSE );
715
744
isIncapacitated .setVisible (true );
0 commit comments