org.dmonix.gui.models
Class SortableListModel

java.lang.Object
  extended byorg.dmonix.gui.models.SortableListModel
All Implemented Interfaces:
javax.swing.ListModel
Direct Known Subclasses:
SortableComboBoxModel

public class SortableListModel
extends java.lang.Object
implements javax.swing.ListModel

This is a basic non-sorted list model with the added possibility to sort the model at any time.
This class is well suited for large amounts of data since it uses a very fast sorting algorithm.

Copyright: Copyright (c) 2003

Company: dmonix.org

Version:
1.0
Author:
Peter Nerg

Field Summary
protected  java.util.Vector data
           
protected  java.util.Vector listeners
           
 
Constructor Summary
SortableListModel()
          Creates a basic character case sensitive list model.
SortableListModel(boolean ignoreCase)
          Creates a basic character case (in)sensitive list model.
SortableListModel(java.util.Comparator comparator)
          Creates a list model with custom comparator.
 
Method Summary
 void addElement(java.lang.Object o)
          Adds the specified component 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.
 java.util.Enumeration elements()
          List the data as an enum.
 void ensureCapacity(int minCapacity)
          Increases the capacity of this list.
protected  void fireEvent(int event, int startIndex, int endIndex)
           
 java.lang.Object getElementAt(int index)
          Returns the component at the specified index.
 int getSize()
          Returns the number of components in this list.
 boolean removeElement(java.lang.Object obj)
          Removes the first (lowest-indexed) occurrence of the argument from this list.
 java.lang.Object removeElementAt(int index)
          Deletes the object at the specified index.
 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.
 void setComparator(java.util.Comparator comparator)
          Set the Comparator used in the sort method.
 void sort()
          Sorts the specified list using the specified comparator.
 java.lang.Object[] toArray()
          Returns an array representation of this list model.
 void toArray(java.lang.Object[] array)
          Copies the components of this model into the specified array.
 void trimToSize()
          Trims the data container.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

protected java.util.Vector data

listeners

protected java.util.Vector listeners
Constructor Detail

SortableListModel

public SortableListModel()
Creates a basic character case sensitive list model.
This is equivalent to SortableListModel(false)


SortableListModel

public SortableListModel(boolean ignoreCase)
Creates a basic character case (in)sensitive list model.
The value of the argument decides whether the list model should be character case sensitive or not.
The model will use the IgnoreCaseComparator.

Parameters:
ignoreCase - character sensitive or not
See Also:
IgnoreCaseComparator

SortableListModel

public SortableListModel(java.util.Comparator comparator)
Creates a list model with custom comparator.

Parameters:
comparator - The comparator
Method Detail

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 listener to add

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 listener to remove

addElement

public void addElement(java.lang.Object o)
Adds the specified component to the end of this list.
The object must implement java.lang.Comparable
The list model can be sorted by invoking the sort method.

Parameters:
o - The object to add
Throws:
java.lang.IllegalArgumentException - If the provided object doesn't implement java.lang.Comparable

clear

public void clear()
Removes all of the elements from this list.


elements

public java.util.Enumeration elements()
List the data as an enum.

Returns:
The data as an enum

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 minimum capacity

getElementAt

public java.lang.Object getElementAt(int index)
Returns the component at the specified index.

Specified by:
getElementAt in interface javax.swing.ListModel
Parameters:
index - The index
Returns:
The object

getSize

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

Specified by:
getSize in interface javax.swing.ListModel
Returns:
The number of items in the model

removeElement

public boolean removeElement(java.lang.Object obj)
Removes the first (lowest-indexed) occurrence of the argument from this list.

Parameters:
obj - The object to remove
Returns:
true - removed, false - not removed/or not found

removeElementAt

public java.lang.Object removeElementAt(int index)
Deletes the object at the specified index.

Parameters:
index - The index
Returns:
Returns the deleted object

setComparator

public void setComparator(java.util.Comparator comparator)
Set the Comparator used in the sort method.

Parameters:
comparator - The comparator

sort

public void sort()
Sorts the specified list using the specified comparator.
Please refer to java.util.Collections.sort for further information.


toArray

public java.lang.Object[] toArray()
Returns an array representation of this list model.

Returns:
The array

toArray

public void toArray(java.lang.Object[] array)
Copies the components of this model into the specified array.
The item at index k in this model is copied into component k of the array.
The array must be big enough to hold all the objects in this vector, else an IndexOutOfBoundsException is thrown.

Parameters:
array - The array to store the data in

trimToSize

public void trimToSize()
Trims the data container.


fireEvent

protected void fireEvent(int event,
                         int startIndex,
                         int endIndex)


Copyright © 1998-2005 dmonix.org Peter Nerg, All Rights Reserved.