Search in sources :

Example 1 with ListChangeListener

use of com.canoo.platform.remoting.ListChangeListener in project dolphin-platform by canoo.

the class ObservableArrayList method subList.

@Override
public List<E> subList(final int fromIndex, final int toIndex) {
    final ObservableArrayList subList = new ObservableArrayList(list.subList(fromIndex, toIndex));
    subList.onChanged(new ListChangeListener() {

        @Override
        public void listChanged(ListChangeEvent evt) {
            final List<ListChangeEvent.Change<E>> changes = Assert.requireNonNull(evt, "evt").getChanges();
            for (final ListChangeEvent.Change<E> change : changes) {
                if (change.isAdded()) {
                    final int fromIndex = list.indexOf(evt.getSource().get(change.getFrom() - 1)) + 1;
                    list.addAll(fromIndex, subList.subList(change.getFrom(), change.getTo()));
                } else if (change.isReplaced()) {
                    if (list.contains(change.getRemovedElements().get(0))) {
                        final int index = list.indexOf(change.getRemovedElements().get(0));
                        list.set(index, (E) subList.get(change.getFrom()));
                    }
                } else if (change.isRemoved()) {
                    list.removeAll(change.getRemovedElements());
                }
            }
        }
    });
    onChanged(new ListChangeListener<E>() {

        @Override
        public void listChanged(ListChangeEvent<? extends E> evt) {
            final List<? extends ListChangeEvent.Change<? extends E>> changes = Assert.requireNonNull(evt, "evt").getChanges();
            for (final ListChangeEvent.Change<? extends E> change : changes) {
                if (change.isAdded()) {
                // TODO Add the element to sublist when adding element in base list
                } else if (change.isReplaced()) {
                    if (subList.contains(change.getRemovedElements().get(0))) {
                        final int index = subList.indexOf(change.getRemovedElements().get(0));
                        subList.set(index, (E) list.get(change.getFrom()));
                    }
                } else if (change.isRemoved()) {
                    subList.removeAll(change.getRemovedElements());
                }
            }
        }
    });
    return subList;
}
Also used : ListChangeEvent(com.canoo.platform.remoting.ListChangeEvent) ObservableList(com.canoo.platform.remoting.ObservableList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ListChangeListener(com.canoo.platform.remoting.ListChangeListener)

Aggregations

ListChangeEvent (com.canoo.platform.remoting.ListChangeEvent)1 ListChangeListener (com.canoo.platform.remoting.ListChangeListener)1 ObservableList (com.canoo.platform.remoting.ObservableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1