Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MappedBackedList when the change is wasUpdated #5

Merged
merged 5 commits into from
Jul 29, 2024

Conversation

LoayGhreeb
Copy link

@LoayGhreeb LoayGhreeb commented Jul 28, 2024

This PR addresses an issue related to EasyBind#mapBacked. The problem arises when using a SortedList with elements that are mapped using EasyBind#mapBacked. The updates to elements in the list do not propagate to the SortedList.

Minimal Example:

import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;

import com.tobiasdiez.easybind.EasyBind;

public class Main {
    public static void main(String[] args) {
        ObservableList<IntegerProperty> list = FXCollections.observableArrayList(
                number -> new Observable[] {number}
        );
        ObservableList<Integer> mappedList = EasyBind.mapBacked(list, IntegerProperty::get);
        SortedList<Integer> sortedList = new SortedList<>(mappedList);

        IntegerProperty number = new SimpleIntegerProperty(1);
        list.add(number);

        System.out.println("mappedList: " + mappedList);
        System.out.println("sortedList: " + sortedList);

        number.set(2);

        System.out.println("mappedList: " + mappedList);
        System.out.println("sortedList: " + sortedList);
    }
}

Output:

mappedList: [1]
sortedList: [1]
mappedList: [2]
sortedList: [1]

Expected Output:

mappedList: [1]
sortedList: [1]
mappedList: [2]
sortedList: [2]

The issue is due to using nextUpdate after changing the object. Calling nextUpdate, the listeners expect that changes are made to the object's properties, not to the object itself. If the object itself changes, nextSet should be called instead.

From the FXCollections#observableArrayList(Callback<E, Observable[]>) documentation:

Creates a new empty javafx.collections.FXCollections#observableArrayList(javafx.util.Callback<E,javafx.beans.Observable[]>) that is backed by an array list and listens to changes in observables of its items.
These observables are listened for changes and the user is notified of these through an ListChangeListener.Change#wasUpdated() change of an attached ListChangeListener. These changes are unrelated to the changes made to the observable list itself using methods such as add and remove.
For example, a list of Shapes can listen to changes in the shapes' fill property.


  • Added mapOnUpdate option to mapBacked: creating a new object on update may not always be needed, especially if the object has bindings that already reflect changes in the mapped object.

@calixtus
Copy link
Member

@Siedlerchr @koppor Decision needed here, merge?

@calixtus calixtus mentioned this pull request Jul 29, 2024
6 tasks
@Siedlerchr Siedlerchr merged commit 9320783 into JabRef:main Jul 29, 2024
4 checks passed
@Siedlerchr
Copy link
Member

We need to update the Coordinates in jabref build gradle to point to this dep. Ideally we can do a proper release version as well

@LoayGhreeb LoayGhreeb deleted the FixMappedBackedList branch July 29, 2024 21:53
@koppor
Copy link
Member

koppor commented Aug 5, 2024

Is it possible to add a CHANGELOG.md entry @LoayGhreeb

@LoayGhreeb LoayGhreeb mentioned this pull request Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants