Class AbstractTableComparatorChooser<E>
- java.lang.Object
-
- ca.odell.glazedlists.gui.AbstractTableComparatorChooser<E>
-
- Direct Known Subclasses:
TableComparatorChooser
public abstract class AbstractTableComparatorChooser<E> extends java.lang.ObjectA TableComparatorChooser is a tool that allows the user to sort a table widget by clicking on the table's headers. It requires that the table has a SortedList as a source as the sorting on that list is used.- Author:
- Kevin Maltby
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.ObjectMULTIPLE_COLUMN_KEYBOARDEmulate the sorting behaviour of SUN's TableSorter, by Philip Milne et.static java.lang.ObjectMULTIPLE_COLUMN_MOUSESort multiple columns without use of the keyboard.static java.lang.ObjectMULTIPLE_COLUMN_MOUSE_WITH_UNDOSort multiple columns without use of the keyboard.static java.lang.ObjectSINGLE_COLUMNEmulate the sorting behaviour of Windows Explorer and Mac OS X Finder.protected SortedList<E>sortedListthe sorted list to choose the comparators forprotected java.util.Comparator<? super E>sortedListComparatorthe potentially foreign comparator associated with the sorted listprotected ca.odell.glazedlists.impl.gui.SortingStatesortingStatemanage which columns are sorted and in which order
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractTableComparatorChooser(SortedList<E> sortedList, TableFormat<? super E> tableFormat)Create aAbstractTableComparatorChooserthat sorts the specifiedSortedListover the specified columns.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappendComparator(int column, int comparatorIndex, boolean reverse)Append the comparator specified by the column, comparator index and reverse parameters to the end of the sequence of comparators thisAbstractTableComparatorChooseris sorting theSortedListby.voidclearComparator()Clear all sorting state and set theSortedListto use its natural order.java.util.ComparatorcreateComparatorForElement(java.util.Comparator<E> comparatorForColumn, int column)Creates aComparatorthat can compare list elements given aComparatorthat can compare column values for the specified column.protected ca.odell.glazedlists.impl.gui.SortingStatecreateSortingState()Returns the object which models the current sorting state of all columns in the table.voiddispose()voidfromString(java.lang.String stringEncoded)This class is capable of representing its own state with a String, to persist sorting state externally.intgetColumnComparatorIndex(int column)Gets the index comparator in use for the specified column.java.util.List<java.util.Comparator>getComparatorsForColumn(int column)Gets the list of comparators for the specified column.java.util.List<java.lang.Integer>getSortingColumns()Get the columns that the TableComparatorChooser is sorting by.protected intgetSortingStyle(int column)Gets the sorting style currently applied to the specified column.booleanisColumnReverse(int column)Gets whether the comparator in use for the specified column is reverse.protected voidrebuildComparator()Updates the comparator in use and applies it to the table.protected voidredetectComparator(java.util.Comparator<? super E> currentComparator)Examines the currentComparatorof the SortedList and adds icons to the table header renderers in response.protected voidsetTableFormat(TableFormat<? super E> tableFormat)Adjusts the TableFormat this comparator chooser uses when selecting comparators.java.lang.StringtoString()Encode the current sorting state as aString.
-
-
-
Field Detail
-
SINGLE_COLUMN
public static final java.lang.Object SINGLE_COLUMN
Emulate the sorting behaviour of Windows Explorer and Mac OS X Finder.Single clicks toggles between forward and reverse. If multiple comparators are available for a particular column, they will be cycled in order.
At most one column can be sorted at a time.
-
MULTIPLE_COLUMN_MOUSE
public static final java.lang.Object MULTIPLE_COLUMN_MOUSE
Sort multiple columns without use of the keyboard. Single clicks cycle through comparators, double clicks clear all secondary sorts before performing the normal behaviour.This is the original sorting strategy provided by Glazed Lists, with a limitation that it is impossible to clear a sort order that is already in place. It's designed to be used with multiple columns and multiple comparators per column.
The overall behaviour is as follows:
- Click: sort this column. If it's already sorted, reverse the sort order. If its already reversed, sort using the column's next comparator in forward order. If there are no more comparators, go to the first comparator. If there are multiple sort columns, sort this column after those columns.
- Double click: like a single click, but clear all sorting columns first.
-
MULTIPLE_COLUMN_MOUSE_WITH_UNDO
public static final java.lang.Object MULTIPLE_COLUMN_MOUSE_WITH_UNDO
Sort multiple columns without use of the keyboard. Single clicks cycle through comparators. Single click on the last comparator of the primary sort column will clear the entire sort (for all columns).This is an improvement over the original sorting strategy provided by Glazed Lists, since it gives a reasonable mechanism for clearing a sort order that is already in place. It's designed to be used with multiple columns and multiple comparators per column.
The overall behaviour is as follows:
- Click: sort this column. If it's already sorted, reverse the sort order. If its already reversed, sort using the column's next comparator in forward order. If there are no more comparators, clear ALL column comparators. If there are multiple sort columns, sort this column after those columns.
-
MULTIPLE_COLUMN_KEYBOARD
public static final java.lang.Object MULTIPLE_COLUMN_KEYBOARD
Emulate the sorting behaviour of SUN's TableSorter, by Philip Milne et. al.This is not a direct adaptation since we choose to support potentially many Comparators per column, whereas TableSorter is limited to one.
For reference, this is TableSorter's behaviour, copied shamelessly from that project's source file:
- Mouse-click: Clears the sorting gui of all other columns and advances the sorting gui of that column through three values: {NOT_SORTED, ASCENDING, DESCENDING} (then back to NOT_SORTED again).
- SHIFT-mouse-click: Clears the sorting gui of all other columns and cycles the sorting gui of the column through the same three values, in the opposite order: {NOT_SORTED, DESCENDING, ASCENDING}.
- CONTROL-mouse-click and CONTROL-SHIFT-mouse-click: as above except that the changes to the column do not cancel the statuses of columns that are already sorting - giving a way to initiate a compound sort.
- See Also:
- Table tutorial
-
sortedList
protected SortedList<E> sortedList
the sorted list to choose the comparators for
-
sortedListComparator
protected java.util.Comparator<? super E> sortedListComparator
the potentially foreign comparator associated with the sorted list
-
sortingState
protected ca.odell.glazedlists.impl.gui.SortingState sortingState
manage which columns are sorted and in which order
-
-
Constructor Detail
-
AbstractTableComparatorChooser
protected AbstractTableComparatorChooser(SortedList<E> sortedList, TableFormat<? super E> tableFormat)
Create aAbstractTableComparatorChooserthat sorts the specifiedSortedListover the specified columns.
-
-
Method Detail
-
createSortingState
protected ca.odell.glazedlists.impl.gui.SortingState createSortingState()
Returns the object which models the current sorting state of all columns in the table.
-
rebuildComparator
protected void rebuildComparator()
Updates the comparator in use and applies it to the table.
-
setTableFormat
protected void setTableFormat(TableFormat<? super E> tableFormat)
Adjusts the TableFormat this comparator chooser uses when selecting comparators. Calling this method will clear any active sorting.
-
getComparatorsForColumn
public java.util.List<java.util.Comparator> getComparatorsForColumn(int column)
Gets the list of comparators for the specified column. The user is free to add comparators to this list or clear the list if the specified column cannot be sorted.
-
getSortingColumns
public java.util.List<java.lang.Integer> getSortingColumns()
Get the columns that the TableComparatorChooser is sorting by.- Returns:
- a List of Integers. The first Integer is the primary sorting column, the second is the secondary, etc. This list may be empty but never null.
-
getColumnComparatorIndex
public int getColumnComparatorIndex(int column)
Gets the index comparator in use for the specified column. This comparator may be retrieved usinggetComparatorsForColumn(int).- Returns:
- the comparator index for the specified column, or -1 if that column is not being used to sort.
-
isColumnReverse
public boolean isColumnReverse(int column)
Gets whether the comparator in use for the specified column is reverse.
-
appendComparator
public void appendComparator(int column, int comparatorIndex, boolean reverse)Append the comparator specified by the column, comparator index and reverse parameters to the end of the sequence of comparators thisAbstractTableComparatorChooseris sorting theSortedListby.Append implies that if this
AbstractTableComparatorChooseris already sorting that list by another column, this comparator will only be used to break ties from thatComparator. If the table is already sorting by the specified column, it will be silently discarded.Suppose we're currently not sorting the table, this method will cause the table to be sorted by the column specified. If we are sorting the table by some column c, this will sort by that column first and the column specified here second.
If this
AbstractTableComparatorChooserdoesn't support multiple column sort, this will replace the currentComparatorrather than appending to it.- Parameters:
column- the column to sort bycomparatorIndex- the comparator to use, specify0for the default comparator.reverse- whether to reverse the specified comparator.
-
clearComparator
public void clearComparator()
Clear all sorting state and set theSortedListto use its natural order.
-
redetectComparator
protected void redetectComparator(java.util.Comparator<? super E> currentComparator)
Examines the currentComparatorof the SortedList and adds icons to the table header renderers in response.To do this, clicks are injected into each of the corresponding
ColumnClickTrackers.
-
getSortingStyle
protected int getSortingStyle(int column)
Gets the sorting style currently applied to the specified column.
-
createComparatorForElement
public java.util.Comparator createComparatorForElement(java.util.Comparator<E> comparatorForColumn, int column)
Creates aComparatorthat can compare list elements given aComparatorthat can compare column values for the specified column. This returns aComparatorthat extracts the table values for the specified column and then delegates the actual comparison to the specified comparator.
-
toString
public java.lang.String toString()
Encode the current sorting state as aString. This specially formattedStringis ideal for persistence using any preferences API. The state of thisAbstractTableComparatorChoosercan be restored by passing the return value of this method tofromString(String).- Overrides:
toStringin classjava.lang.Object
-
fromString
public void fromString(java.lang.String stringEncoded)
This class is capable of representing its own state with a String, to persist sorting state externally. The format uses zero or more column specifications, separated by commas. Here are some valid examples:
String Representation Description "column 3"Sort using the column at index 3, using that column's first comparator, in forward order "column 3 reversed"Sort using the column at index 3, using that column's first comparator, in reverse order "column 3, column 1"Sort using the column at index 3, using that column's first comparator, in forward order
then by
the column at index 1, using that column's first comparator, in forward order."column 3 comparator 2"Sort using the column at index 3, using that column's comparator at index 2, in forward order "column 3 comparator 2 reversed"Sort using the column at index 3, using that column's comparator at index 2, in reverse order "column 3 reversed, column 1 comparator 2, column 5 comparator 1 reversed, column 0"Sort using the column at index 3, using that column's first comparator, in reverse order
then by
the column at index 1, using that column's comparator at index 2, in forward order
then by
the column at index 5, using that column's comparator at index 1, in reverse order
then by
the column at index 0, using that column's first comparator, in forward order.More formally, the grammar for this String representation is as follows:
<COLUMN> = column <COLUMN INDEX> (comparator <COMPARATOR INDEX>)? (reversed)?<COMPARATOR SPEC> = ( <COLUMN> (, <COLUMN>)* )?
-
dispose
public void dispose()
-
-