com.jgoodies.binding.list
Class LinkedListModel<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList<E>
                  extended by com.jgoodies.binding.list.LinkedListModel<E>
Type Parameters:
E - the type of the list elements
All Implemented Interfaces:
ObservableList<E>, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Deque<E>, java.util.List<E>, java.util.Queue<E>, javax.swing.ListModel

public final class LinkedListModel<E>
extends java.util.LinkedList<E>
implements ObservableList<E>

Adds ListModel capabilities to its superclass LinkedList, 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.8 $
Author:
Karsten Lentzsch
See Also:
ArrayListModel, ListIterator, Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
LinkedListModel()
          Constructs an empty linked list.
LinkedListModel(java.util.Collection<? extends E> c)
          Constructs a linked list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 
Method Summary
 boolean add(E e)
          Appends the specified element to the end of this list.
 void add(int index, E element)
          Inserts the specified element at the specified position in this list.
 boolean addAll(int index, java.util.Collection<? extends E> c)
          Inserts all of the elements in the specified Collection into this list, starting at the specified position.
 void addFirst(E e)
          Inserts the given element at the beginning of this list.
 void addLast(E e)
          Appends the given element to the end of this list.
 void addListDataListener(javax.swing.event.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.
 E getElementAt(int index)
          Returns the value at the specified index.
 javax.swing.event.ListDataListener[] getListDataListeners()
          Returns an array of all the list data listeners registered on this LinkedListModel.
 int getSize()
          Returns the length of the list or 0 if there's no list.
 java.util.ListIterator<E> listIterator(int index)
          Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
 E remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(java.lang.Object o)
          Removes a single instance of the specified element from this collection, if it is present (optional operation).
 E removeFirst()
          Removes and returns the first element from this list.
 E removeLast()
          Removes and returns the last element from this list.
 void removeListDataListener(javax.swing.event.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.
 E set(int index, E element)
          Replaces the element at the specified position in this list with the specified element.
 
Methods inherited from class java.util.LinkedList
addAll, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, removeFirstOccurrence, removeLastOccurrence, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, removeAll, retainAll, size, subList, toArray, toArray
 
Methods inherited from interface java.util.Deque
iterator
 

Constructor Detail

LinkedListModel

public LinkedListModel()
Constructs an empty linked list.


LinkedListModel

public LinkedListModel(java.util.Collection<? extends E> c)
Constructs a linked list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

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

add

public void add(int index,
                E 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 java.util.List<E>
Overrides:
add in class java.util.LinkedList<E>
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).

add

public boolean add(E e)
Appends the specified element to the end of this list.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Deque<E>
Specified by:
add in interface java.util.List<E>
Specified by:
add in interface java.util.Queue<E>
Overrides:
add in class java.util.LinkedList<E>
Parameters:
e - element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends E> 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 java.util.List<E>
Overrides:
addAll in class java.util.LinkedList<E>
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:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index > size()).
java.lang.NullPointerException - if the specified Collection is null.

addFirst

public void addFirst(E e)
Inserts the given element at the beginning of this list.

Specified by:
addFirst in interface java.util.Deque<E>
Overrides:
addFirst in class java.util.LinkedList<E>
Parameters:
e - the element to be inserted at the beginning of this list.

addLast

public void addLast(E e)
Appends the given element to the end of this list. (Identical in function to the add method; included only for consistency.)

Specified by:
addLast in interface java.util.Deque<E>
Overrides:
addLast in class java.util.LinkedList<E>
Parameters:
e - the element to be inserted at the end of this list.

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 java.util.Collection<E>
Specified by:
clear in interface java.util.List<E>
Overrides:
clear in class java.util.LinkedList<E>

remove

public E 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 java.util.List<E>
Overrides:
remove in class java.util.LinkedList<E>
Parameters:
index - the index of the element to removed.
Returns:
the element that was removed from the list.
Throws:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

remove

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

This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Deque<E>
Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class java.util.LinkedList<E>
Parameters:
o - element to be removed from this collection, if present.
Returns:
true if the collection contained the specified element.

removeFirst

public E removeFirst()
Removes and returns the first element from this list.

Specified by:
removeFirst in interface java.util.Deque<E>
Overrides:
removeFirst in class java.util.LinkedList<E>
Returns:
the first element from this list.
Throws:
java.util.NoSuchElementException - if this list is empty.

removeLast

public E removeLast()
Removes and returns the last element from this list.

Specified by:
removeLast in interface java.util.Deque<E>
Overrides:
removeLast in class java.util.LinkedList<E>
Returns:
the last element from this list.
Throws:
java.util.NoSuchElementException - if this list is empty.

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 java.util.AbstractList<E>
Parameters:
fromIndex - index of first element to be removed.
toIndex - index after last element to be removed.

set

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

Specified by:
set in interface java.util.List<E>
Overrides:
set in class java.util.LinkedList<E>
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:
java.lang.IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).

listIterator

public java.util.ListIterator<E> listIterator(int index)
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. Obeys the general contract of List.listIterator(int).

The list-iterator is fail-fast: if the list is structurally modified at any time after the Iterator is created, in any way except through the list-iterator's own remove or add methods, the list-iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Specified by:
listIterator in interface java.util.List<E>
Overrides:
listIterator in class java.util.LinkedList<E>
Parameters:
index - index of first element to be returned from the list-iterator (by a call to next).
Returns:
a ListIterator of the elements in this list (in proper sequence), starting at the specified position in the list.
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
See Also:
List.listIterator(int)

addListDataListener

public void addListDataListener(javax.swing.event.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 javax.swing.ListModel
Parameters:
l - the ListDataListener to be added

removeListDataListener

public void removeListDataListener(javax.swing.event.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 javax.swing.ListModel
Parameters:
l - the ListDataListener to be removed

getElementAt

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

Specified by:
getElementAt in interface javax.swing.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 javax.swing.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 javax.swing.event.ListDataListener[] getListDataListeners()
Returns an array of all the list data listeners registered on this LinkedListModel.

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-2008 JGoodies Karsten Lentzsch. All Rights Reserved.