Search in sources :

Example 1 with ListChangeEvent

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

the class ListMapperImpl method processEvent.

@Override
public void processEvent(final PropertyInfo observableListInfo, final String sourceId, final ListChangeEvent<?> event) {
    final String attributeName = observableListInfo.getAttributeName();
    for (final ListChangeEvent.Change<?> change : event.getChanges()) {
        final int from = change.getFrom();
        final int to = from + change.getRemovedElements().size();
        final List<?> newElements = event.getSource().subList(from, change.getTo());
        final int count = newElements.size();
        final PresentationModelBuilder builder = builderFactory.createBuilder();
        builder.withType(PlatformRemotingConstants.LIST_SPLICE).withAttribute("source", sourceId).withAttribute("attribute", attributeName).withAttribute("from", from).withAttribute("to", to).withAttribute("count", count);
        int i = 0;
        for (final Object current : newElements) {
            try {
                builder.withAttribute(Integer.toString(i++), observableListInfo.convertToDolphin(current));
            } catch (Exception e) {
                throw new MappingException("Error in event processing!", e);
            }
        }
        builder.create();
    }
}
Also used : ListChangeEvent(com.canoo.platform.remoting.ListChangeEvent) PresentationModelBuilder(com.canoo.dp.impl.remoting.PresentationModelBuilder) MappingException(com.canoo.dp.impl.remoting.MappingException) MappingException(com.canoo.dp.impl.remoting.MappingException)

Example 2 with ListChangeEvent

use of com.canoo.platform.remoting.ListChangeEvent 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)2 MappingException (com.canoo.dp.impl.remoting.MappingException)1 PresentationModelBuilder (com.canoo.dp.impl.remoting.PresentationModelBuilder)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