JIDE 3.5.15

com.jidesoft.combobox
Class SortableComboBoxModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by com.jidesoft.combobox.DefaultComboBoxModelWrapper
          extended by com.jidesoft.combobox.SortableComboBoxModel
All Implemented Interfaces:
ComboBoxModelWrapper, Serializable, EventListener, ComboBoxModel, ListDataListener, ListModel

public class SortableComboBoxModel
extends DefaultComboBoxModelWrapper
implements ListDataListener

SortableComboBoxModel is a combobox model wrapper which can do the sorting. Given any existing combobox model, you can wrap it into SortableComboBoxModel and use it later on any JComboBox.


 SortableComboBoxModel sortableComboBoxModel = new SortableComboBoxModel(comboBoxModel);
 JComboBox comboBox = new JComboBox(sortableComboBoxModel);
 
To sort the list, you just call

 sortableComboBoxModel.sort(); // or sort(SortableComboBoxModel.SORT_DESCENDING) to sort descending.
 

See Also:
Serialized Form

Field Summary
protected  EventListenerList listenerList
           
static int SORT_ASCENDING
           
static int SORT_DESCENDING
           
static int UNSORTED
           
 
Fields inherited from class com.jidesoft.combobox.DefaultComboBoxModelWrapper
_indexes, _model
 
Constructor Summary
SortableComboBoxModel()
           
SortableComboBoxModel(ComboBoxModel model)
          Creates a SortableListModel from any list model.
 
Method Summary
 void addSortListener(SortListener l)
          Adds the specified listener to receive collapsible pane events from this collapsible frame.
protected  int compare(int row1, int row2)
          Compares two rows to decide the order.
protected  int compare(Object o1, Object o2)
          Compares its two arguments for order.
 void contentsChanged(ListDataEvent e)
          The event handler when items are updated in the underlying combo box model.
 void fireSortEvent()
          Fires an collapsible pane event.
 Comparator getComparator()
          Gets the comparator.
 ComparatorContext getComparatorContext()
          Gets the comparator context.
static SortableComboBoxModel getSortableListModel(ListModel model)
          Gets the sortable table model.
 SortListener[] getSortListeners()
          Returns an array of all the SortListeners added to this CollapsiblePane with addSortListener.
 int getSortOrder()
          Gets the sort order.
 void intervalAdded(ListDataEvent e)
          The event handler when new items are added in the underlying combo box model.
 void intervalRemoved(ListDataEvent e)
          The event handler when items are removed in the underlying combo box model.
 boolean isAlwaysUseComparators()
          Checks if the alwaysUseComparators flag value.
 boolean isAutoResort()
          Checks if the table is automatically resorted when data changes.
 boolean isOptimized()
          Checks if the sortable table model is in optimized mode.
 void removeSortListener(SortListener l)
          Removes the specified collapsible pane listener so that it no longer receives collapsible pane events from this collapsible pane .
 void reset()
          Resets.
 void resort()
          Resort the table.
protected  int search(int[] indexes, int row)
          Searches the specified array of rows for the specified value using binary search.
 void setActualModel(ComboBoxModel model)
          Sets the actual list model.
 void setAlwaysUseComparators(boolean alwaysUseComparators)
          Sets the alwaysUseComparators flag.
 void setAutoResort(boolean autoResort)
          AutoResort is a feature that automatically resort the table when table data changes.
 void setComparator(Comparator comparator)
          Sets the comparator.
 void setComparatorContext(ComparatorContext comparatorContext)
          Sets the comparator context.
 void setIndexes(int[] indexes)
          Overrides the method in DefaultListModelWrapper to clear up any sort order.
 void setOptimized(boolean optimized)
          If optimized flag is true and the table is already sorted, SortableListModel will do incremental sorting when data is only partially changed.
 void setSortOrder(int sortOrder)
          Sets the sort order.
 void sort()
          Sort the list model ascending.
 void sort(int order)
          Sort the list model.
protected  void sort(int[] from, int[] to, int low, int high)
          Sorts the rows.
 void unsort()
          Unsorts the list model.
 
Methods inherited from class com.jidesoft.combobox.DefaultComboBoxModelWrapper
getActualIndexAt, getActualModel, getElementAt, getIndexAt, getIndexes, getSelectedItem, getSize, reallocateIndexes, setSelectedItem
 
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

SORT_ASCENDING

public static final int SORT_ASCENDING
See Also:
Constant Field Values

SORT_DESCENDING

public static final int SORT_DESCENDING
See Also:
Constant Field Values

UNSORTED

public static final int UNSORTED
See Also:
Constant Field Values

listenerList

