@@ -50,7 +50,8 @@ public static void main(String[] args) {
50
50
demo .nestedPropertyCreation ();
51
51
demo .nestedPropertyCreationWithBuilder ();
52
52
demo .nestedPropertyBinding ();
53
- demo .nestedPropertyBindingWithMissingInnerObservable ();
53
+ demo .nestedPropertyBindingWithMissingInnerObservableAndDefaultBehavior ();
54
+ demo .nestedPropertyBindingWithMissingInnerObservableAndCustomizedBehavior ();
54
55
demo .additionalNestedFeatures ();
55
56
}
56
57
@@ -281,10 +282,11 @@ private void nestedPropertyBinding() {
281
282
}
282
283
283
284
/**
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.
285
286
*/
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" );
288
290
289
291
// create a nested property for the current employee's street name
290
292
NestedStringProperty currentEmployeesStreetName = Nestings .on (currentEmployee )
@@ -298,8 +300,43 @@ private void nestedPropertyBindingWithMissingInnerObservable() {
298
300
print ("The inner observable is now missing (is present: \" "
299
301
+ currentEmployeesStreetName .isInnerObservablePresent () + "\" )" );
300
302
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 () + "\" " );
303
340
304
341
currentEmployee .getValue ().addressProperty ().setValue (new Employee .Address ("New Street" ));
305
342
print ("When a new inner observable is present (\" " + currentEmployeesStreetName .isInnerObservablePresent ()
0 commit comments