Skip to content

Commit 511c975

Browse files
author
nicolaiparlog
committed
Merge branch 'feature/nesting_missing' into develop
2 parents bd7bea5 + fbbcfdd commit 511c975

File tree

46 files changed

+1308
-749
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1308
-749
lines changed

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

Lines changed: 43 additions & 6 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,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()

src/main/java/org/codefx/libfx/nesting/Nesting.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
/**
1010
* <p>
11-
* A nesting encapsulates a hierarchy of nested {@link ObservableValue ObservableValues} and its
12-
* {@link #innerObservableProperty() innerObservable} property always contains the current innermost {@code Observable}
13-
* in that hierarchy as an {@link Optional}. A {@code Nesting} can be used as a basic building block for other nested
14-
* functionality.
11+
* A nesting encapsulates a hierarchy of nested {@link ObservableValue ObservableValues}.
12+
* <p>
13+
* Its {@link #innerObservableProperty() innerObservable} property always contains the current innermost
14+
* {@code Observable} in that hierarchy as an {@link Optional}. A {@code Nesting} can be used as a basic building block
15+
* for other nested functionality.
1516
* <h2>Nesting Hierarchy</h2> A nesting hierarchy is composed of some {@code ObservableValues}, often simply called
1617
* <b>observables</b>, and <b>nesting steps</b> which lead from one observable to the next.
1718
* <p>
@@ -24,7 +25,7 @@
2425
* Hence they must all implement {@link ObservableValue ObservableValue}. No step is used on the inner observable so it
2526
* suffices that it implements {@link Observable}.
2627
* <h3>Example</h3> Consider a class {@code Employee} which has an {@code Property<Address> address}, where
27-
* {@code Address} has a {@code StringProperty streetName}. There might be a {@code Property<Emplyee> currentEmployee},
28+
* {@code Address} has a {@code StringProperty streetName}. There might be a {@code Property<Employee> currentEmployee},
2829
* which always holds the current employee.
2930
* <p>
3031
* In this case the hierarchy would be {@code currentEmployee -> address -> streetName} where {@code currentEmployee} is

0 commit comments

Comments
 (0)