protected EventListenerList listenerList
Constructor Detail

SortableComboBoxModel

public SortableComboBoxModel()

SortableComboBoxModel

public SortableComboBoxModel(ComboBoxModel model)
Creates a SortableListModel from any list model.

Parameters:
model - the underlying combo box model
Method Detail

setActualModel

public void setActualModel(ComboBoxModel model)
Description copied from class: DefaultComboBoxModelWrapper
Sets the actual list model.

Specified by:
setActualModel in interface ComboBoxModelWrapper
Overrides:
setActualModel in class DefaultComboBoxModelWrapper
Parameters:
model - the list model.

intervalAdded

public void intervalAdded(ListDataEvent e)
Description copied from class: DefaultComboBoxModelWrapper
The event handler when new items are added in the underlying combo box model.

Specified by:
intervalAdded in interface ListDataListener
Overrides:
intervalAdded in class DefaultComboBoxModelWrapper
Parameters:
e - the event

intervalRemoved

public void intervalRemoved(ListDataEvent e)
Description copied from class: DefaultComboBoxModelWrapper
The event handler when items are removed in the underlying combo box model.

Specified by:
intervalRemoved in interface ListDataListener
Overrides:
intervalRemoved in class DefaultComboBoxModelWrapper
Parameters:
e - the event

contentsChanged

public void contentsChanged(ListDataEvent e)
Description copied from class: DefaultComboBoxModelWrapper
The event handler when items are updated in the underlying combo box model.

Specified by:
contentsChanged in interface ListDataListener
Overrides:
contentsChanged in class DefaultComboBoxModelWrapper
Parameters:
e - the event

search

protected int search(int[] indexes,
                     int row)
Searches the specified array of rows for the specified value using binary search. The array must be sorted prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
Subclass can override this method to implement their own algorithm to do the search. When doing comparison in the algorithm, use the compare(int, int) method defined in this class. The original indexes are sorted in a way that compare(indexes[n], indexes[n + 1]) will always return 1 or 0. So if you compare(indexes[n], row) and it returns -1, you knows the row index should be greater than n.

Parameters:
indexes - the array of row to be searched.
row - the index of the row to be searched for.
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

sort

protected void sort(int[] from,
                    int[] to,
                    int low,
                    int high)
Sorts the rows.

Subclass can override this method to implement their own algorithm to sort. When doing comparison in the algorithm, use the compare(int, int) method defined in this class.

Parameters:
from - an int array of row indices to be sorted
to - an int array of row indices to store the result after sorting
low - the start index of the row in the array to be sorted
high - the end index of the row in the array to be sorted

compare

protected int compare(int row1,
                      int row2)
Compares two rows to decide the order. It will consider the ascending or descending and return different value so the return has different meaning comparing to that of Comparator.compare(o1, o2).
If it returns 1, it means keep the current order. If it returns -1, it means the order should be swapped. If it returns 0, it means the order doesn't matter. It could be either the table model is not sorted so the order doesn't matter or the two rows have the same order.

Subclass can override this method to provide their own way to compare the two rows. Please make sure you access the element using getActualModel().getElement(rowIndex) since SortableListModel is also a ListModelWrapper.

Parameters:
row1 - index of the first row to be compared
row2 - index of the second row to be compared
Returns:
If it returns 1, it means keep the current order. If it returns -1, it means the order should be swapped. If it returns 0, it means the order doesn't matter. It could be either the table model is not sorted so the order doesn't matter or the two rows have the same order.

compare

