com.alee.laf.list
Class WebListModel<T>

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by com.alee.laf.list.WebListModel<T>
All Implemented Interfaces:
java.io.Serializable, javax.swing.ListModel
Direct Known Subclasses:
CheckBoxListModel, FileListModel

public class WebListModel<T>
extends javax.swing.AbstractListModel

Modified and optimized Swing DefaultListModel. This model contains multiply elements add/remove methods and works with typed elements.

Author:
Mikle Garin
See Also:
Serialized Form

Field Summary
protected  java.util.Vector<T> delegate
          List data vector.
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
WebListModel()
          Constructs empty model.
WebListModel(java.util.Collection<T> data)
          Constructs model with the specified elements.
WebListModel(T... data)
          Constructs model with the specified elements.
 
Method Summary
 void add(int index, T element)
          Inserts the specified element at the specified position in this list.
 void add(T element)
          Adds the specified element to this list.
 void addElement(T obj)
          Adds the specified component to the end of this list.
 void addElements(java.util.Collection<T> objects)
          Adds the specified components to the end of this list.
 void addElements(T... objects)
          Adds the specified components to the end of this list.
 int capacity()
          Returns the current capacity of this list.
 void clear()
          Removes all of the elements from this list.
 boolean contains(T elem)
          Tests whether the specified object is a component in this list.
 void copyInto(T[] array)
          Copies the components of this list into the specified array.
 T elementAt(int index)
          Returns the component at the specified index.
 java.util.Enumeration<T> elements()
          Returns an enumeration of the components of this list.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this list, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
 T firstElement()
          Returns the first component of this list.
 T get(int index)
          Returns the element at the specified position in this list.
 T getElementAt(int index)
          Returns the component at the specified index.
 java.util.List<T> getElements()
          Returns list of all elements.
 int getSize()
          Returns the number of components in this list.
 int indexOf(T elem)
          Searches for the first occurrence of elem.
 int indexOf(T elem, int index)
          Searches for the first occurrence of elem, beginning the search at index.
 void insertElementAt(T obj, int index)
          Inserts the specified object as a component in this list at the specified index.
 boolean isEmpty()
          Tests whether this list has any components.
 T lastElement()
          Returns the last component of the list.
 int lastIndexOf(T elem)
          Returns the index of the last occurrence of elem.
 int lastIndexOf(T elem, int index)
          Searches backwards for elem, starting from the specified index, and returns an index to it.
 T remove(int index)
          Removes the element at the specified position in this list.
 T remove(T element)
          Removes the specified element from this list.
 void removeAllAfter(int index)
          Removes all elements from this list which has index larger than the specified one.
 void removeAllBefore(int index)
          Removes all elements from this list which has index lower than the specified one.
 void removeAllElements()
          Removes all components from this list and sets its size to zero.
 boolean removeElement(T object)
          Removes the first (lowest-indexed) occurrence of the argument from this list.
 void removeElementAt(int index)
          Deletes the component at the specified index.
 void removeElements(java.util.Collection<T> objects)
          Removes the specified elements from this list.
 void removeElements(T... objects)
          Removes the specified elements from this list.
 void removeRange(int fromIndex, int toIndex)
          Deletes the components at the specified range of indexes.
 T set(int index, T element)
          Replaces the element at the specified position in this list with the specified element.
 void setElementAt(T obj, int index)
          Sets the component at the specified index of this list to be the specified object.
 void setElements(java.util.Collection<T> objects)
          Clears list data and adds specified elements.
 void setSize(int newSize)
          Sets the size of this list.
 int size()
          Returns the number of components in this list.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this list in the correct order.
 java.lang.String toString()
          Returns a string that displays and identifies this object's properties.
 void trimToSize()
          Trims the capacity of this list to be the list's current size.
 void update(T element)
          Updates cell for the specified element.
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

delegate

protected java.util.Vector<T> delegate
List data vector.

Constructor Detail

WebListModel

public WebListModel()
Constructs empty model.


WebListModel

public WebListModel(T... data)
Constructs model with the specified elements.


WebListModel

public WebListModel(java.util.Collection<T> data)
Constructs model with the specified elements.

Method Detail

getSize

public int getSize()
Returns the number of components in this list.

This method is identical to size, which implements the List interface defined in the 1.2 Collections framework. This method exists in conjunction with setSize so that size is identifiable as a JavaBean property.

