org.tbee.swing
Class SortedListModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by org.tbee.swing.SortedListModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.ListModel

public class SortedListModel
extends javax.swing.AbstractListModel

SortedListModel decorates an unsorted ListModel to provide a sorted model. You can create a SortedListModel from models you already have. Place the SortedListModel into a JList, for example, to provide a sorted view of your underlying model.

Author:
John O'Conner
See Also:
Serialized Form

Field Summary
static int ASCENDING
           
static int DESCENDING
           
static int UNORDERED
           
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
SortedListModel(javax.swing.ListModel model)
          Create a SortedListModel from an existing model using a default text comparator for the default Locale.
SortedListModel(javax.swing.ListModel model, int sortOrder)
          Create a SortedListModel from an existing model using a specific comparator and sort order.
SortedListModel(javax.swing.ListModel model, int sortOrder, java.util.Comparator comp)
          Create a SortedListModel from an existing model.
 
Method Summary
 java.lang.Object getElementAt(int index)
          Retrieve the sorted entry from the original model
 int getSize()
          Retrieve the size of the underlying model
static void main(java.lang.String[] args)
           
 void setComparator(java.util.Comparator comp)
           
 void setSortOrder(int sortOrder)
          Change the sort order of the model at runtime
 int toSortedModelIndex(int unsortedIndex)
          Convert an unsorted model index to a sorted model index.
 int[] toSortedModelIndices(int[] unsortedModelIndices)
          Convert an array of unsorted model selection indices to indices in the sorted model.
 int toUnsortedModelIndex(int index)
          Convert sorted model index to an unsorted model index.
 int[] toUnsortedModelIndices(int[] sortedSelectedIndices)
          Convert an array of sorted model indices to their unsorted model indices.
 
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
 

Field Detail

UNORDERED

public static int UNORDERED

ASCENDING

public static int ASCENDING

DESCENDING

public static int DESCENDING
Constructor Detail

SortedListModel

public SortedListModel(javax.swing.ListModel model)
Create a SortedListModel from an existing model using a default text comparator for the default Locale. Sort in ascending order.

Parameters:
model - the underlying, unsorted ListModel

SortedListModel

public SortedListModel(javax.swing.ListModel model,
                       int sortOrder)
Create a SortedListModel from an existing model using a specific comparator and sort order. Use a default text comparator.

Parameters:
model - the unsorted list model
sortOrder - that should be used

SortedListModel

public SortedListModel(javax.swing.ListModel model,
                       int sortOrder,
                       java.util.Comparator comp)
Create a SortedListModel from an existing model. Sort the model in the specified sort order using the given comparator.

Parameters:
model -
sortOrder -
comp -
Method Detail

getElementAt

public java.lang.Object getElementAt(int index)
                              throws java.lang.IndexOutOfBoundsException
Retrieve the sorted entry from the original model

Parameters:
index - index of an entry in the sorted model
Returns:
element in the original model to which our entry points
Throws:
java.lang.IndexOutOfBoundsException

getSize

public int getSize()
Retrieve the size of the underlying model

Returns:
size of the model

toUnsortedModelIndex

public int toUnsortedModelIndex(int index)
                         throws java.lang.IndexOutOfBoundsException
Convert sorted model index to an unsorted model index.

Parameters:
index - an index in the sorted model
Returns:
modelIndex an index in the unsorted model
Throws:
java.lang.IndexOutOfBoundsException

toUnsortedModelIndices

public int[] toUnsortedModelIndices(int[] sortedSelectedIndices)
Convert an array of sorted model indices to their unsorted model indices. Sort the resulting set of indices.

Parameters:
sortedSelectedIndices - indices of selected elements in the sorted model or sorted view
Returns:
unsortedSelectedIndices selected indices in the unsorted model

toSortedModelIndex

public int toSortedModelIndex(int unsortedIndex)
Convert an unsorted model index to a sorted model index.

Parameters:
unsortedIndex - an element index in the unsorted model
Returns:
sortedIndex an element index in the sorted model

toSortedModelIndices

public int[] toSortedModelIndices(int[] unsortedModelIndices)
Convert an array of unsorted model selection indices to indices in the sorted model. Sort the model indices from low to high to duplicate JList's getSelectedIndices method

Parameters:
unsortedModelIndices -
Returns:
an array of selected indices in the sorted model

setComparator

public void setComparator(java.util.Comparator comp)

setSortOrder

public void setSortOrder(int sortOrder)
Change the sort order of the model at runtime

Parameters:
sortOrder -

main

public static void main(java.lang.String[] args)
Parameters:
args -


Copyright © 2010 KnowledgePlaza. All Rights Reserved.