protected int compare(Object o1,
                      Object o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

By default, we took several steps to improve the performance. First we will check if either objects is null. If both are null, we return 0. If one is null, we return 1 or -1. Then we will check if the objects are String. If yes,we will use String#compareToIgnoreCase. Then we will check if the objects are Comparable and if yes, we will ues Comparable#compareTo method. At last, we will check if getComparator returns a non-null value and use it. If the getComparator() is null, we will try to get a Comparator from ObjectComparatorManager based on the object type and the ComparatorContext and use it. Subclass can override it to provide your own way to compare. If you ever call setComparator(java.util.Comparator) or setComparatorContext(com.jidesoft.comparator.ComparatorContext), we will try to use it without doing the extra checking steps. There is also a setter called setAlwaysUseComparators(boolean). If yes, we will also skip the checking of String and Comparable, and use getComparator or ObjectComparatorManager.

Parameters:
o1 - the first object to be compared
o2 - the second object to be compared
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

getComparator

public Comparator getComparator()
Gets the comparator.

Returns:
the comparator that will be used to compare the values.

setComparator

public void setComparator(Comparator comparator)
Sets the comparator. If you never set it, SortableTableModel will look up for a comparator from ObjectComparatorManager based on the class of the value to be compared and the value returned from getComparatorContext.

Parameters:
comparator -

getComparatorContext

public ComparatorContext getComparatorContext()
Gets the comparator context. Comparator context along with the type of the element in the list will uniquely find a comparator on ObjectComparatorManager. In most cases, comparator context is null. Only if the type is the same but you want to compare differently, you can use comparator context to break the tie.

Returns:
the comparator context. Default value is null.

setComparatorContext

public void setComparatorContext(ComparatorContext comparatorContext)
Sets the comparator context. Comparator context along with the type of the element in the list will uniquely find a comparator on ObjectComparatorManager.

Parameters:
comparatorContext -

getSortOrder

public int getSortOrder()
Gets the sort order.

Returns:
the sort order. The value could be UNSORTED, SORT_DESCENDING, or SORT_ASCENDING.

setSortOrder

public void setSortOrder(int sortOrder)
Sets the sort order.

Parameters:
sortOrder - the new sort order. The value could be UNSORTED, SORT_DESCENDING, or SORT_ASCENDING.

sort

public void sort()
Sort the list model ascending.


sort

public void sort(int order)
Sort the list model. The order could be either SORT_ASCENDING or SORT_DESCENDING. You can also pass in UNSORTED, which will be the same as unsort().

Parameters:
order - the new sort order.

unsort

public void unsort()
Unsorts the list model.


reset

public void reset()
Resets. It's the same as unsort().


setIndexes

public void setIndexes(int[] indexes)
Overrides the method in DefaultListModelWrapper to clear up any sort order.

Specified by:
setIndexes in interface ComboBoxModelWrapper
Overrides:
setIndexes in class DefaultComboBoxModelWrapper
Parameters:
indexes -

getSortableListModel

public static SortableComboBoxModel getSortableListModel(ListModel model)
Gets the sortable table model. If model is a ListModelWrapper, it will get the actual model until it finds the first sortable table model.

Returns:
sortable list model.

isOptimized

public boolean isOptimized()
Checks if the sortable table model is in optimized mode.

Returns:
optimized flag.

setOptimized

public void setOptimized(boolean optimized)
If optimized flag is true and the table is already sorted, SortableListModel will do incremental sorting when data is only partially changed. If the flag is false, SortableListModel will resort completely whenever data changed.
Default is false. The performance will be not as good but it's more stable. If you want to change the flag to true, you need to make sure the underlying table model fires correct event when data changes.

Parameters:
optimized -

isAutoResort

public boolean isAutoResort()
Checks if the table is automatically resorted when data changes.

Returns:
true if autoResort. Otherwise false.

setAutoResort

public void setAutoResort(boolean autoResort)
AutoResort is a feature that automatically resort the table when table data changes. This is the default behavior and useful in most cases. However there are cases when the data changes frequently, resort will make it very hard for user to read the value. So in these cases, you can set autoResort to false. Then you will need to provide a button to call resort() to resort the table.

Note: this flag is only used when optimized flag is true. If optimized is false, autoResort is always true.

Parameters:
autoResort -

resort

public void resort()
Resort the table. When autoResort is false, the table will be out of order after data changes. The method will resort the table while keeping the sorting columns.


addSortListener

public void addSortListener(SortListener l)
Adds the specified listener to receive collapsible pane events from this collapsible frame.

Parameters:
l - the collapsible pane listener

removeSortListener

public void removeSortListener(SortListener l)
Removes the specified collapsible pane listener so that it no longer receives collapsible pane events from this collapsible pane .

Parameters:
l - the collapsible pane listener

getSortListeners

public SortListener[] getSortListeners()
Returns an array of all the SortListeners added to this CollapsiblePane with addSortListener.

Returns:
all of the SortListeners added or an empty array if no listeners have been added
Since:
1.4
See Also:
addSortListener(com.jidesoft.grid.SortListener)

fireSortEvent

public void fireSortEvent()
Fires an collapsible pane event.


isAlwaysUseComparators

public boolean isAlwaysUseComparators()
Checks if the alwaysUseComparators flag value.

Returns:
true if alwaysUseComparators is true. Otherwise false.

setAlwaysUseComparators

public void setAlwaysUseComparators(boolean alwaysUseComparators)
Sets the alwaysUseComparators flag. By default, we will check if the two values are Comparable. If they both are, we will use Comparable.compareTo(Object) to compare. This is the default preferred way because this gives developer a finer control of the comparison result.

Parameters:
alwaysUseComparators - true or false.

JIDE 3.5.15