/*
 * Copyright (c) 2006-2015 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.components;

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

import javax.swing.AbstractListModel;

/**
 * Generic list model, closely following the DefaultListModel API.
 *
 * @param <T> Generic type of the list
 */
public class GenericListModel<T> extends AbstractListModel<T> {

    /** A version number for this class. */
    private static final long serialVersionUID = -4227892376992714545L;
    /** Data stored in list. */
    private final List<T> list;

    /**
     * Instantiates an empty generic list model.
     */
    public GenericListModel() {

        this.list = Collections.synchronizedList(new ArrayList<>());
    }

    /**
     * Instantiates a list model containing the specified list items.
     *
     * @param list Data to be included in the model
     */
    public GenericListModel(final Collection<T> list) {

        this.list = Collections.synchronizedList(new ArrayList<>(list));
    }

    @Override
    public int getSize() {
        return list.size();
    }

    /**
     * Returns the specified index in the list.
     *
     * @param index Index of the item to return
     *
     * @return Item at index
     */
    public T get(final int index) {
        return list.get(index);
    }

    /**
     * Checks whether the list mode is empty.
     *
     * @return true iif the is the list if empty
     */
    public boolean isEmpty() {
        return list.isEmpty();
    }

    /**
     * Checks whether this list model contains the specified object.
     *
     * @param object Object to check for in the model
     *
     * @return true iif the model object is in this model
     */
    public boolean contains(final T object) {
        return list.contains(object);
    }

    /**
     * Checks the first index of the specified option.
     *
     * @param object Object to check the index of
     *
     * @return first index of the object or -1 if not found
     */
    public int indexOf(final T object) {
        return list.indexOf(object);
    }

    /**
     * Checks the last index of the specified option.
     *
     * @param object Object to check the index of
     *
     * @return last index of the object or -1 if not found
     */
    public int lastIndexOf(final T object) {
        return list.lastIndexOf(object);
    }

    /**
     * Sets the object at the specified index to be the specified object.
     *
     * @param index  Index of the object
     * @param object Object to set
     */
    public void set(final int index, final T object) {
        list.set(index, object);
        fireContentsChanged(this, index, index);
    }

    /**
     * Removes the object at the specified index.
     *
     * @param index Index to remove
     */
    public void remove(final int index) {
        list.remove(index);
        fireIntervalRemoved(this, index, index);
    }

    /**
     * Adds the specified object at the specified index.
     *
     * @param index  Index to insert object at
     * @param object Object to insert
     */
    public void add(final int index, final T object) {
        list.add(index, object);
        fireIntervalAdded(this, index, index);
    }

    /**
     * Adds the specified object.
     *
     * @param object Object to add
     */
    public void add(final T object) {
        final int index = list.size();
        list.add(object);
        fireIntervalAdded(this, index, index);
    }

    /**
     * Removes the specified object.
     *
     * @param obj Object to remove
     *
     * @return true iif object was removed
     */
    public boolean remove(final T obj) {
        final int index = indexOf(obj);
        final boolean success = list.remove(obj);
        if (index >= 0) {
            fireIntervalRemoved(this, index, index);
        }
        return success;
    }

    /**
     * Replaces the element at the specified index with a new object.
     *
     * @param oldValue Old value
     * @param newValue New value
     */
    public void replace(final T oldValue, final T newValue) {
        final int index = list.indexOf(oldValue);
        list.set(index, newValue);
        fireContentsChanged(this, index, index);
    }

    /**
     * Clears this model of all items.
     */
    public void clear() {
        final int lastIndex = list.size() - 1;
        list.clear();
        if (lastIndex >= 0) {
            fireIntervalRemoved(this, 0, lastIndex);
        }
    }

    @Override
    public String toString() {
        return list.toString();
    }

    /**
     * Removes the specified range of items from the list.
     *
     * @param start Index to start removing
     * @param end   Index to stop removing
     */
    public void removeRange(final int start, final int end) {
        if (start > end) {
            throw new IllegalArgumentException("start must be greater than "
                    + "or equal to end");
        }
        for (int i = end; i >= start; i--) {
            list.remove(i);
        }
        fireIntervalRemoved(this, start, end);
    }

    /**
     * Returns a list of items in this model
     *
     * @return List available in this model
     */
    public List<T> elements() {
        return Collections.unmodifiableList(list);
    }

    /**
     * Adds all the objects in the specified collection to this list.
     *
     * @param collection Collection to add
     */
    public void addAll(final Collection<T> collection) {
        list.addAll(collection);
    }

    /**
     * Adds all the objects in the specified collection to this list at the specified index.
     *
     * @param index      Index to add the items
     * @param collection Collection to add
     */
    public void addAll(final int index, final Collection<T> collection) {
        list.addAll(index, collection);
    }

    @Override
    public T getElementAt(final int index) {
        return get(index);
    }

}

@@ -6630,30 +6630,160 @@
     
-list.addAll(collection
+final int lastIndex = list.size() - 1;%0A        list.addAll(collection);%0A        fireIntervalAdded(this, lastIndex %3C 0 ? 0 : lastIndex, collection.size()
 );%0A 
@@ -7105,24 +7105,83 @@
 ollection);%0A
+        fireIntervalAdded(this, index, collection.size());%0A
     %7D%0A%0A    @

Add events to GenericListModel addAll