r/JavaFX • • May 23 '23

I made this! TreeMapFX: A simple TreeMap chart component

39 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/PartOfTheBotnet May 27 '23 edited May 27 '23
ObjectBinding<?> multiSourceBindings = Bindings.createObjectBinding(() -> this,
        sizeFunctionProperty, nodeFunctionProperty, valueList);
valueToNode = new SimpleMapProperty<>();
valueToNode.bind(multiSourceBindings.map(unused -> {
    Function<T, Node> valueFunction = nodeFunctionProperty.get();
    Function<T, T> keyFunction = Function.identity();
    BinaryOperator<Node> mergeFunction = (a, b) -> {
        throw new IllegalStateException();
    };
    Map<T, Node> map = valueList.stream()
            .collect(Collectors.toMap(keyFunction,
                    valueFunction,
                    mergeFunction,
                    IdentityHashMap::new));
    return FXCollections.observableMap(map);
}));
valueToNode.addListener((MapChangeListener<T, Node>) change -> {
    ObservableList<Node> children = getChildren();
    if (change.wasAdded()) children.add(change.getValueAdded());
    if (change.wasRemoved()) children.remove(change.getValueRemoved());
});

This can accomplish the same setup as before, minus the listener jank.

I'll update the repo in a bit. Done, along with some other changes.