dsto.dfc.swing.list
Class SortedListModelAdapter

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by dsto.dfc.swing.list.SortedListModelAdapter
All Implemented Interfaces:
CollectionListener, DfcListModel, Disposable, Serializable, EventListener, ListModel

public class SortedListModelAdapter
extends AbstractListModel
implements DfcListModel, CollectionListener, Disposable

Presents any collection as a sorted DfcListModel. If the collection implements MonitoredCollection, changes are automatically reflected in the list model.

Version:
$Revision$
See Also:
Serialized Form

Field Summary
protected  Collection collection
           
protected  Comparator comparator
           
protected  boolean mutable
           
protected  ArrayList sortedCollection
           
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
SortedListModelAdapter(Collection collection)
          Create a list model that sorts Comparable items.
SortedListModelAdapter(Collection collection, Comparator comparator)
          Create a list model that sorts the collection using a specified comparator.
 
Method Summary
 void addEntry(Object entry)
          Add a new entry at its correct sorted index in the list.
 void addEntry(Object entry, int row)
          Add an entry can at a given row.
 boolean canAddEntry(Object entry, int row)
          Test if an entry can be added at a given row.
 boolean canRemoveEntry(int row)
          Test if an entry can be removed.
 boolean canReplaceEntry(Object entry, int row)
          Test if an entry can be replaced.
protected  void checkAddReplace(Object entry, int row)
           
protected  void checkRemove(int row)
           
 void dispose()
          Dispose of the object (unregister listeners, close open resources etc).
 void elementsAdded(Collection c)
          For use when not using a MonitoredCollection in the list: indicate that elements have been added to the underlying collection.
 void elementsAdded(CollectionEvent e)
           
 void elementsRemoved(Collection c)
          For use when not using a MonitoredCollection in the list: indicate that elements have been removed from the underlying collection.
 void elementsRemoved(CollectionEvent e)
           
 Object getElementAt(int index)
           
 int getSize()
           
protected  int getSortedIndex(Object listItem)
           
 int indexOf(Object entry)
          Find the first occurrence of an entry and return its index.
 boolean isMutable()
          Test if this model is mutable (can be changed).
 Object removeEntry(int row)
          Remove an entry at a given row.
 Object replaceEntry(Object entry, int row)
          Replace an entry can at a given row.
 void setMutable(boolean newValue)
           
 
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, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
 

Field Detail

collection

protected Collection collection

sortedCollection

protected ArrayList sortedCollection

comparator

protected Comparator comparator

mutable

protected boolean mutable
Constructor Detail

SortedListModelAdapter

public SortedListModelAdapter(Collection collection)
Create a list model that sorts Comparable items.


SortedListModelAdapter

public SortedListModelAdapter(Collection collection,
                              Comparator comparator)
Create a list model that sorts the collection using a specified comparator.

Method Detail

dispose

public void dispose()
Description copied from interface: Disposable
Dispose of the object (unregister listeners, close open resources etc). It should be safe to call this method more than once. Note for beans that support client event listeners: if there are listeners registered when this is called, this method should do nothing.

Specified by:
dispose in interface Disposable

addEntry

public void addEntry(Object entry)
              throws UnsupportedOperationException
Add a new entry at its correct sorted index in the list.

Parameters:
entry - The new entry.
Throws:
UnsupportedOperationException - if list is not mutable.

getSortedIndex

protected int getSortedIndex(Object listItem)

elementsAdded

public void elementsAdded(Collection c)
For use when not using a MonitoredCollection in the list: indicate that elements have been added to the underlying collection.

Parameters:
c - The collection of new elements.

elementsRemoved

public void elementsRemoved(Collection c)
For use when not using a MonitoredCollection in the list: indicate that elements have been removed from the underlying collection.

Parameters:
c - The deleted elements.

isMutable

public boolean isMutable()
Description copied from interface: DfcListModel
Test if this model is mutable (can be changed).

