net.sf.javagimmicks.collections8.event.ObservableEventList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gimmicks Show documentation
Show all versions of gimmicks Show documentation
Utility classes, APIs and tools for Java
package net.sf.javagimmicks.collections8.event;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import net.sf.javagimmicks.collections8.event.ListEvent.Type;
import net.sf.javagimmicks.event.EventListener;
import net.sf.javagimmicks.event.Observable;
import net.sf.javagimmicks.event.ObservableBase;
/**
* A {@link List} decorator that serves as an {@link Observable} for
* {@link ListEvent}s.
*/
public class ObservableEventList extends AbstractEventList implements
Observable>
{
private static final long serialVersionUID = -6317396247733734848L;
protected final ObservableBase> _helper = new ObservableBase>();
/**
* Wraps a new {@link ObservableEventList} around a given {@link List}.
*
* @param decorated
* the {@link List} to wrap around
*/
public ObservableEventList(final List decorated)
{
super(decorated);
}
@Override
public >> void addEventListener(final L listener)
{
_helper.addEventListener(listener);
}
@Override
public >> void removeEventListener(final L listener)
{
_helper.removeEventListener(listener);
}
@Override
public ObservableEventList subList(final int fromIndex, final int toIndex)
{
return new ObservableEventSubList(this, fromIndex, toIndex);
}
@Override
protected void fireElementsAdded(final int index, final Collection extends E> elements)
{
final ListEvent event = new ListEventImpl(
Type.ADDED,
index,
index + elements.size(),
Collections.unmodifiableList(new ArrayList(elements)));
_helper.fireEvent(event);
}
@Override
protected void fireElementUpdated(final int index, final E element, final E newElement)
{
final ListEvent event = new ListEventImpl(
Type.UPDATED,
index,
index,
Collections.singletonList(element),
Collections.singletonList(newElement));
_helper.fireEvent(event);
}
@Override
protected void fireElementRemoved(final int index, final E element)
{
final ListEvent event = new ListEventImpl(
Type.REMOVED,
index,
index,
Collections.singletonList(element));
_helper.fireEvent(event);
}
private class ListEventImpl implements ListEvent
{
protected final Type _type;
protected final int _fromIndex;
protected final int _toIndex;
protected final List _elements;
protected final List _newElements;
public ListEventImpl(final Type type, final int fromIndex, final int toIndex, final List element,
final List newElement)
{
_type = type;
_fromIndex = fromIndex;
_toIndex = toIndex;
_elements = element;
_newElements = newElement;
}
public ListEventImpl(final Type type, final int fromIndex, final int toIndex, final List element)
{
this(type, fromIndex, toIndex, element, null);
}
@Override
public Type getType()
{
return _type;
}
@Override
public int getFromIndex()
{
return _fromIndex;
}
@Override
public int getToIndex()
{
return _toIndex;
}
@Override
public List getElements()
{
return _elements;
}
@Override
public List getNewElements()
{
return _newElements;
}
@Override
public ObservableEventList getSource()
{
return ObservableEventList.this;
}
}
protected static class ObservableEventSubList extends ObservableEventList
{
private static final long serialVersionUID = 1996968483016862598L;
protected final ObservableEventList _parent;
protected final int _offset;
protected ObservableEventSubList(final ObservableEventList parent, final int fromIndex, final int toIndex)
{
super(parent._decorated.subList(fromIndex, toIndex));
_parent = parent;
_offset = fromIndex;
}
@Override
protected void fireElementsAdded(final int index, final Collection extends E> elements)
{
super.fireElementsAdded(index, elements);
_parent.fireElementsAdded(index + _offset, elements);
}
@Override
protected void fireElementRemoved(final int index, final E element)
{
super.fireElementRemoved(index, element);
_parent.fireElementRemoved(index + _offset, element);
}
@Override
protected void fireElementUpdated(final int index, final E element, final E newElement)
{
super.fireElementUpdated(index, element, newElement);
_parent.fireElementUpdated(index + _offset, element, newElement);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy