Skip to content

Commit fbbcfdd

Browse files
author
nicolaiparlog
committed
Fix 'NestedDemo' to show default and customized behavior
1 parent e9a3677 commit fbbcfdd

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

src/demo/java/org/codefx/libfx/nesting/NestedDemo.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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,30 +282,61 @@ 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+
private void nestedPropertyBindingWithMissingInnerObservableAndDefaultBehavior() {
288+
print("NESTED PROPERTY BINDING WHEN INNER OBSERVABLE IS MISSING (DEFAULT)");
289+
currentEmployee.getValue().addressProperty().getValue().streetNameProperty().set("Some Street");
287290

288-
/*
289-
* TODO update this example and include new ones
290-
*/
291+
// create a nested property for the current employee's street name
292+
NestedStringProperty currentEmployeesStreetName = Nestings.on(currentEmployee)
293+
.nest(Employee::addressProperty)
294+
.nestStringProperty(Address::streetNameProperty)
295+
.buildProperty();
296+
297+
print("Nested property's initial street name: \"" + currentEmployeesStreetName.get() + "\"");
298+
299+
currentEmployee.getValue().addressProperty().setValue(null);
300+
print("The inner observable is now missing (is present: \""
301+
+ currentEmployeesStreetName.isInnerObservablePresent() + "\")");
302+
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+
}
291311

292-
print("NESTED PROPERTY BINDING WHEN INNER OBSERVABLE IS MISSING");
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)");
293321

294322
// create a nested property for the current employee's street name
295323
NestedStringProperty currentEmployeesStreetName = Nestings.on(currentEmployee)
296324
.nest(Employee::addressProperty)
297325
.nestStringProperty(Address::streetNameProperty)
298-
.buildProperty();
326+
.buildPropertyWithBuilder()
327+
.onInnerObservableMissingSetValue("Null street")
328+
.onUpdateWhenInnerObservableMissingAcceptValues()
329+
.build();
299330

300331
print("Nested property's initial street name: \"" + currentEmployeesStreetName.get() + "\"");
301332

302333
currentEmployee.getValue().addressProperty().setValue(null);
303334
print("The inner observable is now missing (is present: \""
304335
+ currentEmployeesStreetName.isInnerObservablePresent() + "\")");
336+
print("The street name changed to the specified value: \"" + currentEmployeesStreetName.get() + "\"");
305337

306-
currentEmployeesStreetName.set("Null Street");
307-
print("The nested property can still be changed: \"" + currentEmployeesStreetName.get() + "\"");
338+
currentEmployeesStreetName.set("Another Street");
339+
print("The nested property can be changed: \"" + currentEmployeesStreetName.get() + "\"");
308340

309341
currentEmployee.getValue().addressProperty().setValue(new Employee.Address("New Street"));
310342
print("When a new inner observable is present (\"" + currentEmployeesStreetName.isInnerObservablePresent()

0 commit comments

Comments
 (0)