Specified by:
isMutable in interface DfcListModel
Returns:
True if the list can be changed.

setMutable

public void setMutable(boolean newValue)

indexOf

public int indexOf(Object entry)
Description copied from interface: DfcListModel
Find the first occurrence of an entry and return its index.

Specified by:
indexOf in interface DfcListModel
Returns:
The index of entry or -1 if not found.

canReplaceEntry

public boolean canReplaceEntry(Object entry,
                               int row)
Description copied from interface: DfcListModel
Test if an entry can be replaced.

Specified by:
canReplaceEntry in interface DfcListModel
Parameters:
entry - The entry to add.
row - The row to overwrite.
Returns:
True if entry can be replaced.
See Also:
DfcListModel.replaceEntry(java.lang.Object, int)

replaceEntry

public Object replaceEntry(Object entry,
                           int row)
                    throws IndexOutOfBoundsException,
                           UnsupportedOperationException
Description copied from interface: DfcListModel
Replace an entry can at a given row.

Specified by:
replaceEntry in interface DfcListModel
Parameters:
entry - The entry to add.
row - The row to overwrite.
Returns:
The old value at row.
Throws:
IndexOutOfBoundsException - if row is out of bounds.
UnsupportedOperationException - if entry is not allowed to be replaced.
See Also:
DfcListModel.canReplaceEntry(java.lang.Object, int)

canAddEntry

public boolean canAddEntry(Object entry,
                           int row)
Description copied from interface: DfcListModel
Test if an entry can be added at a given row.

Specified by:
canAddEntry in interface DfcListModel
Parameters:
entry - The entry to add.
row - The row to add the new entry.
Returns:
True if entry can be added at row.
See Also:
DfcListModel.addEntry(java.lang.Object, int)

addEntry

public void addEntry(Object entry,
                     int row)
              throws IndexOutOfBoundsException,
                     UnsupportedOperationException
Description copied from interface: DfcListModel
Add an entry can at a given row.

Specified by:
addEntry in interface DfcListModel
Parameters:
entry - The entry to add.
row - The row to add the new entry.
Throws:
IndexOutOfBoundsException - if row is out of bounds.
UnsupportedOperationException - if entry is not allowed to be added.
See Also:
DfcListModel.canAddEntry(java.lang.Object, int)

canRemoveEntry

public boolean canRemoveEntry(int row)
Description copied from interface: DfcListModel
Test if an entry can be removed.

Specified by:
canRemoveEntry in interface DfcListModel
Parameters:
row - The row to be removed.
Returns:
True if row can be removed.
See Also:
DfcListModel.removeEntry(int)

removeEntry

public Object removeEntry(int row)
                   throws IndexOutOfBoundsException,
                          UnsupportedOperationException
Description copied from interface: DfcListModel
Remove an entry at a given row.

Specified by:
removeEntry in interface DfcListModel
Parameters:
row - The row to remove.
Returns:
The entry that was removed.
Throws:
IndexOutOfBoundsException - if row is out of bounds.
UnsupportedOperationException - if entry is not allowed to be removed.
See Also:
DfcListModel.canRemoveEntry(int)

checkAddReplace

protected void checkAddReplace(Object entry,
                               int row)
                        throws UnsupportedOperationException
Throws:
UnsupportedOperationException

checkRemove

protected void checkRemove(int row)
                    throws UnsupportedOperationException
Throws:
UnsupportedOperationException

getSize

public int getSize()
Specified by:
getSize in interface ListModel

getElementAt

public Object getElementAt(int index)
Specified by:
getElementAt in interface ListModel

elementsAdded

public void elementsAdded(CollectionEvent e)
Specified by:
elementsAdded in interface CollectionListener

elementsRemoved

public void elementsRemoved(CollectionEvent e)
Specified by:
elementsRemoved in interface CollectionListener


Copyright © 2008 Commonwealth of Australia