Class TableComparatorChooser<E>
- java.lang.Object
-
- ca.odell.glazedlists.gui.AbstractTableComparatorChooser<E>
-
- ca.odell.glazedlists.swing.TableComparatorChooser<E>
-
public class TableComparatorChooser<E> extends AbstractTableComparatorChooser<E>
A TableComparatorChooser is a tool that allows the user to sort a table by clicking on the table's headers. It requires that theJTables model is anAdvancedTableModelwith aSortedListas a source.This class includes custom arrow icons that indicate the sort order. The icons used are chosen based on the current Swing look and feel. Icons are available for the following look and feels: Mac OS X, Metal, Windows.
This class supports multiple sort strategies for each column, specified by having muliple comparators for each column. This may be useful when you want to sort a single column in either of two ways. For example, when sorting movie names, "The Phantom Menace" may be sorted under "T" for "The", or "P" for "Phantom".
This class supports sorting multiple columns simultaneously. In this mode, the user clicks a first column to sort by, and then the user clicks subsequent columns. The list is sorted by the first column and ties are broken by the second column.
If the
AdvancedTableModeluses anAdvancedTableFormat, itsAdvancedTableFormat.getColumnComparator(int)method will be used to populate the initial columnComparators.- Author:
- Jesse Wilson
- See Also:
- Bug 4, Bug 31, Bug 391
-
-
Field Summary
-
Fields inherited from class ca.odell.glazedlists.gui.AbstractTableComparatorChooser
MULTIPLE_COLUMN_KEYBOARD, MULTIPLE_COLUMN_MOUSE, MULTIPLE_COLUMN_MOUSE_WITH_UNDO, SINGLE_COLUMN, sortedList, sortedListComparator, sortingState
-
-
Constructor Summary
Constructors Modifier Constructor Description TableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, boolean multipleColumnSort)Deprecated.replaced withinstall(javax.swing.JTable, ca.odell.glazedlists.SortedList<E>, java.lang.Object), which is functionally identical but uses a more fitting name to convey the action that is performed and fixes an API flaw by explicitly requiring the TableFormat.TableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy)Deprecated.9/25/06 replaced withinstall(javax.swing.JTable, ca.odell.glazedlists.SortedList<E>, java.lang.Object), which is functionally identical but uses a more fitting name to convey the action that is performed and fixes an API flaw by explicitly requiring the TableFormat.protectedTableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy, TableFormat<? super E> tableFormat)Creates and installs a TableComparatorChooser.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSortActionListener(java.awt.event.ActionListener sortActionListener)Registers the specifiedActionListenerto receive notification whenever theJTableis sorted by thisTableComparatorChooser.javax.swing.table.TableCellRenderercreateSortArrowHeaderRenderer(javax.swing.table.TableCellRenderer delegateRenderer)Decorates and returns the givendelegateRendererwith functionality that attempts to install a sorting icon into the Component returned by thedelegateRenderer.voiddispose()Releases the resources consumed by thisTableComparatorChooserso that it may eventually be garbage collected.protected intgetSortingStyle(int column)Gets the sorting style currently applied to the specified column.static <E> TableComparatorChooser<E>install(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy)Installs a new TableComparatorChooser that responds to clicks on the header of the specified table and uses them to sort the specifiedsortedListby delegating to the givenstrategyIf at any time the table should no longer sort, the behaviour can be removed callingdispose()on the object returned by this method.static <E> TableComparatorChooser<E>install(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy, TableFormat<? super E> tableFormat)Installs a new TableComparatorChooser that responds to clicks on the header of the specified table and uses them to sort the specifiedsortedListby delegating to the givenstrategyIf at any time the table should no longer sort, the behaviour can be removed callingdispose()on the object returned by this method.protected booleanisSortingMouseEvent(java.awt.event.MouseEvent e)Determines if the specified mouse event shall be handled by thisTableComparatorChooser.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.voidremoveSortActionListener(java.awt.event.ActionListener sortActionListener)Deregisters the specifiedActionListenerto no longer receive action events.static voidsetIconPath(java.lang.String path)Set allTableComparatorChoosers to use the icons from the directory specified.-
Methods inherited from class ca.odell.glazedlists.gui.AbstractTableComparatorChooser
appendComparator, clearComparator, createComparatorForElement, createSortingState, fromString, getColumnComparatorIndex, getComparatorsForColumn, getSortingColumns, isColumnReverse, setTableFormat, toString
-
-
-
-
Constructor Detail
-
TableComparatorChooser
public TableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, boolean multipleColumnSort)Deprecated.replaced withinstall(javax.swing.JTable, ca.odell.glazedlists.SortedList<E>, java.lang.Object), which is functionally identical but uses a more fitting name to convey the action that is performed and fixes an API flaw by explicitly requiring the TableFormat.Creates and installs a TableComparatorChooser.
-
TableComparatorChooser
public TableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy)Deprecated.9/25/06 replaced withinstall(javax.swing.JTable, ca.odell.glazedlists.SortedList<E>, java.lang.Object), which is functionally identical but uses a more fitting name to convey the action that is performed and fixes an API flaw by explicitly requiring the TableFormat.Creates and installs a TableComparatorChooser.
-
TableComparatorChooser
protected TableComparatorChooser(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy, TableFormat<? super E> tableFormat)Creates and installs a TableComparatorChooser.- Parameters:
table- the table with headers that can be clicked onsortedList- the sorted list to updatestrategy- an implementations ofSortingStrategy, typically one oftableFormat- the TableFormat providing the columns for the table
-
-
Method Detail
-
install
public static <E> TableComparatorChooser<E> install(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy)
Installs a new TableComparatorChooser that responds to clicks on the header of the specified table and uses them to sort the specifiedsortedListby delegating to the givenstrategyIf at any time the table should no longer sort, the behaviour can be removed callingdispose()on the object returned by this method.This method assumes that the JTable is backed by an AdvancedTableModel and it is from that AdvancedTableModel that the TableFormat should be extracted. This is, by far, the typical case and so we provide this simpler install method for convenience.
- Parameters:
table- the table with headers that can be clicked onsortedList- the sorted list to updatestrategy- an implementations ofSortingStrategy, typically one of- Returns:
- TableComparatorChooser object that is responsible for translating mouse clicks on the table header into sorting actions on the sortedList.
-
install
public static <E> TableComparatorChooser<E> install(javax.swing.JTable table, SortedList<E> sortedList, java.lang.Object strategy, TableFormat<? super E> tableFormat)
Installs a new TableComparatorChooser that responds to clicks on the header of the specified table and uses them to sort the specifiedsortedListby delegating to the givenstrategyIf at any time the table should no longer sort, the behaviour can be removed callingdispose()on the object returned by this method.This method makes no assumptions about the TableModel implementation that backs the JTable. As such, it requires the TableFormat as an explicit parameter and expects the TableFormat to be constant (i.e. never changes) for the life of the TableComparatorChooser.
- Parameters:
table- the table with headers that can be clicked ontableFormat- the TableFormat providing the columns for the tablesortedList- the sorted list to updatestrategy- an implementations ofSortingStrategy, typically one of- Returns:
- TableComparatorChooser object that is responsible for translating mouse clicks on the table header into sorting actions on the sortedList.
-
addSortActionListener
public void addSortActionListener(java.awt.event.ActionListener sortActionListener)
Registers the specifiedActionListenerto receive notification whenever theJTableis sorted by thisTableComparatorChooser.
-
removeSortActionListener
public void removeSortActionListener(java.awt.event.ActionListener sortActionListener)
Deregisters the specifiedActionListenerto no longer receive action events.
-
createSortArrowHeaderRenderer
public javax.swing.table.TableCellRenderer createSortArrowHeaderRenderer(javax.swing.table.TableCellRenderer delegateRenderer)
Decorates and returns the givendelegateRendererwith functionality that attempts to install a sorting icon into the Component returned by thedelegateRenderer. In particular, thedelegateRendererwill be decorated with a sorting icon in one of two scenarios:- the delegateRenderer implements
SortableRenderer- in this casesetSortIconis called on the delegateRenderer and it is expected to place the icon anywhere it desires on the Component it returns. This allows maximum flexibility when displaying the sort icon. - the Component returned by the delegateRenderer is a JLabel - in
this case
setIconis called on the JLabel with the sort icon. This caters to the typical case when aDefaultTableCellRendereris used as the delegateRenderer.
- Parameters:
delegateRenderer- the TableCellRenderer acting as a table header renderer and to which a sort icon should be added- Returns:
- a TableCellRenderer that attempts to decorate the given
delegateRendererwith a sort icon
- the delegateRenderer implements
-
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.- Overrides:
redetectComparatorin classAbstractTableComparatorChooser<E>
-
rebuildComparator
protected final void rebuildComparator()
Updates the comparator in use and applies it to the table.- Overrides:
rebuildComparatorin classAbstractTableComparatorChooser<E>
-
getSortingStyle
protected final int getSortingStyle(int column)
Gets the sorting style currently applied to the specified column.- Overrides:
getSortingStylein classAbstractTableComparatorChooser<E>
-
isSortingMouseEvent
protected boolean isSortingMouseEvent(java.awt.event.MouseEvent e)
Determines if the specified mouse event shall be handled by thisTableComparatorChooser. The default implementation handles only clicks with the left mouse button. Extending classes can customize which mouse events the table comparator chooser responds to by overriding this method.As of 2005/12/20, this method is no longer called when the corresponding mouse press event was a popup trigger. In effect, if this is a right-click on Windows or a 'control-click' on the Mac.
As of 2008/02/05, this method is no longer called when the Cursor over the JTableHeader indicates a column resize is expected to take place, rather than a change in sort.
-
setIconPath
public static void setIconPath(java.lang.String path)
Set allTableComparatorChoosers to use the icons from the directory specified. The folder should contain the following eight icon files:- primary_sorted.png
- secondary_sorted.png
- primary_sorted_alternate.png
- secondary_sorted_alternate.png
- primary_sorted_alternate_reverse.png
- secondary_sorted_alternate_reverse.png
- primary_sorted_reverse.png
- secondary_sorted_reverse.png
Note that this path must be on the system classpath. It may be within a jar file.
-
dispose
public void dispose()
Releases the resources consumed by thisTableComparatorChooserso that it may eventually be garbage collected.A
TableComparatorChooserwill be garbage collected without a call todispose(), but not before its sourceEventListis garbage collected. By callingdispose(), you allow theTableComparatorChooserto be garbage collected before its sourceEventList. This is necessary for situations where anTableComparatorChooseris short-lived but its sourceEventListis long-lived.Warning: It is an error to call any method on a
TableComparatorChooserafter it has been disposed.- Overrides:
disposein classAbstractTableComparatorChooser<E>
-
-