Returns:
the number of components in this list
See Also:
size()

getElementAt

public T getElementAt(int index)
Returns the component at the specified index.

Note: Although this method is not deprecated, the preferred method to use is get(int), which implements the List interface defined in the 1.2 Collections framework.

Parameters:
index - an index into this list
Returns:
the component at the specified index
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is negative or greater than the current size of this list
See Also:
get(int)

getElements

public java.util.List<T> getElements()
Returns list of all elements.

Returns:
list of all elements

copyInto

public void copyInto(T[] array)
Copies the components of this list into the specified array. The array must be big enough to hold all the objects in this list, else an IndexOutOfBoundsException is thrown.

Parameters:
array - the array into which the components get copied

trimToSize

public void trimToSize()
Trims the capacity of this list to be the list's current size.

See Also:
Vector.trimToSize()

ensureCapacity

public void ensureCapacity(int minCapacity)
Increases the capacity of this list, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.

Parameters:
minCapacity - the desired minimum capacity
See Also:
Vector.ensureCapacity(int)

setSize

public void setSize(int newSize)
Sets the size of this list.

Parameters:
newSize - the new size of this list
See Also:
Vector.setSize(int)

capacity

public int capacity()
Returns the current capacity of this list.

Returns:
the current capacity
See Also:
Vector.capacity()

size

public int size()
Returns the number of components in this list.

Returns:
the number of components in this list
See Also:
Vector.size()

isEmpty

public boolean isEmpty()
Tests whether this list has any components.

Returns:
true if and only if this list has no components, that is, its size is zero; false otherwise
See Also:
Vector.isEmpty()

elements

public java.util.Enumeration<T> elements()
Returns an enumeration of the components of this list.

Returns:
an enumeration of the components of this list
See Also:
Vector.elements()

contains

public boolean contains(T elem)
Tests whether the specified object is a component in this list.

Parameters:
elem - an object
Returns:
true if the specified object is the same as a component in this list
See Also:
Vector.contains(Object)

indexOf

public int indexOf(T elem)
Searches for the first occurrence of elem.

Parameters:
elem - an object
Returns:
the index of the first occurrence of the argument in this list; returns -1 if the object is not found
See Also:
Vector.indexOf(Object)

indexOf

public int indexOf(T elem,
                   int index)
Searches for the first occurrence of elem, beginning the search at index.

Parameters:
elem - an desired component
index - the index from which to begin searching
Returns:
the index where the first occurrence of elem is found after index; returns -1 if the elem is not found in the list
See Also:
Vector.indexOf(Object, int)

lastIndexOf

public int lastIndexOf(T elem)
Returns the index of the last occurrence of elem.

Parameters:
elem - the desired component
Returns:
the index of the last occurrence of elem in the list; returns -1 if the object is not found
See Also:
Vector.lastIndexOf(Object)

lastIndexOf

public int lastIndexOf(T elem,
                       int index)
Searches backwards for elem, starting from the specified index, and returns an index to it.

Parameters:
elem - the desired component
index - the index to start searching from
Returns:
the index of the last occurrence of the elem in this list at position less than index; returns -1 if the object is not found
See Also:
Vector.lastIndexOf(Object, int)

elementAt

public T elementAt(int index)
Returns the component at the specified index. Throws an ArrayIndexOutOfBoundsException if the index is negative or not less than the size of the list.
Note: Although this method is not deprecated, the preferred method to use is get(int), which implements the List interface defined in the 1.2 Collections framework.

Parameters:
index - an index into this list
Returns:
the component at the specified index
See Also:
get(int), Vector.elementAt(int)

firstElement

public T firstElement()
Returns the first component of this list. Throws a NoSuchElementException if this vector has no components.

Returns:
the first component of this list
See Also:
Vector.firstElement()

lastElement

public T lastElement()
Returns the last component of the list. Throws a NoSuchElementException if this vector has no components.

Returns:
the last component of the list
See Also:
Vector.lastElement()

setElementAt

public void setElementAt(T obj,
                         int index)
Sets the component at the specified index of this list to be the specified object. The previous component at that position is discarded.

Throws an ArrayIndexOutOfBoundsException if the index is invalid.

Note: Although this method is not deprecated, the preferred method to use is set(int, Object), which implements the List interface defined in the 1.2 Collections framework.

Parameters:
obj - what the component is to be set to
index - the specified index
See Also:
set(int, Object), Vector.setElementAt(Object, int)

