@@ -50,7 +50,8 @@ public static void main(String[] args) {
5050 demo .nestedPropertyCreation ();
5151 demo .nestedPropertyCreationWithBuilder ();
5252 demo .nestedPropertyBinding ();
53- demo .nestedPropertyBindingWithMissingInnerObservable ();
53+ demo .nestedPropertyBindingWithMissingInnerObservableAndDefaultBehavior ();
54+ demo .nestedPropertyBindingWithMissingInnerObservableAndCustomizedBehavior ();
5455 demo .additionalNestedFeatures ();
5556 }
5657
@@ -281,10 +282,11 @@ private void nestedPropertyBinding() {
281282 }
282283
283284 /**
284- * Demonstrates how a {@link NestedProperty} behaves when the inner observable is missing.
285+ * Demonstrates how a {@link NestedProperty} behaves by default when the inner observable is missing.
285286 */
286- private void nestedPropertyBindingWithMissingInnerObservable () {
287- print ("NESTED PROPERTY BINDING WHEN INNER OBSERVABLE IS MISSING" );
287+ private void nestedPropertyBindingWithMissingInnerObservableAndDefaultBehavior () {
288+ print ("NESTED PROPERTY BINDING WHEN INNER OBSERVABLE IS MISSING (DEFAULT)" );
289+ currentEmployee .getValue ().addressProperty ().getValue ().streetNameProperty ().set ("Some Street" );
288290
289291 // create a nested property for the current employee's street name
290292 NestedStringProperty currentEmployeesStreetName = Nestings .on (currentEmployee )
@@ -298,8 +300,43 @@ private void nestedPropertyBindingWithMissingInnerObservable() {
298300 print ("The inner observable is now missing (is present: \" "
299301 + currentEmployeesStreetName .isInnerObservablePresent () + "\" )" );
300302
301- currentEmployeesStreetName .set ("Null Street" );
302- print ("The nested property can still be changed: \" " + currentEmployeesStreetName .get () + "\" " );
303+ try {
304+ currentEmployeesStreetName .set ("Null Street" );
305+ print ("You should never see this on the console." );
306+ } catch (Exception ex ) {
307+ print ("By default, the nested property can not be changed: " + ex .getClass ());
308+ // reset the example to a proper state
309+ currentEmployee .getValue ().addressProperty ().setValue (new Employee .Address ("Some Street" ));
310+ }
311+
312+ print ();
313+ }
314+
315+ /**
316+ * Demonstrates how a {@link NestedProperty} can be configured to behave differently when the inner observable is
317+ * missing.
318+ */
319+ private void nestedPropertyBindingWithMissingInnerObservableAndCustomizedBehavior () {
320+ print ("NESTED PROPERTY BINDING WHEN INNER OBSERVABLE IS MISSING (CUSTOM)" );
321+
322+ // create a nested property for the current employee's street name
323+ NestedStringProperty currentEmployeesStreetName = Nestings .on (currentEmployee )
324+ .nest (Employee ::addressProperty )
325+ .nestStringProperty (Address ::streetNameProperty )
326+ .buildPropertyWithBuilder ()
327+ .onInnerObservableMissingSetValue ("Null street" )
328+ .onUpdateWhenInnerObservableMissingAcceptValues ()
329+ .build ();
330+
331+ print ("Nested property's initial street name: \" " + currentEmployeesStreetName .get () + "\" " );
332+
333+ currentEmployee .getValue ().addressProperty ().setValue (null );
334+ print ("The inner observable is now missing (is present: \" "
335+ + currentEmployeesStreetName .isInnerObservablePresent () + "\" )" );
336+ print ("The street name changed to the specified value: \" " + currentEmployeesStreetName .get () + "\" " );
337+
338+ currentEmployeesStreetName .set ("Another Street" );
339+ print ("The nested property can be changed: \" " + currentEmployeesStreetName .get () + "\" " );
303340
304341 currentEmployee .getValue ().addressProperty ().setValue (new Employee .Address ("New Street" ));
305342 print ("When a new inner observable is present (\" " + currentEmployeesStreetName .isInnerObservablePresent ()
0 commit comments