|
| 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 | +} |
0 commit comments