com.jgoodies.binding.list
Class ArrayListModel

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList
              extended by com.jgoodies.binding.list.ArrayListModel
All Implemented Interfaces:
ObservableList, Serializable, Cloneable, Iterable, Collection, List, RandomAccess, ListModel

public final class ArrayListModel
extends ArrayList
implements ObservableList

Adds ListModel capabilities to its superclass ArrayList, i. e. allows to observe changes in the content and structure. Useful for lists that are bound to list views, for example JList, JComboBox and JTable.

Version:
$Revision: 1.2 $
Author:
Karsten Lentzsch
See Also:
LinkedListModel, Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ArrayListModel()
          Constructs an empty list with an initial capacity of ten.
ArrayListModel(Collection c)
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayListModel(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
 
Method Summary
 void add(int index, Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(Object o)
          Appends the specified element to the end of this list.
 boolean addAll(Collection c)
          Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator.
 boolean addAll(int index, Collection c)
          Inserts all of the elements in the specified Collection into this list, starting at the specified position.
 void addListDataListener(ListDataListener l)
          Adds a listener to the list that's notified each time a change to the data model occurs.
 void clear()
          Removes all of the elements from this list.
 void fireContentsChanged(int index)
          Notifies all registered ListDataListeners that the element at the specified index has changed.
 Object getElementAt(int index)
          Returns the value at the specified index.
 ListDataListener[] getListDataListeners()
          Returns an array of all the list data listeners registered on this ArrayListModel.
 int getSize()
          Returns the length of the list or 0 if there's no list.
 Object remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(Object o)
          Removes a single instance of the specified element from this list, if it is present (optional operation).
 void removeListDataListener(ListDataListener l)
          Removes a listener from the list that's notified each time a change to the data model occurs.
protected  void removeRange(int fromIndex, int toIndex)
          Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
 Object set(int index, Object element)
          Replaces the element at the specified position in this list with the specified element.
 
Methods inherited from class java.util.ArrayList
clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, removeAll, retainAll, size, subList, toArray, toArray
 

Constructor Detail

ArrayListModel

public ArrayListModel()
Constructs an empty list with an initial capacity of ten.


ArrayListModel

public ArrayListModel(int initialCapacity)
Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity - the initial capacity of the list.
Throws:
IllegalArgumentException - if the specified initial capacity is negative

ArrayListModel

public ArrayListModel(Collection c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. The ArrayListModel instance has an initial capacity of 110% the size of the specified collection.

Parameters:
c - the collection whose elements are to be placed into this list.
Throws:
NullPointerException - if the specified collection is null
Method Detail

add

public void add(int index,
                Object element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:
add in interface List
Overrides:
add in class ArrayList
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

add

public boolean add(Object o)
Appends the specified element to the end of this list.

Specified by:
add in interface Collection
Specified by:
add in interface List
Overrides:
add in class ArrayList
Parameters:
o - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

addAll

public boolean addAll(int index,
                      Collection c)
Inserts all of the elements in the specified Collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified Collection's iterator.

Specified by:
addAll in interface List
Overrides:
addAll in class ArrayList
Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).
NullPointerException - if the specified Collection is null.

addAll

public boolean addAll(Collection c)
Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)

Specified by:
addAll in interface Collection
Specified by:
addAll in interface List
Overrides:
addAll in class ArrayList
Parameters:
c - the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Throws:
NullPointerException - if the specified collection is null.

clear

public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.

Specified by:
clear in interface Collection
Specified by:
clear in interface List
Overrides:
clear in class ArrayList

remove

public Object remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Specified by:
remove in interface List
Overrides:
remove in class ArrayList
Parameters:
index - the index of the element to removed.
Returns:
the element that was removed from the list.
Throws:
IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

remove

public boolean remove(Object o)
Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).

This implementation looks for the index of the specified element. If it finds the element, it removes the element at this index by calling #remove(int) that fires a ListDataEvent.

Specified by:
remove in interface Collection
Specified by:
remove in interface List
Overrides:
remove in class ArrayList
Parameters:
o - element to be removed from this list, if present.
Returns:
true if the list contained the specified element.

removeRange

protected void removeRange(int fromIndex,
                           int toIndex)
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

Overrides:
removeRange in class ArrayList
Parameters:
fromIndex - index of first element to be removed.
toIndex - index after last element to be removed.

set

public Object set(int index,
                  Object element)
Replaces the element at the specified position in this list with the specified element.

Specified by:
set in interface List
Overrides:
set in class ArrayList
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.
Throws:
IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

addListDataListener

public void addListDataListener(ListDataListener l)
Adds a listener to the list that's notified each time a change to the data model occurs.

Specified by:
addListDataListener in interface ListModel
Parameters:
l - the ListDataListener to be added

removeListDataListener

public void removeListDataListener(ListDataListener l)
Removes a listener from the list that's notified each time a change to the data model occurs.

Specified by:
removeListDataListener in interface ListModel
Parameters:
l - the ListDataListener to be removed

getElementAt

public Object getElementAt(int index)
Returns the value at the specified index.

Specified by:
getElementAt in interface ListModel
Parameters:
index - the requested index
Returns:
the value at index

getSize

public int getSize()
Returns the length of the list or 0 if there's no list.

Specified by:
getSize in interface ListModel
Returns:
the length of the list or 0 if there's no list

fireContentsChanged

public void fireContentsChanged(int index)
Notifies all registered ListDataListeners that the element at the specified index has changed. Useful if there's a content change without any structural change.

This method must be called after the element of the list changes.

Parameters:
index - the index of the element that has changed
See Also:
EventListenerList

getListDataListeners

public ListDataListener[] getListDataListeners()
Returns an array of all the list data listeners registered on this ArrayListModel.

Returns:
all of this model's ListDataListeners, or an empty array if no list data listeners are currently registered
See Also:
addListDataListener(ListDataListener), removeListDataListener(ListDataListener)


Copyright © 2002-2006 JGoodies Karsten Lentzsch. All Rights Reserved.