removeElementAt

public void removeElementAt(int index)
Deletes the component at the specified index.

Throws an ArrayIndexOutOfBoundsException if the index is invalid.

Note: Although this method is not deprecated, the preferred method to use is remove(int), which implements the List interface defined in the 1.2 Collections framework.

Parameters:
index - the index of the object to remove
See Also:
remove(int), Vector.removeElementAt(int)

insertElementAt

public void insertElementAt(T obj,
                            int index)
Inserts the specified object as a component in this list at the specified index.

Throws an ArrayIndexOutOfBoundsException if the index is invalid.

Note: Although this method is not deprecated, the preferred method to use is add(int, Object), which implements the List interface defined in the 1.2 Collections framework.

Parameters:
obj - the component to insert
index - where to insert the new component
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index was invalid
See Also:
add(int, Object), Vector.insertElementAt(Object, int)

addElement

public void addElement(T obj)
Adds the specified component to the end of this list.

Parameters:
obj - the component to be added
See Also:
Vector.addElement(Object)

addElements

public void addElements(T... objects)
Adds the specified components to the end of this list.

Parameters:
objects - the components to be added

addElements

public void addElements(java.util.Collection<T> objects)
Adds the specified components to the end of this list.

Parameters:
objects - the components to be added

setElements

public void setElements(java.util.Collection<T> objects)
Clears list data and adds specified elements.

Parameters:
objects - the components to be added

removeElement

public boolean removeElement(T object)
Removes the first (lowest-indexed) occurrence of the argument from this list.

Parameters:
object - the component to be removed
Returns:
true if the argument was a component of this list; false otherwise
See Also:
Vector.removeElement(Object)

removeElements

public void removeElements(T... objects)
Removes the specified elements from this list.

Parameters:
objects - the components to be removed

removeElements

public void removeElements(java.util.Collection<T> objects)
Removes the specified elements from this list.

Parameters:
objects - the components to be removed

removeAllElements

public void removeAllElements()
Removes all components from this list and sets its size to zero.
Note: Although this method is not deprecated, the preferred method to use is clear, which implements the List interface defined in the 1.2 Collections framework.

See Also:
clear(), Vector.removeAllElements()

removeAllBefore

public void removeAllBefore(int index)
Removes all elements from this list which has index lower than the specified one.

Parameters:
index - index to process

removeAllAfter

public void removeAllAfter(int index)
Removes all elements from this list which has index larger than the specified one.

Parameters:
index - index to process

toString

public java.lang.String toString()
Returns a string that displays and identifies this object's properties.

Overrides:
toString in class java.lang.Object
Returns:
a String representation of this object

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this list in the correct order.

Returns:
an array containing the elements of the list
See Also:
Vector.toArray()

get

public T get(int index)
Returns the element at the specified position in this list.

Throws an ArrayIndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt;= size()).

Parameters:
index - index of element to return

set

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

Throws an ArrayIndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt;= size()).

Parameters:
index - index of element to replace
element - element to be stored at the specified position
Returns:
the element previously at the specified position

add

public void add(T element)
Adds the specified element to this list.

Parameters:
element - element to be added

add

public void add(int index,
                T element)
Inserts the specified element at the specified position in this list.

Throws an ArrayIndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt; size()).

Parameters:
index - index at which the specified element is to be inserted
element - element to be inserted

remove

public T remove(T element)
Removes the specified element from this list.

Parameters:
element - element to remove
Returns:
removed element

remove

public T remove(int index)
Removes the element at the specified position in this list. Returns the element that was removed from the list.

Throws an ArrayIndexOutOfBoundsException if the index is out of range (index &lt; 0 || index &gt;= size()).

Parameters:
index - the index of the element to removed
Returns:
removed element

clear

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


removeRange

public void removeRange(int fromIndex,
                        int toIndex)
Deletes the components at the specified range of indexes. The removal is inclusive, so specifying a range of (1,5) removes the component at index 1 and the component at index 5, as well as all components in between.

Throws an ArrayIndexOutOfBoundsException if the index was invalid. Throws an IllegalArgumentException if fromIndex &gt; toIndex.

Parameters:
fromIndex - the index of the lower end of the range
toIndex - the index of the upper end of the range
See Also:
remove(int)

update

public void update(T element)
Updates cell for the specified element.

Parameters:
element - element to update