Skip to content

Commit 73c4cc2

Browse files
author
Nicolai Parlog
committed
Readme now explains what happens in the branch.
1 parent eb4c0a2 commit 73c4cc2

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
1-
codefx
2-
======
1+
# CodeFX - Nestings
2+
3+
This feature branch revolves around JavaFX properties. The core API provides awesome capabilities but one thing I frequently need is missing: the possibility to interact with properties which are hidden in a more complex object hierarchy.
4+
5+
Below you will find an example of the problem and its solution with some lines of code from **CodeFX**. But before we come to that I wanna shortly present this feature's idea.
6+
7+
## Idea
8+
9+
This branch develops a simple and fluent API to create nestings, where a `Nesting` represents a hierarchy like `Employee -> Address -> ZIP code`. The nesting would collapse the hierarchy to the innermost property (in this example `zipProperty`) and update itself whenever the employee or address instances change.
10+
11+
A nesting can than be used in several ways:
12+
* You can build a property which always holds the innermost value.
13+
* You can attach a change listener which is carried along as the innermost property changes.
14+
* You can create bindings which are not only updated when the innermost property's value changes but also when the property itself is replaced.
15+
16+
These further steps can also be achieved with the same fluent API without breaking one's stride. You can find an example below and more in the classes in the folder `demo/org/codefx/nesting`.
17+
18+
## Example
19+
20+
Let's see an example...
21+
22+
### The Situation
23+
24+
Say you have an object of type `Employee` and you're creating an `EmployeeEditor` for editing a single employee at a time. You will most likely have a model for your UI which has something like a `currentEmployeeProperty`.
25+
Now you might want to bind some properties of the controls you're using for your editor to the current employee's properties. For example you might have a slider and want to bind it to the employee's `salaryProperty`.
26+
27+
### The Problem
28+
29+
Up to now that's all straight forward. But what happens when the current employee is replaced by another? Of course you want your editor to be updated.
30+
31+
### The "Solutions"
32+
33+
You could use `Bindings.select` but it has some downsides. For one thing, it uses strings to identify the nested properties, which breaks down quickly under refactoring. Unfortunately you won't even get an exception when trying to access properties which aren't there anymore - your binding will just forever contain null. Another downside is the return type. It's just an `ObjectBinding` (or `DoubleBinding` or ...) which does not suffice in all use cases.
34+
35+
Another way is to explicitly listen to changes of the model's `currentEmployeeProperty` and update the binding accordingly. That's rather tedious and leads to a lot of the same code all over the place. And it gets even worse, when you're nesting deeper, e.g. binding to the current employee's address' ZIP code.
36+
37+
### The Solution
38+
39+
Use **CodeFX**! :)
40+
41+
``` Java
42+
Nestings.on(currentEmployee)
43+
.nestDouble(employee -> employee.salaryProperty())
44+
.bindBidirectional(slider.valueProperty());
45+
```
46+
47+
``` Java
48+
Nestings.on(currentEmployee)
49+
.nest(employee -> employee.addressProperty())
50+
.nest(address -> address.zipProperty())
51+
.addChangeListener(myListener);
52+
```
53+

0 commit comments

Comments
 (0)