2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.util.List;
27 import java.awt.event.*;
29 import javax.swing.event.*;
30 import javax.swing.table.*;
33 * TableSorter is a decorator for TableModels; adding sorting functionality to a
34 * supplied TableModel. TableSorter does not store or copy the data in its
35 * TableModel; instead it maintains a map from the row indexes of the view to
36 * the row indexes of the model. As requests are made of the sorter (like
37 * getValueAt(row, col)) they are passed to the underlying model after the row
38 * numbers have been translated via the internal mapping array. This way, the
39 * TableSorter appears to hold another copy of the table with the rows in a
42 * TableSorter registers itself as a listener to the underlying model, just as
43 * the JTable itself would. Events recieved from the model are examined,
44 * sometimes manipulated (typically widened), and then passed on to the
45 * TableSorter's listeners (typically the JTable). If a change to the model has
46 * invalidated the order of TableSorter's rows, a note of this is made and the
47 * sorter will resort the rows the next time a value is requested.
49 * When the tableHeader property is set, either by using the setTableHeader()
50 * method or the two argument constructor, the table header may be used as a
51 * complete UI for TableSorter. The default renderer of the tableHeader is
52 * decorated with a renderer that indicates the sorting status of each column.
53 * In addition, a mouse listener is installed with the following behavior:
55 * <li>Mouse-click: Clears the sorting status of all other columns and advances
56 * the sorting status of that column through three values: {NOT_SORTED,
57 * ASCENDING, DESCENDING} (then back to NOT_SORTED again).
58 * <li>SHIFT-mouse-click: Clears the sorting status of all other columns and
59 * cycles the sorting status of the column through the same three values, in the
60 * opposite order: {NOT_SORTED, DESCENDING, ASCENDING}.
61 * <li>CONTROL-mouse-click and CONTROL-SHIFT-mouse-click: as above except that
62 * the changes to the column do not cancel the statuses of columns that are
63 * already sorting - giving a way to initiate a compound sort.
66 * This is a long overdue rewrite of a class of the same name that first
67 * appeared in the swing table demos in 1997.
69 * @author Philip Milne
70 * @author Brendon McLean
71 * @author Dan van Enckevort
72 * @author Parwinder Sekhon
73 * @version 2.0 02/27/04
76 public class TableSorter extends AbstractTableModel
78 protected TableModel tableModel;
80 public static final int DESCENDING = -1;
82 public static final int NOT_SORTED = 0;
84 public static final int ASCENDING = 1;
86 private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
88 public static final Comparator COMPARABLE_COMAPRATOR = new Comparator()
90 public int compare(Object o1, Object o2)
92 return ((Comparable) o1).compareTo(o2);
96 public static final Comparator LEXICAL_COMPARATOR = new Comparator()
98 public int compare(Object o1, Object o2)
100 return o1.toString().compareTo(o2.toString());
104 private Row[] viewToModel;
106 private int[] modelToView;
108 private JTableHeader tableHeader;
110 private MouseListener mouseListener;
112 private TableModelListener tableModelListener;
114 private Map columnComparators = new HashMap();
116 private List sortingColumns = new ArrayList();
120 this.mouseListener = new MouseHandler();
121 this.tableModelListener = new TableModelHandler();
124 public TableSorter(TableModel tableModel)
127 setTableModel(tableModel);
130 public TableSorter(TableModel tableModel, JTableHeader tableHeader)
133 setTableHeader(tableHeader);
134 setTableModel(tableModel);
137 private void clearSortingState()
143 public TableModel getTableModel()
148 public void setTableModel(TableModel tableModel)
150 if (this.tableModel != null)
152 this.tableModel.removeTableModelListener(tableModelListener);
155 this.tableModel = tableModel;
156 if (this.tableModel != null)
158 this.tableModel.addTableModelListener(tableModelListener);
162 fireTableStructureChanged();
165 public JTableHeader getTableHeader()
170 public void setTableHeader(JTableHeader tableHeader)
172 if (this.tableHeader != null)
174 this.tableHeader.removeMouseListener(mouseListener);
175 TableCellRenderer defaultRenderer = this.tableHeader
176 .getDefaultRenderer();
177 if (defaultRenderer instanceof SortableHeaderRenderer)
180 .setDefaultRenderer(((SortableHeaderRenderer) defaultRenderer).tableCellRenderer);
183 this.tableHeader = tableHeader;
184 if (this.tableHeader != null)
186 this.tableHeader.addMouseListener(mouseListener);
187 this.tableHeader.setDefaultRenderer(new SortableHeaderRenderer(
188 this.tableHeader.getDefaultRenderer()));
192 public boolean isSorting()
194 return sortingColumns.size() != 0;
197 private Directive getDirective(int column)
199 for (int i = 0; i < sortingColumns.size(); i++)
201 Directive directive = (Directive) sortingColumns.get(i);
202 if (directive.column == column)
207 return EMPTY_DIRECTIVE;
210 public int getSortingStatus(int column)
212 return getDirective(column).direction;
215 private void sortingStatusChanged()
218 fireTableDataChanged();
219 if (tableHeader != null)
221 tableHeader.repaint();
225 public void setSortingStatus(int column, int status)
227 Directive directive = getDirective(column);
228 if (directive != EMPTY_DIRECTIVE)
230 sortingColumns.remove(directive);
232 if (status != NOT_SORTED)
234 sortingColumns.add(new Directive(column, status));
236 sortingStatusChanged();
239 protected Icon getHeaderRendererIcon(int column, int size)
241 Directive directive = getDirective(column);
242 if (directive == EMPTY_DIRECTIVE)
246 return new Arrow(directive.direction == DESCENDING, size,
247 sortingColumns.indexOf(directive));
250 private void cancelSorting()
252 sortingColumns.clear();
253 sortingStatusChanged();
256 public void setColumnComparator(Class type, Comparator comparator)
258 if (comparator == null)
260 columnComparators.remove(type);
264 columnComparators.put(type, comparator);
268 protected Comparator getComparator(int column)
270 Class columnType = tableModel.getColumnClass(column);
271 Comparator comparator = (Comparator) columnComparators.get(columnType);
272 if (comparator != null)
276 if (Comparable.class.isAssignableFrom(columnType))
278 return COMPARABLE_COMAPRATOR;
280 return LEXICAL_COMPARATOR;
283 private Row[] getViewToModel()
285 if (viewToModel == null)
287 int tableModelRowCount = tableModel.getRowCount();
288 viewToModel = new Row[tableModelRowCount];
289 for (int row = 0; row < tableModelRowCount; row++)
291 viewToModel[row] = new Row(row);
296 Arrays.sort(viewToModel);
302 public int modelIndex(int viewIndex)
304 return getViewToModel()[viewIndex].modelIndex;
307 private int[] getModelToView()
309 if (modelToView == null)
311 int n = getViewToModel().length;
312 modelToView = new int[n];
313 for (int i = 0; i < n; i++)
315 modelToView[modelIndex(i)] = i;
321 // TableModel interface methods
323 public int getRowCount()
325 return (tableModel == null) ? 0 : tableModel.getRowCount();
328 public int getColumnCount()
330 return (tableModel == null) ? 0 : tableModel.getColumnCount();
333 public String getColumnName(int column)
335 return tableModel.getColumnName(column);
338 public Class getColumnClass(int column)
340 return tableModel.getColumnClass(column);
343 public boolean isCellEditable(int row, int column)
345 return tableModel.isCellEditable(modelIndex(row), column);
348 public Object getValueAt(int row, int column)
350 return tableModel.getValueAt(modelIndex(row), column);
353 public void setValueAt(Object aValue, int row, int column)
355 tableModel.setValueAt(aValue, modelIndex(row), column);
360 private class Row implements Comparable
362 private int modelIndex;
364 public Row(int index)
366 this.modelIndex = index;
369 public int compareTo(Object o)
371 int row1 = modelIndex;
372 int row2 = ((Row) o).modelIndex;
374 for (Iterator it = sortingColumns.iterator(); it.hasNext();)
376 Directive directive = (Directive) it.next();
377 int column = directive.column;
378 Object o1 = tableModel.getValueAt(row1, column);
379 Object o2 = tableModel.getValueAt(row2, column);
382 // Define null less than everything, except null.
383 if (o1 == null && o2 == null)
397 comparison = getComparator(column).compare(o1, o2);
401 return directive.direction == DESCENDING ? -comparison
409 private class TableModelHandler implements TableModelListener
411 public void tableChanged(TableModelEvent e)
413 // If we're not sorting by anything, just pass the event along.
421 // If the table structure has changed, cancel the sorting; the
422 // sorting columns may have been either moved or deleted from
424 if (e.getFirstRow() == TableModelEvent.HEADER_ROW)
431 // We can map a cell event through to the view without widening
432 // when the following conditions apply:
434 // a) all the changes are on one row (e.getFirstRow() == e.getLastRow())
436 // b) all the changes are in one column (column !=
437 // TableModelEvent.ALL_COLUMNS) and,
438 // c) we are not sorting on that column (getSortingStatus(column) ==
440 // d) a reverse lookup will not trigger a sort (modelToView != null)
442 // Note: INSERT and DELETE events fail this test as they have column ==
445 // The last check, for (modelToView != null) is to see if modelToView
446 // is already allocated. If we don't do this check; sorting can become
447 // a performance bottleneck for applications where cells
448 // change rapidly in different parts of the table. If cells
449 // change alternately in the sorting column and then outside of
450 // it this class can end up re-sorting on alternate cell updates -
451 // which can be a performance problem for large tables. The last
452 // clause avoids this problem.
453 int column = e.getColumn();
454 if (e.getFirstRow() == e.getLastRow()
455 && column != TableModelEvent.ALL_COLUMNS
456 && getSortingStatus(column) == NOT_SORTED
457 && modelToView != null)
459 int viewIndex = getModelToView()[e.getFirstRow()];
460 fireTableChanged(new TableModelEvent(TableSorter.this, viewIndex,
461 viewIndex, column, e.getType()));
465 // Something has happened to the data that may have invalidated the row
468 fireTableDataChanged();
473 private class MouseHandler extends MouseAdapter
475 public void mouseClicked(MouseEvent e)
477 JTableHeader h = (JTableHeader) e.getSource();
478 TableColumnModel columnModel = h.getColumnModel();
479 int viewColumn = columnModel.getColumnIndexAtX(e.getX());
480 int column = columnModel.getColumn(viewColumn).getModelIndex();
483 int status = getSortingStatus(column);
484 if (!e.isControlDown())
488 // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING}
490 // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is
492 status = status + (e.isShiftDown() ? -1 : 1);
493 status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
494 setSortingStatus(column, status);
499 private static class Arrow implements Icon
501 private boolean descending;
505 private int priority;
507 public Arrow(boolean descending, int size, int priority)
509 this.descending = descending;
511 this.priority = priority;
514 public void paintIcon(Component c, Graphics g, int x, int y)
516 Color color = c == null ? Color.GRAY : c.getBackground();
517 // In a compound sort, make each succesive triangle 20%
518 // smaller than the previous one.
519 int dx = (int) (size / 2 * Math.pow(0.8, priority));
520 int dy = descending ? dx : -dx;
521 // Align icon (roughly) with font baseline.
522 y = y + 5 * size / 6 + (descending ? -dy : 0);
523 int shift = descending ? 1 : -1;
527 g.setColor(color.darker());
528 g.drawLine(dx / 2, dy, 0, 0);
529 g.drawLine(dx / 2, dy + shift, 0, shift);
532 g.setColor(color.brighter());
533 g.drawLine(dx / 2, dy, dx, 0);
534 g.drawLine(dx / 2, dy + shift, dx, shift);
539 g.setColor(color.darker().darker());
543 g.setColor(color.brighter().brighter());
545 g.drawLine(dx, 0, 0, 0);
551 public int getIconWidth()
556 public int getIconHeight()
562 private class SortableHeaderRenderer implements TableCellRenderer
564 private TableCellRenderer tableCellRenderer;
566 public SortableHeaderRenderer(TableCellRenderer tableCellRenderer)
568 this.tableCellRenderer = tableCellRenderer;
571 public Component getTableCellRendererComponent(JTable table,
572 Object value, boolean isSelected, boolean hasFocus, int row,
575 Component c = tableCellRenderer.getTableCellRendererComponent(table,
576 value, isSelected, hasFocus, row, column);
577 if (c instanceof JLabel)
579 JLabel l = (JLabel) c;
580 l.setHorizontalTextPosition(JLabel.LEFT);
581 int modelColumn = table.convertColumnIndexToModel(column);
582 l.setIcon(getHeaderRendererIcon(modelColumn, l.getFont().getSize()));
588 private static class Directive
592 private int direction;
594 public Directive(int column, int direction)
596 this.column = column;
597 this.direction = direction;