The JavaDoc for the ListChangeListener provides a template for handling changes. I don't know how to handle permutations, however. For every index, I can find out where the new index of the item is, but I don't know what to do with it. This is a bit of a puzzle that is independent of programming language. An ObservableList can only add(), remove(), set(), and also has an iterator.
If I have an original list [1,2,3], and bind a list[] to it, the bound list[1,2,3] needs to match it. If the original list gets its comparator swapped so that the original list now reads [3,2,1], how do I make the bound list follow along?
/**
* Binds a source list's elements to a destination list. Any changes made in
* the source list will reflect in the destination list.
*
* @param <SRC> The source list's object type.
* @param <DEST> The destination list's object type.
* @param dest The destination list that will be bound to the src list.
* @param src The source list to watch for changes, and propagate up to the
* destination list.
* @param transformer A function that will transform a source list data
* type, A, into a destination list data type, B.
*/
public static <SRC, DEST> void bindLists(
ObservableList<DEST> dest, ObservableList<SRC> src, Function<? super SRC, ? extends DEST> transformer) {
/*Add the initial data into the destination list.*/
for (SRC a : src) {
dest.add(transformer.apply(a));
}
/*Watch for future data to add to the destination list. Also watch for removal
of data form the source list to remove its respective item in the destination
list.*/
src.addListener((ListChangeListener.Change<? extends SRC> c) -> {
while (c.next()) {
if (c.wasPermutated()) {
/*How do you handle permutations? Do you remove and then add,
or add and then remove, or use set, or use a copy arraylist
and set the right indices? Removing/adding causes concurrent modifications.*/
for (int oldIndex = c.getFrom(); oldIndex < c.getTo(); oldIndex++) {
int newIndex = c.getPermutation(oldIndex);
dest.remove(oldIndex);
dest.add(newIndex, dest.get(oldIndex));
}
} else if (c.wasUpdated()) {
} else {
/*Respond to removed data.*/
for (SRC item : c.getRemoved()) {
int from = c.getFrom();
dest.remove(from);
}
/*Respond to added data.*/
for (SRC item : c.getAddedSubList()) {
int indexAdded = src.indexOf(item);
dest.add(indexAdded, transformer.apply(item));
}
}
}
});
}