All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.collections.event.ObservableEventList Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.collections.event;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.sf.javagimmicks.collections.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 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 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