Skip to content

Commit eb4c0a2

Browse files
author
Nicolai Parlog
committed
Added a simple demo for nested properties.
1 parent b5318fe commit eb4c0a2

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="test"/>
5+
<classpathentry kind="src" path="demo"/>
56
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
67
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
78
<classpathentry kind="output" path="bin"/>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.codefx.nesting;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
import javafx.beans.property.Property;
5+
import javafx.beans.property.SimpleDoubleProperty;
6+
import javafx.beans.property.SimpleObjectProperty;
7+
8+
import org.codefx.nesting.property.NestedProperty;
9+
10+
/**
11+
* Demonstrates some features of the nesting API.
12+
*/
13+
public class NestingDemo {
14+
15+
// #region ATTRIBUTES
16+
17+
/**
18+
* The currently selected employee.
19+
*/
20+
private final Property<Employee> currentEmployee;
21+
22+
//#end ATTRIBUTES
23+
24+
// #region CONSTRUCTION & MAIN
25+
26+
/**
27+
* Creates a new demo.
28+
*/
29+
private NestingDemo() {
30+
this.currentEmployee = new SimpleObjectProperty<>(new Employee(54_000));
31+
}
32+
33+
/**
34+
* Runs this demo.
35+
*
36+
* @param args
37+
* command line arguments (will not be used)
38+
*/
39+
public static void main(String[] args) {
40+
NestingDemo demo = new NestingDemo();
41+
demo.demoNestedProperties();
42+
}
43+
44+
//#end CONSTRUCTION & MAIN
45+
46+
// #region DEMOS
47+
48+
/**
49+
* Demonstrates the use of {@link NestedProperty}.
50+
*/
51+
private void demoNestedProperties() {
52+
// TODO change this to DoubleProperty as soon as that is implemented
53+
Property<Number> currentEmployeesSalary = Nestings.on(currentEmployee)
54+
.nest(employee -> employee.salaryProperty())
55+
.buildProperty();
56+
57+
System.out.println("Salary - object hierarchy: " + currentEmployee.getValue().salaryProperty().getValue());
58+
System.out.println("Salary - nested property: " + currentEmployeesSalary.getValue());
59+
}
60+
61+
//#end DEMOS
62+
63+
// #region PRIVATE CLASSES
64+
65+
/**
66+
* A simple demo class which represents an employee.
67+
*/
68+
private static class Employee {
69+
70+
/**
71+
* The salary.
72+
*/
73+
private final DoubleProperty salary;
74+
75+
/**
76+
* Creates a new employee with the specified salary.
77+
*
78+
* @param initialSalary
79+
* the employee's initial salary
80+
*/
81+
private Employee(double initialSalary) {
82+
this.salary = new SimpleDoubleProperty(this, "salary", initialSalary);
83+
}
84+
85+
/**
86+
* The salary.
87+
*
88+
* @return the salary as a property
89+
*/
90+
public DoubleProperty salaryProperty() {
91+
return salary;
92+
}
93+
94+
}
95+
96+
//#end PRIVATE CLASSES
97+
98+
}

test/org/codefx/AllTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
@RunWith(Suite.class)
1212
@SuiteClasses({
13-
_AllNestingTests.class,
13+
_AllNestingTests.class,
1414
})
1515
public class AllTests {
1616
// no body needed

0 commit comments

Comments
 (0)