org.geotoolkit.gui.swing
Class ListTableModel<E>

Object
  extended by AbstractTableModel
      extended by ListTableModel<E>
Type Parameters:
E - The type of elements in the list backing this table model.
All Implemented Interfaces:
Serializable, TableModel
Direct Known Subclasses:
CategoryTable, MosaicTableModel

public abstract class ListTableModel<E>
extends AbstractTableModel

A table model backed by a list. The list is given to the constructor and retained by direct reference - it is not cloned, because it may contain millions of elements, sometime through a custom List implementation fetching the information on-the-fly from a database. Consequently if the content of the list is modified externally, then a fireXXX method (inherited from AbstractTableModel must be invoked explicitly. Note that those fireXXX methods don't need to be invoked if the list is modified through the methods provided in this class.


Multi-threading
Unless otherwise specified, methods in this class must be invoked from the Swing thread. Exceptions are the getElements method, which will be automatically executed in the Swing thread no matter the invoker thread. This convenience does not apply to methods inherited from the super-class, and may not apply to methods defined in the sub-classes.


Serialization
This model is serialiable if the underlying list is serializable.

Since:
3.00
Version:
3.13
Author:
Martin Desruisseaux (Geomatys)
See Also:
Serialized Form
Module:
display/geotk-widgets-swing (download)    View source code for this class

Field Summary
protected  List<E> elements
          The list of row elements.
 
Fields inherited from class AbstractTableModel
listenerList
 
Constructor Summary
protected ListTableModel(Class<E> type)
          Creates a new table model backed by an ArrayList.
protected ListTableModel(Class<E> type, List<E> elements)
          Creates a new table model for the given list.
 
Method Summary
 void add(Collection<? extends E> toAdd)
          Adds all elements from the given collection.
 E[] getElements()
          Returns a snapshot of every elements contained in this model.
 E[] getElements(int[] selection)
          Returns a snapshot of selected elements.
 E[] getElements(int lower, int upper)
          Returns a snapshot of a range of elements contained in this model.
 int getRowCount()
          Returns the number of rows in the table.
 void insert(int insertAt, Collection<? extends E> toAdd)
          Inserts at the given position all elements from the given collection.
 void remove(int[] indices)
          Removes the given elements from the list.
 void remove(int lower, int upper)
          Removes a range of rows (or elements).
 int removeDuplicates()
          Removes duplicated elements.
 void setElements(E... elts)
          Sets the elements.
 boolean sort()
          Sorts the elements in their natural order.
 
Methods inherited from class AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getColumnName, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface TableModel
getColumnCount, getValueAt
 

Field Detail

elements

protected final List<E> elements
The list of row elements. This is a direct reference to the list given by the user at construction time, not a clone. If this list is modified externaly, then the appropriate fireXXX method must be invoked explicitly.

Constructor Detail

ListTableModel

protected ListTableModel(Class<E> type)
Creates a new table model backed by an ArrayList.

Parameters:
type - The type of elements in the list.

ListTableModel

protected ListTableModel(Class<E> type,
                         List<E> elements)
Creates a new table model for the given list. The given list is retained by direct reference - it is not cloned. Consequently if the content of this list is modified externaly, then one of the fireXXX method inherited from AbstractTableModel must be invoked explicitly.

Parameters:
type - The type of elements in the list.
elements - The list of elements to display in a table.
Method Detail

getRowCount

public int getRowCount()
Returns the number of rows in the table.

Returns:
The number of rows.

getElements

public E[] getElements()
Returns a snapshot of every elements contained in this model. This method will be executed in the Swing thread even if invoked from an other thread.

Returns:
A snapshot of every elements in this model.

getElements

public E[] getElements(int lower,
                       int upper)
Returns a snapshot of a range of elements contained in this model. This method will be executed in the Swing thread even if invoked from an other thread.

Parameters:
lower - Index of the first element to be returned.
upper - Index after the last element to be returned.
Returns:
A snapshot of the given range of elements in this model.

getElements

public E[] getElements(int[] selection)
Returns a snapshot of selected elements. This method will be executed in the Swing thread even if invoked from an other thread.

Parameters:
selection - The indices of selected elements, typically given by JTable.getSelectedRows().
Returns:
A snapshot of the selected elements in this model.

setElements

public void setElements(E... elts)
Sets the elements. All previous elements are discarded before to add the new ones.

Parameters:
elts - The new elements.
Since:
3.13

add

public void add(Collection<? extends E> toAdd)
         throws UnsupportedOperationException
Adds all elements from the given collection. The default implementation invokes List.addAll(Collection) and uses the change of list size for computing the index to be given to the AbstractTableModel.fireTableRowsInserted(int,int) method. Consequently the list implementation doesn't need to accept every elements. Note however that the list must not change in a concurrent thread, otherwise the change event to be fired may have an inacurate index range.

Parameters:
toAdd - The elements to add.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.

insert

public void insert(int insertAt,
                   Collection<? extends E> toAdd)
            throws UnsupportedOperationException,
                   IndexOutOfBoundsException
Inserts at the given position all elements from the given collection. The elements in this table that are after the insertion point are shifted to larger index.

The default implementation invokes List.addAll(int,Collection) and uses the change of list size for computing the index to be given to the AbstractTableModel.fireTableRowsInserted(int,int) method. Consequently the list implementation doesn't need to accept every elements. Note however that the list must not change in a concurrent thread, otherwise the change event to be fired may have an inacurate index range.

Parameters:
insertAt - The insertion point. The first element will be inserted at that position.
toAdd - The elements to add.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.
IndexOutOfBoundsException - if the given index is out of range.

remove

public void remove(int lower,
                   int upper)
            throws UnsupportedOperationException
Removes a range of rows (or elements). Note that the upper value is inclusive, which is different than the Java Collection usage but consistent with the Swing usage.

Parameters:
lower - Index of the first row to remove, inclusive.
upper - Index of the last row to remove, inclusive.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.

remove

public void remove(int[] indices)
            throws UnsupportedOperationException
Removes the given elements from the list. The default implementation delegates the work to remove(int,int). Consecutive indexes will be removed in a single call of the above method.

Parameters:
indices - The index of elements to remove.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.

removeDuplicates

public int removeDuplicates()
                     throws UnsupportedOperationException
Removes duplicated elements. If two elements are equal in the sense of the Object.equals(Object) method, then the first one is removed from the elements list. We keep the last element instead of the first one on the assumption that the last element is the most recently added.

Returns:
The number of duplicates which have been found.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.

sort

public boolean sort()
             throws UnsupportedOperationException,
                    ClassCastException
Sorts the elements in their natural order.

Returns:
true if the row order changed as a result of this method call.
Throws:
UnsupportedOperationException - if the underlying elements list is not modifiable.
ClassCastException - if the list contains elements that are not mutually comparable.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.